Qiita Teams that are logged in
You are not logged in to any team

Log in to Qiita Team
Community
OrganizationEventAdvent CalendarQiitadon (β)
Service
Qiita JobsQiita ZineQiita Blog
44
Help us understand the problem. What is going on with this article?

More than 5 years have passed since last update.

@74th

golangでlogを標準出力とテキストファイルの2箇所の出力する

2箇所に出力したい場合、io.MultiWriterが便利。

logfile, err := os.OpenFile("./test.log", os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0666)
if err != nil {
    panic("cannnot open test.log:" + err.Error())
}
defer logfile.Close()

// io.MultiWriteで、
// 標準出力とファイルの両方を束ねて、
// logの出力先に設定する
log.SetOutput(io.MultiWriter(logfile, os.Stdout))

log.SetFlags(log.Ldate | log.Ltime)
log.Println("Log!!")
44
Help us understand the problem. What is going on with this article?
Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
44
Help us understand the problem. What is going on with this article?