自带的日志库
log.SetPrefix("mr_") //加前缀
log.SetFlags(log.Ldate | log.Lmicroseconds|log.Lshortfile) //精确到毫秒,并且抬头信息为时间+文件名+源代码所在行号
logFile, err := os.OpenFile("mylog.log", os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0666)
if (nil != err){
}
log.SetOutput(logFile)
for i := 0; i < 100; i++{
log.Printf("%s:%s", "name", "goer")
}
双写
pLog := log.New(io.MultiWriter(os.Stderr, logFile),"Error:", log.Ldate | log.Lmicroseconds)
for i := 0; i < 100; i++{
pLog.Printf("%s:%s", "name", "goer")
}
Go标准库的日志框架非常简单,仅仅提供了Print,Panic和Fatal三个函数。对于更精细的日志级别、日志文件分割,以及日志分发等方面,并没有提供支持
logrus
logFile, err := os.OpenFile("mylog.log", os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0666)
if (nil != err){
}
customFormatter := &logrus.TextFormatter{
FullTimestamp: true,
TimestampFormat: "2006-01-02 15:04:05.000", //毫秒时间,注意不能使用空格 2006-01-02 15:04:05 000
ForceColors: true,
}
logger := logrus.New()
logger.Formatter = customFormatter
logger.Out = logFile
for i := 0; i < 100; i++{
logger.Info("logrus log to lumberjack in normal text formatter")
time.Sleep(time.Millisecond * 1)
}
格式化日志,占位符
log.Fatalf("Failed to send event %s to topic %s with key %d", "click", "xiaoyu", 123)