mirror of
https://github.com/khorevaa/logos.git
synced 2025-01-22 05:10:53 +02:00
Logos: like log4j, but for golang.
Features
This project is a wrapper around the excellent logging framework zap.
- Dependency
go.uber.org/zap
for logginggithub.com/elastic/go-ucfg
for config logging system
- Simple and Clean Interface
- One log manager for all logs
- Hot config update from file or env
- Appenders
Console
, write to consoleFile
, any log filegelfupd
, greylog loggerRollingFile
, rolling file writing & compress
- Encoders
Console
, colorful & formatting text for consoleGelf
, gelf for greylogJson
, standard json encoder
- Useful utility function
Setlevel(LogName string, level int)
, hot update logger levelUpdateLogger(LogName string, logger *zap.Logger*)
, hot update core loggerRedirectStdLog()
, redirect standard log package
- High Performance
- Significantly faster json loggers.
How to use
Quick start
package main
import (
"github.com/khorevaa/logos"
)
func main() {
log := logos.New("<your-package-name>") // like github.com/khorevaa/logos
log.Info("This is me first log. Hello world logging systems")
}
Json Writer
To log a machine-friendly, use json
.
package main
import (
"errors"
"github.com/khorevaa/logos"
)
func main() {
rawConfig := `
appenders:
console:
- name: CONSOLE
target: stdout
encoder:
json:
loggerConfigs:
root:
level: info
appender_refs:
- CONSOLE
`
logos.InitWithConfigContent(rawConfig)
log := logos.New("<your-package-name>") // like github.com/khorevaa/logos
log.Info("This is me first log. Hello world logging systems")
}
Pretty Console Writer
To log a human-friendly, colorized output, use Console
.
package main
import (
"errors"
"github.com/khorevaa/logos"
)
func main() {
rawConfig := `
appenders:
console:
- name: CONSOLE
target: stdout
encoder:
console:
color_scheme:
info_level: blue+b
debug_level: green+b
loggerConfigs:
root:
level: debug
appender_refs:
- CONSOLE
`
logos.InitWithConfigContent(rawConfig)
log := logos.New("<your-package-name>") // like github.com/khorevaa/logos
log.Info("This is me first log. Hello world logging systems")
err := errors.New("log system error")
log.Debug("This is me first error", logos.Any("err", err))
}
Note: pretty logging also works on windows console
High Performance
A quick and simple benchmark with zap/zerolog, which runs on github actions:
// go test -v -cpu=4 -run=none -bench=. -benchtime=10s -benchmem log_test.go
package main
import (
"io/ioutil"
"testing"
"github.com/khorevaa/logos"
"github.com/phuslu/log"
"github.com/rs/zerolog"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
)
var fakeMessage = "Test logging, but use a somewhat realistic message length. "
func BenchmarkLogos(b *testing.B) {
const newConfig = `
appenders:
console:
- name: CONSOLE
target: discard
encoder:
console:
loggers:
root:
level: info
appender_refs:
- CONSOLE
`
err := logos.InitWithConfigContent(newConfig)
if err != nil {
panic(err)
}
logger := logos.New("benchmark")
for i := 0; i < b.N; i++ {
logger.Info(fakeMessage, zap.String("foo", "bar"), zap.Int("int", 42))
}
}
func BenchmarkZap(b *testing.B) {
logger := zap.New(zapcore.NewCore(
zapcore.NewJSONEncoder(zap.NewProductionEncoderConfig()),
zapcore.AddSync(ioutil.Discard),
zapcore.InfoLevel,
))
for i := 0; i < b.N; i++ {
logger.Info(fakeMessage, zap.String("foo", "bar"), zap.Int("int", 42))
}
}
func BenchmarkZeroLog(b *testing.B) {
logger := zerolog.New(ioutil.Discard).With().Timestamp().Logger()
for i := 0; i < b.N; i++ {
logger.Info().Str("foo", "bar").Int("int", 42).Msg(fakeMessage)
}
}
func BenchmarkPhusLog(b *testing.B) {
logger := log.Logger{
TimeFormat: "", // uses rfc3339 by default
Writer: log.IOWriter{ioutil.Discard},
}
for i := 0; i < b.N; i++ {
logger.Info().Str("foo", "bar").Int("int", 42).Msg(fakeMessage)
}
}
A Performance result as below, for daily benchmark results see github actions
Languages
Go
100%