Skip to content

Ax LogLog And Wrap

ccpaging edited this page Mar 27, 2018 · 1 revision

打开 LogLog 内部日志

import (
	l4g "github.com/ccpaging/nxlog4go"
)

init() {
	l4g.GetLogLog().SetLevel(l4g.TRACE)
}

缺省状态下,loglog 是 nil。调用GetLogLog()自动新建。源程序如下:

// Return internal logger.
// This logger used to output log statements from within the package.
// Do not set any filters.
func GetLogLog() *Logger {
	if loglog == nil {
		loglog = New(_SILENT_).SetPrefix("nxlog4go").SetPattern("%T %P:%S %L %M\n").SetCaller(false)
	}
	return loglog
}

详见:loglog.go

关闭 runtime.Caller

runtime.Caller 耗费 cpu 时间比较多。不适合生产环境下使用。

    log.SetCaller(false)

source 参数可以用函数参数的方式传递。

如果使用了多个 package 共享 logger且含同一组 Appender,可以用prefix来记录模块名。


封装日志函数

一般来说,nxlog4go已经封装的日志函数足够用了。

在应用里做封装是因为关闭了runtime.Caller,需要传递source参数。

func Debug(source string, arg0 interface{}, args ...interface{}) {
	log.Log(l4g.DEBUG, source, arg0, args ...)
}

func Info(source string, arg0 interface{}, args ...interface{}) {
	log.Log(l4g.INFO, source, arg0, args ...)
}

func Warn(source string, arg0 interface{}, args ...interface{}) {
	log.Log(l4g.WARNING, source, arg0, args ...)
}

func Error(source string, arg0 interface{}, args ...interface{}) {
	log.Log(l4g.ERROR, source, arg0, args ...)
}
Clone this wiki locally