Documentation ¶
Overview ¶
Package log provides an alternative to the standard log library.
Each LogLevels can have its own log provider. That means INFO can be logged in a file, ERROR is mailed and everything else will logged in the console. Different loggers with different log levels can be created. This means you can have a log for an importer and a different log for the application it self.
The log is easy to extend by implementing the log.Interface.
Example ¶
This example demonstrate the basics of the log.Interface. For more details check the documentation.
package main import ( "github.com/patrickascher/gofw/logger" "github.com/patrickascher/gofw/logger/file" ) func main() { // Register a new log with the name "access". fileLogger, err := file.New(file.Options{Filepath: "access.log"}) if err != nil { //... } err = logger.Register( // log name "access", // The log should only log messages from the level WARNING and higher. // If the LogLevel is empty, it will start logging from TRACE. logger.Config{Writer: fileLogger, LogLevel: logger.WARNING}, // Here is an example that everything should be logged in a file except CRITICAL, those should be emailed. // Each log level can have their own log provider. // // emailLogger = email.New(email.Options{...}) // log.Config{Writer: logFile, CriticalWriter:emailLogger}, ) if err != nil { //... } // get the log log, err := logger.Get("access") if err != nil { //.. } // log messages // The first parameter is the message it self. After that an unlimited number of arguments can follow. // It depends on the log provider how this is handled. Please check the provider documentation for more details. log.Info("User %v has successfully logged in", "John Doe") //This message will not be logged because the minimum log level is WARNING. log.Warning("User xy is locked because of too many login attempts") }
Output:
Index ¶
- Constants
- Variables
- func Register(name string, c Config) error
- type Config
- type Interface
- type LogEntry
- type Logger
- func (l *Logger) Critical(msg string, args ...interface{})
- func (l *Logger) Debug(msg string, args ...interface{})
- func (l *Logger) Error(msg string, args ...interface{})
- func (l *Logger) Info(msg string, args ...interface{})
- func (l *Logger) Trace(msg string, args ...interface{})
- func (l *Logger) Warning(msg string, args ...interface{})
Examples ¶
Constants ¶
const ( TRACE level = iota + 1 DEBUG INFO WARNING ERROR CRITICAL )
Log levels
Variables ¶
var ( ErrLogLevel = errors.New("log: LogLevel is unknown %#v") ErrMandatoryWriter = errors.New("log: writer is mandatory") ErrUnknownLogger = errors.New("log: %v does not exist") )
Error messages
Functions ¶
Types ¶
type Config ¶
type Config struct { LogLevel level Writer Interface TraceWriter Interface DebugWriter Interface InfoWriter Interface WarningWriter Interface ErrorWriter Interface CriticalWriter Interface }
Config for the log instance. Writer is mandatory, all others are optional. If the LogLevel is empty, TRACE will be set as default.
type LogEntry ¶
type LogEntry struct { Level level Filename string Line int Timestamp time.Time Message string Arguments []interface{} }
LogEntry is representing the actual log message.