logger

package module
v0.0.2 Latest Latest
Warning

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

Go to latest
Published: Apr 13, 2023 License: Apache-2.0 Imports: 12 Imported by: 39

README

logger

convenient log package

1. 使用说明

    // 配置logger,如果不配置时默认为控制台输出,等级为DEBG
    logger.SetLogger(`{"Console": {"level": "DEBG"}`)
    // 配置说明见下文

    // 设置完成后,即可在控制台和日志文件app.log中看到如下输出
    logger.Trace("this is Trace")
    logger.Debug("this is Debug")
    logger.Info("this is Info")
    logger.Warn("this is Warn")
    logger.Error("this is Error")
    logger.Crit("this is Critical")
    logger.Alert("this is Alert")
    logger.Emer("this is Emergency")

2. 日志等级

当前日志输出等级共8种,从0-7对应的等级由高到底,当配置为某个输出等级时,只有大于等于该等级的日志才会输出。不同的输出适配器支持不同的日志等级配置:

等级 配置 释义 控制台颜色
6 EMER 系统级紧急,比如磁盘出错,内存异常,网络不可用等 红色底
5 ALRT 系统级警告,比如数据库访问异常,配置文件出错等 紫色
4 EROR 用户级错误 红色
3 WARN 用户级警告 黄色
2 INFO 用户级重要 天蓝色
1 DEBG 用户级调试 绿色
0 TRAC 用户级基本输出 绿色

3. 配置说明

logger当前支持控制台、文件、网络3种方式适配器输出,可以通过各自的参数进行设置,该logger支持多个方式同时输出,如果未配置某项适配器时,则不初始化也不会输出到该适配器。

通过调用logger.SetLogger(config string)方法设置参数,config支持json配置,也支持指定内容为json配置的文件路径,例如:

    // 通过配置参数直接配置
    logger.SetLogger(`{"Console": {"level": "DEBG"}}`)
    // 通过配置文件配置
    logger.SetLogger("/home/log.json")

{
    "TimeFormat":"2006-01-02 15:04:05", // 输出日志开头时间格式
    "Console": {            // 控制台日志配置
        "level": "TRAC",    // 控制台日志输出等级
        "color": true       // 控制台日志颜色开关 
    },
    "File": {                   // 文件日志配置
        "filename": "app.log",  // 初始日志文件名
        "level": "TRAC",        // 日志文件日志输出等级
        "daily": true,          // 跨天后是否创建新日志文件,当append=true时有效
        "maxlines": 1000000,    // 日志文件最大行数,当append=true时有效
        "maxsize": 1,           // 日志文件最大大小,当append=true时有效
        "maxdays": -1,          // 日志文件有效期
        "append": true,         // 是否支持日志追加
        "permit": "0660"        // 新创建的日志文件权限属性
    },
    "Conn": {                       // 网络日志配置
        "net":"tcp",                // 日志传输模式
        "addr":"10.1.55.10:1024",   // 日志接收服务器
        "level": "Warn",            // 网络日志输出等级
        "reconnect":true,           // 网络断开后是否重连
        "reconnectOnMsg":false,     // 发送完每条消息后是否断开网络
    }
}
  • 时间格式
时间类型 时间格式
ANSIC "Mon Jan _2 15:04:05 2006"
UnixDate "Mon Jan _2 15:04:05 MST 2006"
RubyDate "Mon Jan 02 15:04:05 -0700 2006"
RFC822 "02 Jan 06 15:04 MST"
RFC822Z "02 Jan 06 15:04 -0700"
RFC850 "Monday, 02-Jan-06 15:04:05 MST"
RFC1123 "Mon, 02 Jan 2006 15:04:05 MST"
RFC1123Z "Mon, 02 Jan 2006 15:04:05 -0700"
RFC3339 "2006-01-02T15:04:05Z07:00"
RFC3339Nano "2006-01-02T15:04:05.999999999Z07:00"
Kitchen "3:04PM"
Stamp "Jan _2 15:04:05"
StampMilli "Jan _2 15:04:05.000"
StampMicro "Jan _2 15:04:05.000000"
StampNano "Jan _2 15:04:05.000000000"
RFC3339Nano1 "2006-01-02 15:04:05.999999999 -0700 MST"
DEFAULT "2006-01-02 15:04:05"
  • 时间格式打印:
