README ¶
Loki
Inspired by a popular NodeJS logger library: debug.
Feature
- Out of the box
- Colorful console output
- Enable logger by environment easily
Difference with other logger library
Sometimes, it's not enough to split loggers by different log level.
With Loki, you can inject more detailed logs in your code, and enable them by environment variable whenever you want, which makes debug more conveniently.
Install
go get github.com/joway/loki@latest
API
Usage
Use root logger
// default level is INFO
loki.SetLevel(loki.DEBUG)
loki.Info("x: %s", "hi")
loki.Debug("x: %s", "hi")
loki.Error("x: %s", "hi")
Create your logger
logger := loki.New("app:moduleName")
logger.Info("x: %s", "hi")
logger.Debug("x: %s", "hi")
logger.Error("x: %s", "hi")
To enable this logger, add env LOKI_ENV=app:moduleName
in your startup command, like: LOKI_ENV=app:moduleName ./main
.
You can also use LOKI_ENV=app:a,app:b,model:a
to enable multiple loggers.
LOKI_ENV=*
can enable all loggers.
Use file handler
// "02 Jan 06 15:04 MST"
l := loki.New("app:xxx")
fp, err := os.OpenFile("test.log", os.O_WRONLY|os.O_CREATE|os.O_TRUNC, os.ModePerm)
defer fp.Close()
flushIntervalMs := 1000
l.SetHandler(loki.NewFileHandler(fp, flushIntervalMs))
l.Info("x: %s", "hi")
Change time format
// "02 Jan 06 15:04 MST"
loki.SetTimeFormatter(time.RFC822)
// disable time output
loki.SetTimeFormatter("")
Use your custom logger formatter
type ErrFormatter struct {
loki.Formatter
}
func (f ErrFormatter) format(a ...interface{}) string {
err := a[0].(error)
return fmt.Sprintf("Error %v", err)
}
logger := loki.New("app:xxx")
f := ErrFormatter{}
logger.SetFormatter(f)
logger.Debug(errors.New("test error"))
Documentation ¶
Index ¶
- Variables
- func Debug(a ...interface{})
- func Error(a ...interface{})
- func Fatal(a ...interface{})
- func Info(a ...interface{})
- func SetFormatter(formatter Formatter)
- func SetHandler(handler Handler)
- func SetLevel(level int)
- func SetTimeFormatter(format string)
- func Warn(a ...interface{})
- type ConsoleHandler
- type FileHandler
- type Formatter
- type Handler
- type Logger
- func (l Logger) Check() bool
- func (l Logger) Compile(a ...interface{}) string
- func (l Logger) Debug(a ...interface{})
- func (l Logger) Error(a ...interface{})
- func (l Logger) Fatal(a ...interface{})
- func (l Logger) Info(a ...interface{})
- func (l *Logger) SetFormatter(formatter Formatter)
- func (l *Logger) SetHandler(handler Handler)
- func (l *Logger) SetLevel(level int)
- func (l *Logger) SetLogEnv(env string)
- func (l *Logger) SetTimeFormatter(format string)
- func (l Logger) Warn(a ...interface{})
- type StandardFormatter
Constants ¶
This section is empty.
Variables ¶
var ( // DEBUG level of DEBUG DEBUG = 1 // INFO level of DEBUG INFO = 2 // WARN level of DEBUG WARN = 3 // ERROR level of DEBUG ERROR = 4 )
Functions ¶
func SetTimeFormatter ¶ added in v0.2.2
func SetTimeFormatter(format string)
SetTimeFormatter set the time format string of logger
Types ¶
type FileHandler ¶
type FileHandler struct { Handler // contains filtered or unexported fields }
FileHandler output logs to console
type Formatter ¶
type Formatter interface {
// contains filtered or unexported methods
}
Formatter format the message
type Handler ¶
type Handler interface {
// contains filtered or unexported methods
}
Handler handle the output process
func NewConsoleHandler ¶
func NewConsoleHandler() Handler
NewConsoleHandler return ConsoleHandler instance
type Logger ¶
type Logger struct {
// contains filtered or unexported fields
}
Logger is a instance to handle logs
func (Logger) Fatal ¶
func (l Logger) Fatal(a ...interface{})
Fatal output level ERROR log and exit with code 1
func (*Logger) SetFormatter ¶
SetFormatter set the formatter of logger
func (*Logger) SetHandler ¶
SetHandler set the handler of logger
func (*Logger) SetTimeFormatter ¶
SetTimeFormatter set the time format string of logger
type StandardFormatter ¶
type StandardFormatter struct {
Formatter
}
StandardFormatter default formatter