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 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog