log4golog4goJavalog4j
仓库地址:
https://github.com/jeanphorn/log4go.git
2. 特点json
3. 使用示例
- 下载源码
go get github.com/jeanphorn/log4go
- 导入进工程
import log "github.com/jeanphorn/log4go"
- 具体步骤,请查看
https://github.com/jeanphorn/log4go
4. 不用配置文件使用log
package mainimport (l4g "github.com/jeanphorn/log4go"
)var (Log l4g.Logger = make(l4g.Logger)
)func InitLog(filename string) {_, err := os.Stat("./log/")if os.IsNotExist(err) {os.Mkdir("./log/", 0755)}flw := l4g.NewFileLogWriter("./log/"+filename, false, false)flw.SetFormat("[%D %T] [%L] (%S) %M")flw.SetRotate(true)flw.SetRotateSize(1 * 1024 * 1024) // 10Mflw.SetRotateMaxBackup(10) // 备份的日志文件数量flw.SetRotateDaily(false) // 不按天回滚flw.SetSanitize(true)Log.AddFilter("file", l4g.INFO, flw)Log.Info("Log init done")time.Sleep(100 * time.Microsecond)
}func main() {InitLog("test.log")Log.Info("this is info")
}
但是运行之后,log 文件中没有日志打印,原因是:
log4gochannelgoroutinegoroutinegoroutinechannel
5. 解决方法
time.Sleep(time.Microsecond)sleep()goroutine
func main() {InitLog("test.log")Log.Error("this is error")time.Sleep(100 * time.Microsecond)Log.Info("this is info")time.Sleep(100 * time.Microsecond)
}
6. 其它 log 库
- https://github.com/hhkbp2/go-logging.git
- https://github.com/cihub/seelog
参考:
- https://stackoverrun.com/cn/q/3852638
- https://www.v2ex.com/amp/t/239039