go使用logrus同時輸出屏幕和文件日志


func InitLog() {
   //設置輸出樣式,自帶的只有兩種樣式logrus.JSONFormatter{}和logrus.TextFormatter{}
   log.SetFormatter(&log.TextFormatter{})
   log.SetOutput(os.Stdout)
   //設置output,默認為stderr,可以為任何io.Writer,比如文件*os.File
    file, err := os.OpenFile("checkemstools.log", os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0666)
    writers := []io.Writer{
       file,
       os.Stdout}
   //同時寫文件和屏幕
    fileAndStdoutWriter := io.MultiWriter(writers...)
   if err == nil {
      log.SetOutput(fileAndStdoutWriter)
   } else {
      log.Info("failed to log to file.")
   }
   //設置最低loglevel
   log.SetLevel(log.InfoLevel)
}
————————————————
版權聲明:本文為CSDN博主「xiaowhy」的原創文章,遵循CC 4.0 BY-SA版權協議,轉載請附上原文出處鏈接及本聲明。
原文鏈接:https://blog.csdn.net/XiaoWhy/article/details/107209317

  


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM