flg

package module
v0.0.0-...-9c24bdc Latest Latest
Warning

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

Go to latest
Published: Oct 4, 2021 License: Apache-2.0 Imports: 8 Imported by: 0

README

flg

a simple log lib wapper use zap and lumberjack

对zlog和lumberjack进行封装。希望能够结合两者的优点

toml config 基本的配置

[jackcfg]
filename="test.log"
maxsize=500
maxage=7
maxbackups=1000
localtime=true
compress=false


[zapcfgs]
[zapcfgs.1]
level="info"
isdev=true
logmod=3
servername="test"

how to use it with toml config 使用

1 create toml config file

2 import the lib,and then you can use it like this

common code

一个典型的用法。你啥也不用关心,只用关心写啥日志就行。日志文件滚动啥的都交给日志文件。

你也可以定义多个日志类型的记录。

也支持LoadFromObject的方式创建日志对象


package main

import (
	"github.com/beckbikang/flg"
	"go.uber.org/zap"
)

//you can define you log var outer
var (
	lt *zap.Logger
	gflg *flg.Logger
)

func init(){
	gflg = &flg.Logger{}

	err := gflg.LoadFromFile("test.toml")
	if err != nil{
		panic("get file faild")
	}

	lt,err = gflg.GetLogByKey("test")
	if err != nil {
		panic(err)
	}
	lt.Info("start running")
}

func main() {

	// in my case you do not need define this
	//defer lt.Sync()

	lt.Info("a test")

}

测试例子


func TestLoadFromFile(t *testing.T){
	l := &Logger{}
	err := l.LoadFromFile("test.toml")
	if err != nil {
		panic("get file faild")
	}
	lg,err := l.GetLogByKey("test")
	lg.Info("a test")

	lg.Info("abc",zap.Int("int",11))
}

func TestLoadFromObject(t *testing.T){
	var fconfig FConfig
	l := &Logger{}
	if _, err := toml.DecodeFile("test.toml", &fconfig); err != nil {
		panic(err)
	}
	err := l.LoadFromObject(&fconfig)
	if err != nil {
		panic("TestLoadFromObject faild")
	}
	lg,err := l.GetLogByKey("test")
	lg.Info("a test")
	lg.Info("abc",zap.Int("int",11))
}

Documentation

Index

Constants

View Source
const (
	//logmod 默认是 1 文件  2 stdout 4 其他
	FILE_MODE = 1 << iota
	STDOUT_MODE
	OTHER_MODE

	TIME_KEY        = "time"
	LEVEL_KEY       = "level"
	NAME_KEY        = "logger"
	CALLER_KEY      = "line"
	MESSAGE_KEY     = "data"
	STACK_TRACE_KEY = "stacktrace"
	SERVER_NAME     = "server"
)
View Source
const (
	TIMEFORMAT = "2006-01-02 15-04-05.000"
)

Variables

This section is empty.

Functions

This section is empty.

Types

type FConfig

type FConfig struct {
	Lfg LConfig `toml:"jackcfg"`

	Zfgs map[string]ZConfig `toml:"zapcfgs"`
}

type LConfig

type LConfig struct {
	Filename   string `toml:"filename"`
	MaxSize    int    `toml:"maxsize"` //MB
	MaxAge     int    `toml:"maxage"`
	MaxBackups int    `toml:"maxbackups"`
	LocalTime  bool   `toml:"localtime"`
	Compress   bool   `toml:"compress"`
}

type Logger

type Logger struct {
	// contains filtered or unexported fields
}

定义基本的日志

func (*Logger) GetLogByKey

func (l *Logger) GetLogByKey(key string) (*zap.Logger, error)

func (*Logger) LoadFromFile

func (l *Logger) LoadFromFile(filename string) error

* 从文件加载日志配置

func (*Logger) LoadFromObject

func (l *Logger) LoadFromObject(fconfig *FConfig) error

type ZConfig

type ZConfig struct {
	LogName       string `toml:"logname"`
	Timekey       string `toml:"timekey"`
	LevelKey      string `toml:"levelkey"`
	NameKey       string `toml:"namekey"`
	CallerKey     string `toml:"callerkey"`
	MessageKey    string `toml:"messagekey"`
	StacktraceKey string `toml:"stacktracekey"`

	Level      string `toml:"level"`
	IsDev      bool   `toml:"isdev"`
	LogMod     int8   `toml:"logmod"`
	ServerName string `toml:"servername"`
}

Jump to

Keyboard shortcuts

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