我尝试为我的代码添加日志,但我无法在单独的行中获取每个日志
这是我的代码
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
我想让他们每个人都在一个单独的行中
先谢谢
答案 0 :(得分:1)
也许这段代码会帮助你(“\ n” - &gt;“\ n \ r”)(“\ n \ r”与“\ r \ n”的作用相同):< / p>
log.SetPrefix("\r")