2024-04-26 13:09:54 +00:00
|
|
|
package level
|
|
|
|
|
|
|
|
import (
|
2024-05-20 06:20:13 +00:00
|
|
|
"git.ma-al.com/maal-libraries/observer/pkg/syslog"
|
2024-04-26 13:09:54 +00:00
|
|
|
"go.opentelemetry.io/otel/attribute"
|
|
|
|
)
|
|
|
|
|
2024-05-16 11:45:13 +00:00
|
|
|
type SeverityLevel uint8
|
2024-04-26 13:09:54 +00:00
|
|
|
|
|
|
|
const (
|
2024-05-17 13:31:35 +00:00
|
|
|
// A magical zero value.
|
|
|
|
// WARN: DO NOT USE IN LOGS OR BASICALLY EVER
|
|
|
|
unset SeverityLevel = iota
|
2024-05-16 11:45:13 +00:00
|
|
|
// This event requires an immediate action. If you suspect that occurence of an event may signal that the
|
|
|
|
// data will get lost, corrupted, or that the application will change its behaviour following the event in
|
|
|
|
// an undesired way, select the ALERT level.
|
2024-05-17 13:31:35 +00:00
|
|
|
ALERT
|
2024-05-16 11:45:13 +00:00
|
|
|
// A critical error has occured. Critical errors are such which can be tough to fix or made the users
|
|
|
|
// experience significantly worse but unlike errors that trigger ALERT they can be fixed at any moment.
|
2024-04-26 13:09:54 +00:00
|
|
|
CRIT
|
2024-05-16 11:45:13 +00:00
|
|
|
// An error has occured but it is not expected to cause any serious issues. These will be often
|
|
|
|
// `Internal Server Error` responses from an HTTP server.
|
2024-04-26 13:09:54 +00:00
|
|
|
ERR
|
2024-05-16 11:45:13 +00:00
|
|
|
// Signals that something suspicious has happened, for example, a query took too long to execute, gaining access
|
|
|
|
// to a resource took multiple attempts, a conflict was automatically resolved, etc.
|
|
|
|
WARN
|
|
|
|
// Used to inform about standard, expected events of an application, like creation of a new object or a new
|
|
|
|
// log-in from a user. Information that could be:
|
|
|
|
// - used to audit the application,
|
|
|
|
// - resolve customer's complaints,
|
|
|
|
// - track history of significant changes,
|
|
|
|
// - calculate valuable statistics;
|
|
|
|
// should be collected and logged at this level.
|
2024-04-26 13:09:54 +00:00
|
|
|
INFO
|
2024-05-16 11:45:13 +00:00
|
|
|
// Verbose information that is useful and meaningful to application developers and system administrators.
|
2024-04-26 13:09:54 +00:00
|
|
|
DEBUG
|
2024-05-16 11:45:13 +00:00
|
|
|
// Extremely verbose information that can be used to investigate performance of specific parts of an application.
|
|
|
|
// It is transled to [syslog.DEBUG] by [IntoSyslogLevel].
|
|
|
|
TRACE
|
2024-04-26 13:09:54 +00:00
|
|
|
)
|
|
|
|
|
2024-05-16 11:45:13 +00:00
|
|
|
func (l SeverityLevel) String() string {
|
2024-04-26 13:09:54 +00:00
|
|
|
switch l {
|
|
|
|
case ALERT:
|
|
|
|
return "ALERT"
|
|
|
|
case CRIT:
|
|
|
|
return "CRIT"
|
|
|
|
case ERR:
|
|
|
|
return "ERR"
|
2024-05-16 11:45:13 +00:00
|
|
|
case WARN:
|
2024-04-26 13:09:54 +00:00
|
|
|
return "WARN"
|
|
|
|
case INFO:
|
|
|
|
return "INFO"
|
|
|
|
case DEBUG:
|
|
|
|
return "DEBUG"
|
2024-05-17 13:31:35 +00:00
|
|
|
case TRACE:
|
|
|
|
return "TRACE"
|
2024-04-26 13:09:54 +00:00
|
|
|
default:
|
|
|
|
return "CRIT"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-05-16 11:45:13 +00:00
|
|
|
func FromString(level string) SeverityLevel {
|
2024-04-26 13:09:54 +00:00
|
|
|
switch level {
|
|
|
|
case "ALERT":
|
|
|
|
return ALERT
|
|
|
|
case "CRIT":
|
|
|
|
return CRIT
|
|
|
|
case "ERR":
|
|
|
|
return ERR
|
|
|
|
case "WARN":
|
2024-05-16 11:45:13 +00:00
|
|
|
return WARN
|
2024-04-26 13:09:54 +00:00
|
|
|
case "INFO":
|
|
|
|
return INFO
|
|
|
|
case "DEBUG":
|
|
|
|
return DEBUG
|
2024-05-17 13:31:35 +00:00
|
|
|
case "TRACE":
|
|
|
|
return TRACE
|
2024-04-26 13:09:54 +00:00
|
|
|
default:
|
|
|
|
return CRIT
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-05-16 11:45:13 +00:00
|
|
|
func (lvl SeverityLevel) IntoTraceAttribute() attribute.KeyValue {
|
|
|
|
return attribute.String("level", lvl.String())
|
|
|
|
}
|
2024-04-26 13:09:54 +00:00
|
|
|
|
2024-05-16 11:45:13 +00:00
|
|
|
func (lvl SeverityLevel) IntoSyslogLevel() syslog.SyslogLevel {
|
|
|
|
switch lvl {
|
|
|
|
case ALERT:
|
|
|
|
return syslog.ALERT
|
|
|
|
case CRIT:
|
|
|
|
return syslog.CRIT
|
|
|
|
case ERR:
|
|
|
|
return syslog.ERR
|
|
|
|
case WARN:
|
|
|
|
return syslog.WARNING
|
|
|
|
case INFO:
|
|
|
|
return syslog.INFO
|
|
|
|
case DEBUG:
|
|
|
|
return syslog.DEBUG
|
|
|
|
case TRACE:
|
|
|
|
return syslog.DEBUG
|
|
|
|
default:
|
|
|
|
return syslog.EMERG
|
|
|
|
}
|
2024-04-26 13:09:54 +00:00
|
|
|
}
|