========RFC1123Z time format========
Thu, 02 Aug 2018 18:48:04 +0800 [DEBG] [github.com/wonderivan/logger/log_test.go:115] Debug RFC1123Z
========Stamp time format========
Aug  2 18:48:04 [DEBG] [github.com/wonderivan/logger/log_test.go:115] Debug Stamp
========StampMilli time format========
Aug  2 18:48:04.489 [DEBG] [github.com/wonderivan/logger/log_test.go:115] Debug StampMilli
========StampNano time format========
Aug  2 18:48:04.490002155 [DEBG] [github.com/wonderivan/logger/log_test.go:115] Debug StampNano
========RubyDate time format========
Thu Aug 02 18:48:04 +0800 2018 [DEBG] [github.com/wonderivan/logger/log_test.go:115] Debug RubyDate
========RFC822 time format========
02 Aug 18 18:48 CST [DEBG] [github.com/wonderivan/logger/log_test.go:115] Debug RFC822
========RFC822Z time format========
02 Aug 18 18:48 +0800 [DEBG] [github.com/wonderivan/logger/log_test.go:115] Debug RFC822Z
========RFC1123 time format========
Thu, 02 Aug 2018 18:48:04 CST [DEBG] [github.com/wonderivan/logger/log_test.go:115] Debug RFC1123
========RFC3339 time format========
2018-08-02T18:48:04+08:00 [DEBG] [github.com/wonderivan/logger/log_test.go:115] Debug RFC3339
========RFC3339Nano time format========
2018-08-02T18:48:04.490377325+08:00 [DEBG] [github.com/wonderivan/logger/log_test.go:115] Debug RFC3339Nano
========ANSIC time format========
Thu Aug  2 18:48:04 2018 [DEBG] [github.com/wonderivan/logger/log_test.go:115] Debug ANSIC
========UnixDate time format========
Thu Aug  2 18:48:04 CST 2018 [DEBG] [github.com/wonderivan/logger/log_test.go:115] Debug UnixDate
========RFC850 time format========
Thursday, 02-Aug-18 18:48:04 CST [DEBG] [github.com/wonderivan/logger/log_test.go:115] Debug RFC850
========Kitchen time format========
6:48PM [DEBG] [github.com/wonderivan/logger/log_test.go:115] Debug Kitchen
========StampMicro time format========
Aug  2 18:48:04.490662 [DEBG] [github.com/wonderivan/logger/log_test.go:115] Debug StampMicro

4. 其他

  1. logger默认是控制台输出,输出等级为DEBG,默认是支持颜色区分的。
  2. 日志文件append为true时,当写入的日志文件发生跨天(daily为true)或超过最大限制时,会创建一个新文件,原有文件格式被重命名为: ****.xxxx-xx-xx.xxx.xxx 格式,例如:当向app.log写入日志时,触发了创建新文件操作,则将app.log重命名为 app.2018-01-01.001.log, 如果此时app.2018-01-01.001.log已经存在,则将刚才的app.log重命名为 app.2018-01-01.002.log,以此类推。
  3. logger package默认初始化了全局的defaultLogger,直接调用logger包的Debug方法时,会默认调用defaultLogger.Debug,所以普通调用时,仅需要import logger即可使用。
  4. 网络配置中的reconnectOnMsg为每条消息都重连一次网络日志中心,适用于写日志频率极低的情况下的服务调用,避免长时间连接,占用资源。但强烈不建议普通使用时设置为true,这将会导致调用方反复的网络重连,极大增加资源消耗和延迟。
  5. conn网络输出适配器经过ELK集成环境的测试验证,通过该方式发送的日志,能够正常通过Elecsearch和Kibana检索和分析

Documentation

Index

Constants

View Source
const (
	LevelDebug Level = 0 // 用户级调试
	LevelTrace       = 1 // 用户级基本输出
	LevelAlert       = 2
	LevelError       = 7 // 用户级错误
	LevelPanic       = 8 //Panic
	LevelFATAL       = 9 //PANIC
)

日志等级,从0-7,日优先级由高到低

View Source
const DefaultConsoleName = "_defaultConsoleName"

Variables

This section is empty.

Functions

func Alert

func Alert(f any, v ...any)

func Debug

func Debug(f any, v ...any)

func Error

func Error(f any, v ...any)

func Fatal

func Fatal(f any, v ...any)

