1
0
mirror of https://github.com/khorevaa/logos.git synced 2025-01-22 05:10:53 +02:00
2021-02-01 19:28:39 +03:00
2021-02-01 18:37:03 +03:00
2021-02-01 17:50:15 +03:00
2021-02-01 17:50:15 +03:00
2021-02-01 19:28:39 +03:00
2021-02-01 18:44:38 +03:00
2021-02-01 18:31:19 +03:00
2021-02-01 17:50:15 +03:00
2021-02-01 17:50:15 +03:00
2021-02-01 18:35:21 +03:00
2021-02-01 17:50:15 +03:00
2021-02-01 19:23:08 +03:00
2021-02-01 18:07:57 +03:00
2021-02-01 17:50:15 +03:00
2021-02-01 17:50:15 +03:00
2021-02-01 17:50:15 +03:00
2021-02-01 17:50:15 +03:00
2021-02-01 17:50:15 +03:00
2021-02-01 17:50:15 +03:00
2021-02-01 19:23:08 +03:00
2021-02-01 19:19:18 +03:00
2021-02-01 19:19:18 +03:00
2021-02-01 18:37:03 +03:00
2021-02-01 17:50:15 +03:00
2021-02-01 17:50:15 +03:00

Logos: like log4j, but for golang.

go.dev goreport build coverage stability-stable

Features

This project is a wrapper around the excellent logging framework zap.

  • Dependency
    • go.uber.org/zap for logging
    • github.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 console
    • File, any log file
    • gelfupd, greylog logger
    • RollingFile, rolling file writing & compress
  • Encoders
    • Console, colorful & formatting text for console
    • Gelf, gelf for greylog
    • Json, standard json encoder
  • Useful utility function
    • Setlevel(LogName string, level int), hot update logger level
    • UpdateLogger(LogName string, logger *zap.Logger*), hot update core logger
    • RedirectStdLog(), redirect standard log package
  • High Performance

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))

}

img.png

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


Description
No description provided
Readme MIT 132 KiB
Languages
Go 100%