记录到golang中的文件 [英] Logging to a file in golang

查看:339
本文介绍了记录到golang中的文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我从golang开始,当我开始构建我的applciation时,我想从一开始就添加日志记录,这就是我遇到的问题。



如果我打开文件并使用标准日志记录库,则可以写入文件。

 包主


$ b导入(
os
fmt
log



func main(){
//打开一个文件
f,err:= os.OpenFile(test.log,os.O_APPEND | os.O_CREATE | os.O_RDWR,0666)
if err!= nil {
fmt.Printf(错误打开文件:%v,err)
}

//不要忘记关闭它
推迟f.Close()

//将其分配给标准记录器
log.SetOutput(f)

log.Output(1,this is an event)

}

我将得到test.log中的日志行。但是,如果我尝试将其用于支持logrus https://github.com/Sirupsen/logrus 这样

  package main 

import(
os
fmt
loggithub.com/Sirupsen/logrus


func init(){

//打开文件
f, err:= os.OpenFile(testlogrus.log,os.O_APPEND | os.O_CREATE | os.O_RDWR,0666)
if err!= nil {
fmt.Printf(error opening file: %v,err)
}

//不要忘记关闭它
推迟f.Close()

// Log as JSON而不是默认的ASCII格式器。
log.SetFormatter(& log.JSONFormatter {})

//输出到stderr而不是stdout,也可以是一个文件。
log.SetOutput(f)

//只记录警告严重性或以上。
log.SetLevel(log.DebugLevel)
}


func main(){

log.WithFields(log.Fields {
Animal:Logrus,
})。Info(出现logrus)


}



所有我将会看到的都是错误。



无法写入日志,写入testlogrus .log:错误的文件描述符

我得到的只是错误的文件描述符错误。任何想法我做错了什么?



感谢
Craig

解决方案

由于您在 init 函数中设置了文件,并且您有推迟f.Close(),所以文件在 init 返回后关闭。



您必须保持文件打开或将整个文件移动到主文件。


I am starting out with golang, and as I am starting to build out my applciation I want to add logging from the start, and that is where I am running into issues.

If I open a file and use the standard logging library, I am able to write to a file. Like so.

    package main



   import (
        "os"
        "fmt"
        "log"
    )


    func main() {
        // open a file
        f, err := os.OpenFile("test.log", os.O_APPEND | os.O_CREATE | os.O_RDWR, 0666)
        if err != nil {
            fmt.Printf("error opening file: %v", err)
        }

        // don't forget to close it
        defer f.Close()

        // assign it to the standard logger
        log.SetOutput(f)

        log.Output(1, "this is an event")

    }

I will get my test.log with the log line in it. However if I try to adapt this to support logrus https://github.com/Sirupsen/logrus like this

    package main

import (
    "os"
    "fmt"
    log "github.com/Sirupsen/logrus"
)

func init() {

    // open a file
    f, err := os.OpenFile("testlogrus.log", os.O_APPEND | os.O_CREATE | os.O_RDWR, 0666)
    if err != nil {
        fmt.Printf("error opening file: %v", err)
    }

    // don't forget to close it
    defer f.Close()

    // Log as JSON instead of the default ASCII formatter.
    log.SetFormatter(&log.JSONFormatter{})

    // Output to stderr instead of stdout, could also be a file.
    log.SetOutput(f)

    // Only log the warning severity or above.
    log.SetLevel(log.DebugLevel)
}


func main() {

    log.WithFields(log.Fields{
        "Animal": "Logrus",
    }).Info("A logrus appears")


}

All I will ever see are errors.

Failed to write to log, write testlogrus.log: bad file descriptor

All I ever get is the bad file descriptor error. Any ideas what I am doing wrong?

Thanks Craig

解决方案

Since you setup the file in the init function and you have defer f.Close(), the file gets closed after init returns.

you either have to keep the file open or move the whole thing into main.

这篇关于记录到golang中的文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