首先下载log4go
包 命令:go get github.com/alecthomas/log4go
一,创建loggerUtils.go
工具:
package utils
import (
logger "github.com/alecthomas/log4go"
)
var logger1File = "D:/temp/demo1.log"
var Logger1 = logger.Logger{}
var logger2File = "D:/temp/demo2.log"
var Logger2 = logger.Logger{}
func InitLog() {
rotateStatus := false
logger.NewConsoleLogWriter()
log := logger.NewConsoleLogWriter()
//可以定义输出格式
//log.SetFormat("")
Logger1.AddFilter("stdout", logger.INFO, log)
Logger1.AddFilter("file", logger.DEBUG, logger.NewFileLogWriter(logger1File, rotateStatus))
Logger2.AddFilter("stdout", logger.INFO, log)
Logger2.AddFilter("file", logger.DEBUG, logger.NewFileLogWriter(logger2File, rotateStatus))
}
二,函数调用:
func main() {
utils.InitLog()
for i := 0; i < 10; i++ {
//启动协程 测试日志输出
go utils.Logger1.Info("logger1")
go utils.Logger2.Info("logger2")
}
time.Sleep(10 * 1000 * 1000 * 1000)
}