bar-counter / slog

golang sample log support rotate

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

ci go mod version GoDoc GoReportCard codecov github release

for what

  • this project used to golang log management

Contributing

Contributor Covenant GitHub contributors

We welcome community contributions to this project.

Please read Contributor Guide for more information on how to get started.

depends

in go mod project

# warning use privte git host must set
# global set for once
# add private git host like github.com to evn GOPRIVATE
$ go env -w GOPRIVATE='github.com'
# use ssh proxy
# set ssh-key to use ssh as http
$ git config --global url."git@github.com:".insteadOf "http://github.com/"
# or use PRIVATE-TOKEN
# set PRIVATE-TOKEN as gitlab or gitea
$ git config --global http.extraheader "PRIVATE-TOKEN: {PRIVATE-TOKEN}"
# set this rep to download ssh as https use PRIVATE-TOKEN
$ git config --global url."ssh://github.com/".insteadOf "https://github.com/"

# before above global settings
# test version info
$ git ls-remote -q http://github.com/bar-counter/slog.git

# test depends see full version
$ go list -mod readonly -v -m -versions github.com/bar-counter/slog
# or use last version add go.mod by script
$ echo "go mod edit -require=$(go list -mod=readonly -m -versions github.com/bar-counter/slog | awk '{print $1 "@" $NF}')"
$ echo "go mod vendor"

evn

  • golang sdk 1.17+

Features

  • easy API to use, slog.Debug("this is debug")...
  • easy config new slog.DefaultLagerDefinition()
  • config load by yaml file
  • support stdout and file
  • color stdout support
  • show/hide code line number
  • format json/stdout
  • rolling policy at file output
    • log_rotate_date: max 10 days, greater than will change to 1, rotate date, coordinate log_rotate_date: daily
    • log_rotate_size: max 64M, greater than will change to 10, rotate size,coordinate rollingPolicy: size
    • log_backup_count: max 100 files, greater than will change to 7, log system will compress the log file when log reaches rotate set, this set is max file count
  • more perfect test case coverage
  • more perfect benchmark case

usage

  • use slog.DefaultLagerDefinition()
package main

import (
	"fmt"
	"github.com/bar-counter/slog"
	"testing"
)

func TestMainLog(t *testing.T) {
	lagerDefinition := slog.DefaultLagerDefinition()
	err := slog.InitWithConfig(lagerDefinition)
	if err != nil {
		t.Fatal(err)
	}

	slog.Debug("this is debug")
	slog.Infof("this is info %v", "some info")
	slog.Warn("this is warn")
	slog.Error("this is error", fmt.Errorf("some error"))
}
  • load with *.yaml
writers: stdout # file,stdout.`file` will let `logger_file` to file,`stdout` will show at std, most of the time use bose
logger_level: DEBUG # DEBUG INFO WARN ERROR FATAL
logger_file: logs/foo.log # "" is not writer log file, and this will cover by env: CHASSIS_HOME
log_hide_lineno: false # `true` will hide code line number, `false` will show code line number, default is false
log_format_text: false # format_text `false` will format json, `true` will out stdout
rolling_policy: size # rotate policy, can choose as: daily, size. `daily` store as daily,`size` will save as max
log_rotate_date: 1 # max 10 days, greater than will change to 1, rotate date, coordinate `log_rotate_date: daily`
log_rotate_size: 8 # max 64M, greater than will change to 10, rotate size,coordinate `rollingPolicy: size`
log_backup_count: 7 # max 100 files, greater than will change to 7, log system will compress the log file when log reaches rotate set, this set is max file count
  • use slog.InitWithFile("log.yaml")
package main

import (
	"fmt"
	"github.com/bar-counter/slog"
)

func main() {
	err := slog.InitWithFile("log.yaml")
	if err != nil {
		panic(err)
	}

	slog.Debug("this is debug")
	slog.Infof("this is info %v", "some info")
	slog.Warn("this is warn")
	slog.Error("this is error", fmt.Errorf("some error"))
}

dev

make init dep
  • test code
make test

add main.go file and run

# run at env dev
make dev

# run at env ordinary
make run
  • ci to fast check
make ci

docker

# then test build as test/Dockerfile
$ make dockerTestRestartLatest
# clean test build
$ make dockerTestPruneLatest

# more info see
$ make helpDocker

use

  • use to replace bar-counter/slog to you code

About

golang sample log support rotate

License:Apache License 2.0


Languages

Language:Go 94.4%Language:Makefile 4.6%Language:Dockerfile 1.0%