Documentation ¶
Overview ¶
Package log provides an improved logger
Example ¶
logger, err := New("/path/to/file.log", 0644) if err != nil { fmt.Printf("Error: %v\n", err) return } // Enable buffered IO with 1-second flush interval logger.EnableBufIO(time.Second) // Set minimal log level logger.MinLevel(INFO) // Print message to log logger.Print(DEBUG, "This is %s message", "debug") // Package provides different methods for each level logger.Debug("This is %d %s message", 2, "debug") logger.Info("This is info message") logger.Warn("This is warning message") logger.Error("This is error message") logger.Crit("This is critical message") // Enable colors for output logger.UseColors = true // Encode messages to JSON logger.UseJSON = true // Print caller info logger.WithCaller = true // Use custom date & time layout logger.TimeLayout = time.RFC3339 // Add fields to message logger.Debug("This is %d %s message", 2, "debug", F{"user", "bob"}, F{"id", 200}) // Or collection of fields. Fields do not require initialization. var logFields Fields logFields.Add(F{"user", "bob"}, F{"id", 200}) logger.Debug("This is %d %s message", 2, "debug", logFields) // AUX message it's unskippable message which will be printed to log file with // any minimum level // // Note that all AUX messages are dropped when using JSON format logger.Aux("This is aux message") // Print simple divider logger.Divider() // For log rotation we provide method Reopen logger.Reopen() // If buffered IO is used, you should flush data before exit logger.Flush()
Output:
Index ¶
- Constants
- Variables
- func Aux(f string, a ...any) error
- func Crit(f string, a ...any) error
- func Debug(f string, a ...any) error
- func Divider() error
- func EnableBufIO(interval time.Duration)
- func Error(f string, a ...any) error
- func Flush() error
- func Info(f string, a ...any) error
- func Is(level uint8) bool
- func MinLevel(level any) error
- func Print(level uint8, f string, a ...any) error
- func Reopen() error
- func Set(file string, perms os.FileMode) error
- func Warn(f string, a ...any) error
- type F
- type Field
- type Fields
- type ILogger
- type Logger
- func (l *Logger) Aux(f string, a ...any) error
- func (l *Logger) Crit(f string, a ...any) error
- func (l *Logger) Debug(f string, a ...any) error
- func (l *Logger) Divider() error
- func (l *Logger) EnableBufIO(flushInterval time.Duration)
- func (l *Logger) Error(f string, a ...any) error
- func (l *Logger) Flush() error
- func (l *Logger) Info(f string, a ...any) error
- func (l *Logger) Is(level uint8) bool
- func (l *Logger) MinLevel(level any) error
- func (l *Logger) Print(level uint8, f string, a ...any) error
- func (l *Logger) Reopen() error
- func (l *Logger) Set(file string, perms os.FileMode) error
- func (l *Logger) Warn(f string, a ...any) error
- type NilLogger
- func (l *NilLogger) Aux(f string, a ...any) error
- func (l *NilLogger) Crit(f string, a ...any) error
- func (l *NilLogger) Debug(f string, a ...any) error
- func (l *NilLogger) Error(f string, a ...any) error
- func (l *NilLogger) Info(f string, a ...any) error
- func (l *NilLogger) Print(level uint8, f string, a ...any) error
- func (l *NilLogger) Warn(f string, a ...any) error
- type StdLogger
- func (l *StdLogger) Fatal(v ...any)
- func (l *StdLogger) Fatalf(format string, v ...any)
- func (l *StdLogger) Fatalln(v ...any)
- func (l *StdLogger) Output(calldepth int, s string) error
- func (l *StdLogger) Panic(v ...any)
- func (l *StdLogger) Panicf(format string, v ...any)
- func (l *StdLogger) Panicln(v ...any)
- func (l *StdLogger) Print(v ...any)
- func (l *StdLogger) Printf(format string, v ...any)
- func (l *StdLogger) Println(v ...any)
Examples ¶
- Package
- Aux
- Crit
- Debug
- Divider
- EnableBufIO
- Error
- Fields.Add
- Fields.Reset
- Flush
- Info
- Is
- Logger.Aux
- Logger.Crit
- Logger.Debug
- Logger.Divider
- Logger.EnableBufIO
- Logger.Error
- Logger.Flush
- Logger.Info
- Logger.Is
- Logger.MinLevel
- Logger.Print
- Logger.Reopen
- Logger.Set
- Logger.Warn
- MinLevel
- New
- Reopen
- Set
- Warn
Constants ¶
const ( DEBUG uint8 = 0 // DEBUG debug messages INFO uint8 = 1 // INFO info messages WARN uint8 = 2 // WARN warning messages ERROR uint8 = 3 // ERROR error messages CRIT uint8 = 4 // CRIT critical error messages AUX uint8 = 99 // AUX unskipable messages (separators, headers, etc...) )
const ( DATE_LAYOUT_TEXT = "2006/01/02 15:04:05.000" // Datetime layout for text logs DATE_LAYOUT_JSON = time.RFC3339 // Datetime layout for JSON logs )
Variables ¶
var ( // ErrNilLogger is returned by Logger struct methods if struct is nil ErrNilLogger = errors.New("Logger is nil") // ErrUnexpectedLevel is returned by the MinLevel method if given level is unknown ErrUnexpectedLevel = errors.New("Unexpected level type") // ErrOutputNotSet is returned by the Reopen method if output file is not set ErrOutputNotSet = errors.New("Output file is not set") )
Errors
var Colors = map[uint8]string{ DEBUG: "{s-}", INFO: "", WARN: "{y}", ERROR: "{#208}", CRIT: "{#196}{*}", }
Colors colors is map with fmtc color tags for every level
var Global = &Logger{ PrefixWarn: true, PrefixError: true, PrefixCrit: true, minLevel: INFO, mu: &sync.Mutex{}, }
Global is global logger struct
var PrefixMap = map[uint8]string{ DEBUG: "[DEBUG]", INFO: "[INFO]", WARN: "[WARNING]", ERROR: "[ERROR]", CRIT: "[CRITICAL]", }
PrefixMap is map with messages prefixes
Functions ¶
func Aux ¶
Aux writes unskippable message (for separators/headers)
Example ¶
err := Set("/path/to/file.log", 0644) if err != nil { fmt.Printf("Error: %v\n", err) return } Aux("Auxiliary message")
Output:
func Crit ¶
Crit writes critical message to global logger output
Example ¶
err := Set("/path/to/file.log", 0644) if err != nil { fmt.Printf("Error: %v\n", err) return } Crit("Critical error message")
Output:
func Debug ¶
Debug writes debug message to global logger output
Example ¶
err := Set("/path/to/file.log", 0644) if err != nil { fmt.Printf("Error: %v\n", err) return } Debug("Debug message")
Output:
func Divider ¶
func Divider() error
Divider writes simple divider to logger output
Example ¶
err := Set("/path/to/file.log", 0644) if err != nil { fmt.Printf("Error: %v\n", err) return } Divider()
Output:
func EnableBufIO ¶
EnableBufIO enables buffered I/O
Example ¶
err := Set("/path/to/file.log", 0644) if err != nil { fmt.Printf("Error: %v\n", err) return } // Enable buffered IO with 1-second flush interval EnableBufIO(time.Second) Info("Info message") Flush()
Output:
func Error ¶
Error writes error message to global logger output
Example ¶
err := Set("/path/to/file.log", 0644) if err != nil { fmt.Printf("Error: %v\n", err) return } Error("Error message")
Output:
func Flush ¶
func Flush() error
Flush writes buffered data to file
Example ¶
err := Set("/path/to/file.log", 0644) if err != nil { fmt.Printf("Error: %v\n", err) return } // Enable buffered IO with 1-second flush interval EnableBufIO(time.Second) Info("Info message") Flush()
Output:
func Info ¶
Info writes info message to global logger output
Example ¶
err := Set("/path/to/file.log", 0644) if err != nil { fmt.Printf("Error: %v\n", err) return } Info("Info message")
Output:
func Is ¶
Is returns true if current minimal logging level is equal or greater than given
Example ¶
err := Set("/path/to/file.log", 0644) if err != nil { fmt.Printf("Error: %v\n", err) return } if Is(INFO) { Info("Info message") }
Output:
func MinLevel ¶
MinLevel defines minimal logging level
Example ¶
err := Set("/path/to/file.log", 0644) if err != nil { fmt.Printf("Error: %v\n", err) return } // Set minimal log level to error MinLevel(ERROR) Info("This message is not displayed") Error("This message is displayed")
Output:
func Print ¶
Print writes message to global logger output
Example ¶
err := Set("/path/to/file.log", 0644) if err != nil { fmt.Printf("Error: %v\n", err) return } Print(INFO, "Info message") Print(ERROR, "Error message")
Output:
func Reopen ¶
func Reopen() error
Reopen close file descriptor for global logger and open it again Useful for log rotation
Example ¶
err := Set("/path/to/file.log", 0644) if err != nil { fmt.Printf("Error: %v\n", err) return } // For log rotation we provide method Reopen Reopen()
Output:
Types ¶
type Fields ¶
type Fields struct {
// contains filtered or unexported fields
}
Fields is a field collection
func (*Fields) Add ¶
Add adds given fields to collection
Example ¶
// Fields do not require initialization var f Fields f.Add(F{"user", "bob"}, F{"id", 200}) Debug("This is %d %s message", 2, "debug", f)
Output:
func (*Fields) Reset ¶
Reset removes all fields from collection
Example ¶
// Fields do not require initialization var f Fields f.Add(F{"user", "bob"}, F{"id", 200}) Debug("This is %d %s message", 2, "debug", f) // With Reset you can reuse Fields instance f.Reset().Add(F{"user", "john"}, F{"id", 201}) Debug("This is %d %s message", 3, "debug", f)
Output:
type ILogger ¶
type ILogger interface { Aux(f string, a ...any) error Debug(f string, a ...any) error Info(f string, a ...any) error Warn(f string, a ...any) error Error(f string, a ...any) error Crit(f string, a ...any) error Print(level uint8, f string, a ...any) error }
ILogger is interface for compatible loggers
type Logger ¶
type Logger struct { PrefixDebug bool // Show prefix for debug messages PrefixInfo bool // Show prefix for info messages PrefixWarn bool // Show prefix for warning messages PrefixError bool // Show prefix for error messages PrefixCrit bool // Show prefix for critical/fatal messages TimeLayout string // Date and time layout used for rendering dates UseColors bool // Enable ANSI escape codes for colors in output UseJSON bool // Encode messages to JSON WithCaller bool // Show caller info WithFullCallerPath bool // Show full path of caller // contains filtered or unexported fields }
Logger is a basic logger struct
func New ¶
New creates new logger struct
Example ¶
logger, err := New("/path/to/file.log", 0644) if err != nil { fmt.Printf("Error: %v\n", err) return } // Package provides different methods for each level logger.Debug("This is %d %s message", 2, "debug") logger.Info("This is info message") logger.Warn("This is warning message") logger.Error("This is error message") logger.Crit("This is critical message") // If buffered IO is used, you should flush data before exit logger.Flush()
Output:
func (*Logger) Aux ¶
Aux writes unfiltered message (for separators/headers) to logger output
Example ¶
logger, err := New("/path/to/file.log", 0644) if err != nil { fmt.Printf("Error: %v\n", err) return } logger.Aux("Auxiliary message")
Output:
func (*Logger) Crit ¶
Crit writes critical message to logger output
Example ¶
logger, err := New("/path/to/file.log", 0644) if err != nil { fmt.Printf("Error: %v\n", err) return } logger.Crit("Critical error message")
Output:
func (*Logger) Debug ¶
Debug writes debug message to logger output
Example ¶
logger, err := New("/path/to/file.log", 0644) if err != nil { fmt.Printf("Error: %v\n", err) return } logger.Debug("Debug message")
Output:
func (*Logger) Divider ¶
Divider writes simple divider to logger output
Example ¶
logger, err := New("/path/to/file.log", 0644) if err != nil { fmt.Printf("Error: %v\n", err) return } logger.Divider()
Output:
func (*Logger) EnableBufIO ¶
EnableBufIO enables buffered I/O support
Example ¶
logger, err := New("/path/to/file.log", 0644) if err != nil { fmt.Printf("Error: %v\n", err) return } // Enable buffered IO with 1-second flush interval logger.EnableBufIO(time.Second) logger.Info("Info message") logger.Flush()
Output:
func (*Logger) Error ¶
Error writes error message to logger output
Example ¶
logger, err := New("/path/to/file.log", 0644) if err != nil { fmt.Printf("Error: %v\n", err) return } logger.Error("Error message")
Output:
func (*Logger) Flush ¶
Flush writes buffered data to file
Example ¶
logger, err := New("/path/to/file.log", 0644) if err != nil { fmt.Printf("Error: %v\n", err) return } // Enable buffered IO with 1-second flush interval logger.EnableBufIO(time.Second) logger.Info("Info message") logger.Flush()
Output:
func (*Logger) Info ¶
Info writes info message to logger output
Example ¶
logger, err := New("/path/to/file.log", 0644) if err != nil { fmt.Printf("Error: %v\n", err) return } logger.Info("Info message")
Output:
func (*Logger) Is ¶
Is returns true if current minimal logging level is equal or greater than given
Example ¶
logger, err := New("/path/to/file.log", 0644) if err != nil { fmt.Printf("Error: %v\n", err) return } if logger.Is(INFO) { logger.Info("Info message") }
Output:
func (*Logger) MinLevel ¶
MinLevel defines minimal logging level
Example ¶
logger, err := New("/path/to/file.log", 0644) if err != nil { fmt.Printf("Error: %v\n", err) return } // Set minimal log level to error logger.MinLevel(ERROR) logger.Info("This message is not displayed") logger.Error("This message is displayed")
Output:
func (*Logger) Print ¶
Print writes message to logger output
Example ¶
logger, err := New("/path/to/file.log", 0644) if err != nil { fmt.Printf("Error: %v\n", err) return } logger.Print(INFO, "Info message") logger.Print(ERROR, "Error message")
Output:
func (*Logger) Reopen ¶
Reopen closes file descriptor and opens it again Useful for log rotation
Example ¶
logger, err := New("/path/to/file.log", 0644) if err != nil { fmt.Printf("Error: %v\n", err) return } // For log rotation we provide method Reopen logger.Reopen()
Output:
func (*Logger) Set ¶
Set changes logger output target
Example ¶
logger, err := New("/path/to/file.log", 0644) if err != nil { fmt.Printf("Error: %v\n", err) return } err = logger.Set("/path/to/file2.log", 0640) if err != nil { fmt.Printf("Error: %v\n", err) return } logger.Info("Message will go to file2.log")
Output:
type NilLogger ¶
type NilLogger struct{}
NilLogger is Logger (ILogger) compatible logger that doesn't print anything
type StdLogger ¶
type StdLogger struct {
Logger *Logger
}
StdLogger is logger wrapper compatible with stdlib logger