logger package
- logging process
---
logging -------- ----------- --------- |
---> | filter1| ---> | formatter1| -+-> | handler1| ---> console |
-------- ----------- | --------- |
| --------- |
+-> | handler2| ---> file |
--------- |
|----------------- logger -----------------------| |
| logger group
logging -------- ----------- --------- |
---> | filter2| ---> | formatter2| -+-> | handler1| ---> console |
-------- ----------- | --------- |
| --------- |
+-> | handler2| ---> file |
--------- |
|----------------- logger -----------------------| |
---
- filter
- LogLevelFilter
- filter by log level.
- LogLevelFilter
- formatter
- StandardFormatter
- standard formatter
- JSONFormatter
- json formatter
- StandardFormatter
- handler
- ConsoleHadnler
- output to console.
- color is supported.
- SyslogHadnler
- output to syslog
- RotationFileHandler
- output to file.
- rotation is supported.
- ConsoleHadnler
belog.Emerg("test\n")
belog.Alert("test\n")
belog.Crit("test\n")
belog.Error("test\n")
belog.Warn("test\n")
belog.Notice("test\n")
belog.Info("test\n")
belog.Debug("test\n")
belog.Trace("test\n")
filter := belog.NewLogLevelFilter()
filter.SetLogLevel(belog.LogLevelTrace)
err := belog.ChangeFilter(filter)
if err != nil {
fmt.Println(err)
}
formatter := belog.NewStandardFormatter()
formatter.SetDateTimeLayout("2006-01-02 15:04:05 -0700 MST")
formatter.SetLayout("%(dateTime) [%(logLevel):%(logLevelNum)] (%(pid)) %(programCounter) %(loggerName) %(fileName) %(shortFileName) %(lineNum) %(message)")
err := belog.ChangeFormatter(formatter)
if err != nil {
fmt.Println(err)
}
handler := belog.NewRotationFileHandler()
handler.SetLogFileName("belog-test.log")
handler.SetLogDirPath("/var/tmp/belog-test")
handler.SetMaxAge(2)
handler.SetMaxSize(65535)
handler.SetAsync(true)
handler.SetAsyncFlushInterval(3)
handler.SetBufferSize(2048)
handlers := make([]belog.Handler, 0)
handlers = append(handlers, handler1)
err := belog.ChangeHandlers(handlers)
if err != nil {
fmt.Println(err)
}
- You can use logger of your favorit setting.
- It is requred, if you need multiple logger.
- Logger name 'default' is reserved by defaultLogger.
func init() {
// create filter
filter := belog.NewLogLevelFilter()
filter.SetLogLevel(belog.LogLevelTrace)
// create formatter
formatter := belog.NewStandardFormatter()
formatter.SetDateTimeLayout("2006-01-02 15:04:05 -0700 MST")
formatter.SetLayout("%(dateTime) [%(logLevel):%(logLevelNum)] (%(pid)) %(programCounter) %(loggerName) %(fileName) %(shortFileName) %(lineNum) %(message)")
// create handler
handler := belog.NewRotationFileHandler()
handler.SetLogFileName("belog-test.log")
handler.SetLogDirPath("/var/tmp/belog-test")
handler.SetMaxAge(10)
handler.SetMaxSize(1024 * 1024 * 1024)
// add handlers
handlers := make([]belog.Handler, 0)
handlers = append(handlers, handler1)
handlers = append(handlers, handler2)
// set logger
belog.SetLogger("mylogger1", filter, formatter, handlers)
}
- You can get mutiple logger.
- If you get logger name is not exists, this return default togger.
func init() {
logger := belog.GetLoggerGroup("mylogger1", "mylogger2")
logger.Info("test")
}
- Loadable config format are toml or yaml of json.
- See samples of test directory.
- Logger name 'default' is reserved by defaultLogger.
--- sample.yaml ---
loggers:
mylogger:
filter:
structname: LogLevelFilter
structsetters: []
formatter:
structname: StandardFormatter
structsetters:
- settername: SetDateTimeLayout
setterparams:
- 2006-01-02 15:04:05 -0700 MST
- settername: SetLayout
setterparams:
- '%(dateTime) [%(logLevel)] (%(pid)) %(programCounter) %(loggerName) %(fileName) %(lineNum) %(message)'
handlers:
- structname: ConsoleHandler
structsetters: []
- structname: RotationFileHandler
structsetters: []
func init() {
if err := belog.LoadConfig("sample.yaml"); err != nil {
fmt.Println(err)
}
}
type my struct {
Alice *Person
Logger *belog.ConfigLoggers
}
func init() {
...
if err := SetupLoggers(my.Logger); err != nil {
fmt.Println(err)
}
}
- Your filter struct have to method of filter interface.
type Filter interface {
Evaluate(loggerName string, log LogEvent) bool
}
- You must create function, it must create filter struct and must return pointer of filter struct.
func NewLogLevelFilter() (logLevelFilter *LogLevelFilter) {
return &LogLevelFilter{
logLevel: LogLevelInfo,
mutex: new(sync.RWMutex),
}
}
- Finally, you must register filter in init function.
- First argument of RegisterFilter function is your filter name.
- This process is requied,if you setup logger from config or from configLogger object.
func init() {
belog.RegisterFilter("LogLevelFilter", func() (filter belog.Filter) {
return NewLogLevelFilter()
})
}
- Your formatter struct have to method of formatter interface.
type Formatter interface {
Format(loggerName string, log LogEvent) (formattedLog string, err error)
}
- You must create function, it must create formatter struct and must return pointer of formatter struct.
func NewStandardFormatter() (standardFormatter *StandardFormatter) {
return &StandardFormatter{
dateTimeLayout: "2006-01-02 15:04:05",
layout: "%(dateTime) [%(logLevel)] (%(pid)) %(loggerName) %(fileName) %(lineNum) %(message)",
mutex: new(sync.RWMutex),
}
}
- Finally, you must register formatter in init function.
- First argument of RegisterFormatter function is your formatter name.
- This process is requied, if you setup logger from config or from configLogger object.
func init() {
belog.RegisterFormatter("StandardFormatter", func() (formatter belog.Formatter) {
return NewStandardFormatter()
})
}
- Your handler struct have to method of handler interface.
type Handler interface {
IsOpened() (bool)
Open()
Write(loggerName string, logEvent LogEvent, formattedLog string)
Flush()
Close()
}
- You must create function, it must create handler struct and must return pointer of handler struct.
func NewConsoleHandler() (consoleHandler *ConsoleHandler) {
return &ConsoleHandler{
outputType: OutputTypeStdout,
mutex: new(sync.RWMutex),
}
}
- Finally, you must register handler in init function.
- First argument of RegisterHandler function is your handler name.
- This process is requied,if you setup logger from config or from configLogger object.
func init() {
belog.RegisterHandler("ConsoleHandler", func() (handler belog.Handler) {
return NewConsoleHandler()
})
}
- LogEvent interface
type LogEvent interface {
Program() (program string)
Pid() (pid int)
Hostname() (hostname string)
Time() (time time.Time)
LogLevel() (logLevel string)
LogLevelNum() (logLevelNum LogLevel)
Pc() (pc uintptr)
FileName() (fileName string)
LineNum() (lineNum int)
Message() (message string)
SetAttr(key string, value interface{})
GetAttr(key string) (value interface{})
GetAttrs() map[string]interface{}
}