我试图为我的代码添加日志,但我无法在单独的行上获取每个日志这是我的代码 package main import ( "log" "os" )func update(){ log.Println(1,"update") log.Println(1,"update") log.Println(1,"update") log.Println(1,"update")}func install(){ log.Println(1,"install") log.Println(1,"install") log.Println(1,"install") log.Println(1,"install")} func main() { // make sure log.txt exists first // use touch command to create if log.txt does not exist os.Create("log/Installationlog.txt") logFile, err := os.OpenFile("log/Installationlog.txt", os.O_WRONLY, 0666) if err != nil { panic(err) } defer logFile.Close() // direct all log messages to log.txt log.SetOutput(logFile) log.Println("ERROR::First log message!") install() update() }我的代码的输出是2016/01/18 13:30:51 ERROR::First log message! 2016/01/18 13:30:51 1 install 2016/01/18 13:30:51 1 install 2016/01/18 13:30:51 1 install 2016/01/18 13:30:51 1 install 2016/01/18 13:30:51 1 update 2016/01/18 13:30:51 1 update 2016/01/18 13:30:51 1 update 2016/01/18 13:30:51 1 update全部在一行中,我希望它们每个都在单独的行中
1 回答
慕工程0101907
TA贡献1887条经验 获得超5个赞
也许这段代码会帮助你 ("\n" -> "\n\r") ("\n\r" 与 "\r\n" 的作用相同):
log.SetPrefix("\r")
- 1 回答
- 0 关注
- 212 浏览
添加回答
举报
0/150
提交
取消