func Panic added in v0.0.2

func Panic(f any, v ...any)

func SetCallDepth added in v0.0.2

func SetCallDepth(depth int)

func SetLevel added in v0.0.2

func SetLevel(level Level)

SetLevel 设置日志输出等级

func SetOutput added in v0.0.2

func SetOutput(name string, output Output) error

func SetPathTrim added in v0.0.2

func SetPathTrim(trimPath string)

SetPathTrim 设置日志起始路径

func Sprintf added in v0.0.2

func Sprintf(format any, args ...any) (text string)

func Trace

func Trace(f any, v ...any)

Types

type Conn added in v0.0.2

type Conn struct {
	sync.Mutex
	Network   string `json:"network"`
	Address   string `json:"address"`
	Reconnect bool   `json:"reconnect"`
	Format    func(*Message) string
	// contains filtered or unexported fields
}

func NewConn added in v0.0.2

func NewConn(network, address string) *Conn

func (*Conn) Close added in v0.0.2

func (c *Conn) Close()

func (*Conn) Init added in v0.0.2

func (c *Conn) Init() error

func (*Conn) Name added in v0.0.2

func (c *Conn) Name() string

func (*Conn) Write added in v0.0.2

func (c *Conn) Write(msg *Message) (err error)

type Console added in v0.0.2

type Console struct {
	sync.Mutex
	Sprintf func(*Message) string
	// contains filtered or unexported fields
}

func NewConsole added in v0.0.2

func NewConsole() *Console

func (*Console) Init added in v0.0.2

func (c *Console) Init() (err error)

func (*Console) Write added in v0.0.2

func (c *Console) Write(msg *Message) error

type File added in v0.0.2

type File struct {
	Sprintf  func(*Message) string //格式化message
	FileName fileNameFormatter     //日志文件名
	// contains filtered or unexported fields
}

func NewFile added in v0.0.2

func NewFile(path string) *File

func (*File) Init added in v0.0.2

func (this *File) Init() (err error)

func (*File) Write added in v0.0.2

func (this *File) Write(msg *Message) (err error)

type Interface

type Interface interface {
	Fatal(format any, args ...any) //终止程序运行
	Panic(format any, args ...any) //抛出Panic
	Error(format any, args ...any)
	Alert(format any, args ...any)
	Debug(format any, args ...any)
	Trace(format any, args ...any)
}

type Level added in v0.0.2

type Level int8

func (Level) Brush added in v0.0.2

func (l Level) Brush(text string) string

func (Level) String added in v0.0.2

func (l Level) String() string

type Logger

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

func Default added in v0.0.2

func Default() *Logger

func New

func New(depth ...int) *Logger

func (*Logger) Alert

func (this *Logger) Alert(format interface{}, args ...interface{})

func (*Logger) Debug

func (this *Logger) Debug(format interface{}, v ...interface{})

Debug Log DEBUG level message.

func (*Logger) DelOutput added in v0.0.2

func (this *Logger) DelOutput(name string)

func (*Logger) Error

func (this *Logger) Error(format interface{}, v ...interface{})

Error Log ERROR level message.

func (*Logger) Fatal

func (this *Logger) Fatal(format any, args ...any)

func (*Logger) Panic added in v0.0.2

func (this *Logger) Panic(format any, args ...any)

func (*Logger) SetCallDepth

func (this *Logger) SetCallDepth(depth int)

func (*Logger) SetLevel added in v0.0.2

func (this *Logger) SetLevel(level Level)

SetLevel 设置日志输出等级

func (*Logger) SetOutput added in v0.0.2

func (this *Logger) SetOutput(name string, output Output) error

func (*Logger) SetPathTrim added in v0.0.2

func (this *Logger) SetPathTrim(trimPath string)

SetPathTrim 设置日志起始路径

func (*Logger) Trace

func (this *Logger) Trace(format interface{}, v ...interface{})

Trace Log TRAC level message.

func (*Logger) Write added in v0.0.2

func (this *Logger) Write(msg *Message)

type Message

type Message struct {
	Path    string
	Time    time.Time
	Level   Level
	Stack   string
	Content string
}

func (*Message) String

func (this *Message) String() string

type Output added in v0.0.2

type Output interface {
	Init() error
	Write(message *Message) (err error)
}

Output Output输出时是否对字体染色

Jump to

Keyboard shortcuts

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