The Level type represents a logging priority. Higher levels indicate more severe conditions. The Logger discards messages with a level lower than its configured minimum.
Level type
Represents a logging priority with values ranging from DebugLevel to FatalLevel.
Level constants
DebugLevel
Designates fine grained informational events that are most useful to debug an application.
const DebugLevel Level = -1
Example
logger.SetLevel(velo.DebugLevel)
logger.Debug("entering function", "name", "processRequest")
InfoLevel
Designates informational messages that highlight the progress of the application at coarse grained level. This is the default level.
const InfoLevel Level = 0
Example
logger.Info("server started", "port", 8080)
WarnLevel
Designates potentially harmful situations.
const WarnLevel Level = 1
Example
logger.Warn("high memory usage", "percent", 85)
ErrorLevel
Designates error events that might still allow the application to continue running.
const ErrorLevel Level = 2
Example
logger.Error("failed to connect to database", "error", err)
DPanicLevel
Designates critical errors. In development, the Logger panics after writing the message.
const DPanicLevel Level = 3
Example
logger.Log(velo.DPanicLevel, "critical state detected")
PanicLevel
Designates severe errors. The Logger panics after writing the message.
const PanicLevel Level = 4
Example
logger.Panic("unrecoverable error", "component", "auth")
FatalLevel
Designates very severe error events. The Logger calls os.Exit(1) after writing the message.
const FatalLevel Level = 5
Example
logger.Fatal("cannot start application", "error", err)
Level methods
String
Returns the lowercase ASCII representation of the level.
func (l Level) String() string
Example
level := velo.InfoLevel
fmt.Println(level.String()) // Output: info
JSONField
Returns the formatted JSON key-value pair for the level.
func (l Level) JSONField() string
Provides a zero allocation string (e.g., "level":"info") for the JSONEncoder to use during serialization.
Example
level := velo.InfoLevel
fmt.Println(level.JSONField()) // Output: "level":"info"
MarshalText
Serializes the Level to text.
func (l Level) MarshalText() ([]byte, error)
Returns the lowercase string representation of the level (e.g., “info”).
Example
level := velo.WarnLevel
data, _ := level.MarshalText()
fmt.Println(string(data)) // Output: warn
UnmarshalText
Deserializes text into a Level.
func (l *Level) UnmarshalText(text []byte) error
The text representation of the level
Accepts lowercase or uppercase string representations (e.g., “info” or “INFO”). This facilitates configuring log levels via YAML, TOML, or JSON.
Example
var level velo.Level
err := level.UnmarshalText([]byte("error"))
if err != nil {
panic(err)
}
fmt.Println(level) // Output: error
Parsing functions
ParseLevel
Converts a string into a Level.
func ParseLevel(text string) (Level, error)
The string representation of the level
Accepts lowercase or uppercase string representations. Returns an error if the string does not match a known level.
Example
level, err := velo.ParseLevel("warn")
if err != nil {
panic(err)
}
logger.SetLevel(level)
AtomicLevel
Represents a dynamically adjustable logging level.
type AtomicLevel struct {
// contains filtered or unexported fields
}
Allows you to safely change the log level of a Logger and all its descendants at runtime without restarting the application. You must create an AtomicLevel using the NewAtomicLevel or NewAtomicLevelAt constructors.
NewAtomicLevel
Initializes an AtomicLevel set to InfoLevel.
func NewAtomicLevel() AtomicLevel
Example
atomicLevel := velo.NewAtomicLevel()
fmt.Println(atomicLevel.Level()) // Output: info
NewAtomicLevelAt
Initializes an AtomicLevel set to the specified Level.
func NewAtomicLevelAt(l Level) AtomicLevel
Example
atomicLevel := velo.NewAtomicLevelAt(velo.WarnLevel)
fmt.Println(atomicLevel.Level()) // Output: warn
ParseAtomicLevel
Converts a string into an AtomicLevel.
func ParseAtomicLevel(text string) (AtomicLevel, error)
The string representation of the level
Accepts lowercase or uppercase string representations. Returns an error if the string does not match a known level.
Example
atomicLevel, err := velo.ParseAtomicLevel("debug")
if err != nil {
panic(err)
}
AtomicLevel methods
Level
Retrieves the current minimum logging level.
func (lvl AtomicLevel) Level() Level
Example
atomicLevel := velo.NewAtomicLevel()
fmt.Println(atomicLevel.Level()) // Output: info
SetLevel
Updates the minimum logging level safely across all goroutines.
func (lvl AtomicLevel) SetLevel(l Level)
Example
atomicLevel := velo.NewAtomicLevel()
atomicLevel.SetLevel(velo.ErrorLevel)
Enabled
Determines if the specified level meets or exceeds the current minimum.
func (lvl AtomicLevel) Enabled(l Level) bool
Example
atomicLevel := velo.NewAtomicLevelAt(velo.WarnLevel)
if atomicLevel.Enabled(velo.ErrorLevel) {
fmt.Println("Error logging is enabled")
}
String
Returns the string representation of the current minimum level.
func (lvl AtomicLevel) String() string
Example
atomicLevel := velo.NewAtomicLevelAt(velo.DebugLevel)
fmt.Println(atomicLevel.String()) // Output: debug
MarshalText
Serializes the AtomicLevel to a byte slice.
func (lvl AtomicLevel) MarshalText() (text []byte, err error)
Returns the lowercase string representation of the current minimum level.
Example
atomicLevel := velo.NewAtomicLevelAt(velo.InfoLevel)
data, _ := atomicLevel.MarshalText()
fmt.Println(string(data)) // Output: info
UnmarshalText
Deserializes text into the AtomicLevel.
func (lvl *AtomicLevel) UnmarshalText(text []byte) error
The text representation of the level
Accepts the same string representations as the static Level type.
Example
var atomicLevel velo.AtomicLevel
err := atomicLevel.UnmarshalText([]byte("warn"))
if err != nil {
panic(err)
}
fmt.Println(atomicLevel.Level()) // Output: warn