一、日志简介
二、普通日志信息打印
官方源码如下
func Println(v ...interface{}) {
std.Output(2, fmt.Sprintln(v...))
}
直接使用log包调用Println()即可
log.Println("打印日志信息")
三、Panic日志信息打印
通过源码可以看出在日志信息打印后调用了panic()函数,且日志信息为panic信息
// Panicln is equivalent to Println() followed by a call to panic().
func Panicln(v ...interface{}) {
s := fmt.Sprintln(v...)
std.Output(2, s)
panic(s)
}
执行后输出日志信息,同时也会触发panic
log.Panicln("打印日志信息")
四、致命日志信息
打印日志后,终止程序
// Fatal is equivalent to Print() followed by a call to os.Exit(1).
func Fatal(v ...interface{}) {
std.Output(2, fmt.Sprint(v...))
os.Exit(1)
}
执行日志打印后,程序被终止
log.Fatal("打印日志信息")
五、打印日志信息到文件中
const (
Ldate = 1 << iota // the date in the local time zone: 2022/03/23
Ltime // the time in the local time zone: 03:23:23
Lmicroseconds // microsecond resolution: 01:23:23.123123. assumes Ltime.
Llongfile // full file name and line number: /a/b/c/d.go:23
Lshortfile // final file name element and line number: d.go:23. overrides Llongfile
LUTC // if Ldate or Ltime is set, use UTC rather than the local time zone
LstdFlags = Ldate | Ltime // initial values for the standard logge
)
代码如下
f, _ := os.OpenFile("D:/golog.log", os.O_APPEND|os.O_CREATE, 07777)
defer f.Close()
logger := log.New(f, "[info]\t", log.Ltime)
logger.Println("输出日志信息")
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。