slog

package module
v1.4.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jun 27, 2023 License: Apache-2.0 Imports: 15 Imported by: 23

README

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

Documentation

Index

Constants

View Source
const (
	RollingPolicySize  = "size"
	RollingPolicyDaily = "daily"
	LogRotateDate      = 1
	LogRotateSize      = 10
	LogBackupCount     = 7
)

constant values for logrotate parameters

View Source
const (
	//DEBUG is a constant of string type
	DEBUG = "DEBUG"
	INFO  = "INFO"
	WARN  = "WARN"
	ERROR = "ERROR"
	FATAL = "FATAL"
)

Variables

This section is empty.

Functions

func CopyFile

func CopyFile(srcFile, destFile string) error

CopyFile copy file

func Debug

func Debug(action string, data ...lager.Data)

func Debugf

func Debugf(format string, args ...interface{})

func Error

func Error(action string, err error, data ...lager.Data)

func Errorf

func Errorf(err error, format string, args ...interface{})

func EscapPath

func EscapPath(msg string) string

EscapPath escape path

func Fatal

func Fatal(action string, err error, data ...lager.Data)

func Fatalf

func Fatalf(err error, format string, args ...interface{})

func FilterFileList

func FilterFileList(path, pat string) ([]string, error)

FilterFileList function for filter file list path : where the file will be filtered pat : regexp pattern to filter the matched file

func Info

func Info(action string, data ...lager.Data)

func Infof

func Infof(format string, args ...interface{})

func InitWithConfig

func InitWithConfig(passLagerDef *PassLagerCfg) error

InitWithConfig

use pass lager configuration

func InitWithFile

func InitWithFile(lagerFile string) error

InitWithFile

readPassLagerConfigFile is unmarshal the paas lager configuration file(log.yaml)

func LogRotate

func LogRotate(path string, MaxFileSize int, MaxBackupCount int)

LogRotate function for log rotate path: where log files need rollover MaxFileSize: MaxSize of a file before rotate. By M Bytes. MaxBackupCount: Max counts to keep of a log's backup files.

func NewLoggerExt

func NewLoggerExt(component string, appGUID string, isLogFormatText, isHideLineno bool) lager.Logger

NewLoggerExt is a function which is used to write new logs

func RegisterWriter

func RegisterWriter(name string, writer io.Writer)

RegisterWriter is used to register a io writer

func Warn

func Warn(action string, data ...lager.Data)

func Warnf

func Warnf(format string, args ...interface{})

Types

type Config

type Config struct {
	LoggerLevel    string
	LoggerFile     string
	Writers        []string
	EnableRsyslog  bool
	RsyslogNetwork string
	RsyslogAddr    string

	LogFormatText bool
	LogHideLineno bool
}

Config

is a struct which stores details for maintaining logs

type Lager

type Lager struct {
	Writers        string `yaml:"writers"`
	LoggerLevel    string `yaml:"logger_level"`
	LoggerFile     string `yaml:"logger_file"`
	LogHideLineno  bool   `yaml:"log_hide_lineno"`
	LogFormatText  bool   `yaml:"log_format_text"`
	RollingPolicy  string `yaml:"rolling_policy"`
	LogRotateDate  int    `yaml:"log_rotate_date"`
	LogRotateSize  int    `yaml:"log_rotate_size"`
	LogBackupCount int    `yaml:"log_backup_count"`
}

Lager struct for logger parameters

type PassLagerCfg

type PassLagerCfg struct {
	Writers        string `yaml:"writers"`
	LoggerLevel    string `yaml:"logger_level"`
	LoggerFile     string `yaml:"logger_file"`
	LogHideLineno  bool   `yaml:"log_hide_lineno"`
	LogFormatText  bool   `yaml:"log_format_text"`
	RollingPolicy  string `yaml:"rolling_policy"`
	LogRotateDate  int    `yaml:"log_rotate_date"`
	LogRotateSize  int    `yaml:"log_rotate_size"`
	LogBackupCount int    `yaml:"log_backup_count"`
}

PassLagerCfg is the struct for lager information(passlager.yaml)

var PassLagerDefinition *PassLagerCfg = DefaultLagerDefinition()

PassLagerDefinition

is having the information about logging

func DefaultLagerDefinition

func DefaultLagerDefinition() *PassLagerCfg

DefaultLagerDefinition

use default lager definition

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL