Documentation ¶
Overview ¶
Package log provides an improved logger
Example (Logger) ¶
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") // AUX message it's unskippable message which will be printed to log file with // any minimum level logger.Aux("This is aux message") // 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 ...interface{}) error
- func Crit(f string, a ...interface{}) error
- func Debug(f string, a ...interface{}) error
- func EnableBufIO(interval time.Duration)
- func Error(f string, a ...interface{}) error
- func Flush() error
- func Info(f string, a ...interface{}) error
- func MinLevel(level interface{}) error
- func Print(level uint8, f string, a ...interface{}) error
- func Reopen() error
- func Set(file string, perms os.FileMode) error
- func Warn(f string, a ...interface{}) error
- type Logger
- func (l *Logger) Aux(f string, a ...interface{}) error
- func (l *Logger) Crit(f string, a ...interface{}) error
- func (l *Logger) Debug(f string, a ...interface{}) error
- func (l *Logger) EnableBufIO(interval time.Duration)
- func (l *Logger) Error(f string, a ...interface{}) error
- func (l *Logger) Flush() error
- func (l *Logger) Info(f string, a ...interface{}) error
- func (l *Logger) MinLevel(level interface{}) error
- func (l *Logger) Print(level uint8, f string, a ...interface{}) error
- func (l *Logger) Reopen() error
- func (l *Logger) Set(file string, perms os.FileMode) error
- func (l *Logger) Warn(f string, a ...interface{}) error
- type StdLogger
- func (l *StdLogger) Fatal(v ...interface{})
- func (l *StdLogger) Fatalf(format string, v ...interface{})
- func (l *StdLogger) Fatalln(v ...interface{})
- func (l *StdLogger) Output(calldepth int, s string) error
- func (l *StdLogger) Panic(v ...interface{})
- func (l *StdLogger) Panicf(format string, v ...interface{})
- func (l *StdLogger) Panicln(v ...interface{})
- func (l *StdLogger) Print(v ...interface{})
- func (l *StdLogger) Printf(format string, v ...interface{})
- func (l *StdLogger) Println(v ...interface{})
Examples ¶
Constants ¶
Variables ¶
View Source
var ( // ErrLoggerIsNil is returned by Logger struct methods if struct is nil ErrLoggerIsNil = errors.New("Logger is nil or not created properly") // 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
View Source
var Colors = map[uint8]string{ DEBUG: "{s-}", INFO: "", WARN: "{y}", ERROR: "{r}", CRIT: "{m}", }
Colors colors is map with fmtc color tags for every level
View Source
var Global = &Logger{ PrefixWarn: true, PrefixError: true, PrefixCrit: true, minLevel: INFO, mu: &sync.Mutex{}, }
Global is global logger struct
View Source
var PrefixMap = map[uint8]string{ DEBUG: "[DEBUG]", INFO: "[INFO]", WARN: "[WARNING]", ERROR: "[ERROR]", CRIT: "[CRITICAL]", }
PrefixMap is map with messages prefixes
View Source
var TimeFormat = "2006/01/02 15:04:05.000"
TimeFormat contains format string for time in logs
Functions ¶
Types ¶
type Logger ¶
type Logger struct { PrefixDebug bool // Prefix for debug messages PrefixInfo bool // Prefix for info messages PrefixWarn bool // Prefix for warning messages PrefixError bool // Prefix for error messages PrefixCrit bool // Prefix for critical error messages UseColors bool // Enable ANSI escape codes for colors in output // contains filtered or unexported fields }
Logger is a basic logger struct
func (*Logger) EnableBufIO ¶
EnableBufIO enables buffered I/O support
type StdLogger ¶
type StdLogger struct {
Logger *Logger
}
StdLogger is logger wrapper compatible with stdlib logger
func (*StdLogger) Fatal ¶
func (l *StdLogger) Fatal(v ...interface{})
Fatal is analog of Fatal from stdlib
func (*StdLogger) Fatalln ¶
func (l *StdLogger) Fatalln(v ...interface{})
Fatalln is analog of Fatalln from stdlib
func (*StdLogger) Panic ¶
func (l *StdLogger) Panic(v ...interface{})
Panic is analog of Panic from stdlib
func (*StdLogger) Panicln ¶
func (l *StdLogger) Panicln(v ...interface{})
Panicln is analog of Panicln from stdlib
func (*StdLogger) Print ¶
func (l *StdLogger) Print(v ...interface{})
Print is analog of Print from stdlib
Click to show internal directories.
Click to hide internal directories.