observer/pkg/level/level.go

111 lines
2.8 KiB
Go
Raw Permalink Normal View History

2024-04-26 13:09:54 +00:00
package level
import (
"strings"
"git.ma-al.com/maal-libraries/observer/pkg/syslog"
2024-04-26 13:09:54 +00:00
"go.opentelemetry.io/otel/attribute"
)
type SeverityLevel uint8
2024-04-26 13:09:54 +00:00
const (
// A magical zero value.
// WARN: DO NOT USE IN LOGS OR BASICALLY EVER
unset SeverityLevel = iota
// 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.
ALERT
// 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
// 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
// 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
// Verbose information that is useful and meaningful to application developers and system administrators.
2024-04-26 13:09:54 +00:00
DEBUG
// 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
)
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"
case WARN:
2024-04-26 13:09:54 +00:00
return "WARN"
case INFO:
return "INFO"
case DEBUG:
return "DEBUG"
case TRACE:
return "TRACE"
2024-04-26 13:09:54 +00:00
default:
return "CRIT"
}
}
func FromString(level string) SeverityLevel {
level = strings.ToLower(level)
2024-04-26 13:09:54 +00:00
switch level {
case "alert":
2024-04-26 13:09:54 +00:00
return ALERT
case "crit", "critical":
2024-04-26 13:09:54 +00:00
return CRIT
case "err", "error":
2024-04-26 13:09:54 +00:00
return ERR
case "warn", "warning":
return WARN
case "info":
2024-04-26 13:09:54 +00:00
return INFO
case "debug":
2024-04-26 13:09:54 +00:00
return DEBUG
case "trace":
return TRACE
2024-04-26 13:09:54 +00:00
default:
return unset
2024-04-26 13:09:54 +00:00
}
}
func (lvl SeverityLevel) IntoTraceAttribute() attribute.KeyValue {
return attribute.String("level", lvl.String())
}
2024-04-26 13:09:54 +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
}