douhanxujiuji6098 2018-05-17 21:02
浏览 49
已采纳

为什么使用自定义log.Logger登录文件失败?

I cannot get my program to write to the log file from a function outside of main() I did see https://stackoverflow.com/a/19966217/4374801 which is similar, but does not address my exact problem.
Here is the important bits of what I am doing:

var (
    Info    *log.Logger
    Error   *log.Logger
)

func init() {  
    // set up log file                                                                                                       
    fileHandle, err := os.OpenFile("/var/log/checkcert", os.O_WRONLY|os.O_CREATE|os.O_APPEND, 0644)                          
    if err != nil {                                                                                                          
        log.Fatal(err)                                                                                                       
    }                                                                                                                        
    //defer to close when you're done with it                                                                                
    defer fileHandle.Close()                                                                                                 

    //set output of logs to fileHandle                                                                                       
    log.SetOutput(fileHandle)                                                                                                

    Info = log.New(fileHandle,                                                                                               
        "Log: ",                                                                                                             
        log.Ldate|log.Ltime|log.Lshortfile)                                                                                  

    Error = log.New(fileHandle,                                                                                              
        "Error: ",                                                                                                           
        log.Ldate|log.Ltime|log.Lshortfile)                                                                                  
} 

// The function below is called from main()
// The version here is truncated to the pertinent bit
func checkDomain(){
    Info.Println("test inside checkDomain")
}

The above creates the log file, but does not write the test to it. If I put a test Println within init(), that works.

  • 写回答

1条回答 默认 最新

  • duanao2585 2018-05-17 21:34
    关注

    The defer will execute at the end of the function, meaning your file will be closed at the end of the init function before you even run main. You should not close your file until main ends, so perhaps consider moving the initialisation of your loggers and file into the start of main instead of init.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 求解 yolo算法问题
  • ¥15 虚拟机打包apk出现错误
  • ¥30 最小化遗憾贪心算法上界
  • ¥15 用visual studi code完成html页面
  • ¥15 聚类分析或者python进行数据分析
  • ¥15 三菱伺服电机按启动按钮有使能但不动作
  • ¥15 js,页面2返回页面1时定位进入的设备
  • ¥50 导入文件到网吧的电脑并且在重启之后不会被恢复
  • ¥15 (希望可以解决问题)ma和mb文件无法正常打开,打开后是空白,但是有正常内存占用,但可以在打开Maya应用程序后打开场景ma和mb格式。
  • ¥20 ML307A在使用AT命令连接EMQX平台的MQTT时被拒绝