简体   繁体   English

哪里写恐慌日志?

[英]where does go write panic logs?

I installed golang on a redhat server through epel. 我通过epel在redhat服务器上安装了golang。 I'm trying to analyze the panic logs but I can't find where the logs are stored. 我正在尝试分析恐慌日志,但我找不到日志的存储位置。 Any hint ? 任何提示? There is no trace in /var/log / var / log中没有跟踪

      func main() {

            http.HandleFunc("/", panicRecover(test))
            log.Fatal(http.ListenAndServe(":80", nil))
        }



    func test(){
        out, err := exec.Command("echo ", "something").Output()
            if err != nil {
                log.Panic(err)

            }
    }


func panicRecover(f func(w http.ResponseWriter, r *http.Request)) func(w http.ResponseWriter, r *http.Request) {
    return func(w http.ResponseWriter, r *http.Request) {
        defer func() {
            if r := recover(); r != nil {
                log.Printf("PANIC RECOVERED:%s\n", r)
            }
        }()
        f(w, r)
    }

To stderr . stderr

For example, 例如,

package main

import "log"

func main() {
    log.Panic("log.Panic to stderr")
}


$ go run panic.go 2>stderr.txt

stderr.txt File: stderr.txt文件:

2014/06/09 00:30:54 log.Panic to stderr
panic: log.Panic to stderr

goroutine 16 [running]:
runtime.panic(0x497180, 0xc2080001a0)
    /home/peter/go/src/pkg/runtime/panic.c:279 +0xf5
log.Panic(0x7f88770fff20, 0x1, 0x1)
    /home/peter/go/src/pkg/log/log.go:307 +0xb6
main.main()
    /home/peter/gopath/src/so/panic.go:6 +0xa7

goroutine 17 [runnable]:
runtime.MHeap_Scavenger()
    /home/peter/go/src/pkg/runtime/mheap.c:507
runtime.goexit()
    /home/peter/go/src/pkg/runtime/proc.c:1445

goroutine 18 [runnable]:
bgsweep()
    /home/peter/go/src/pkg/runtime/mgc0.c:1976
runtime.goexit()
    /home/peter/go/src/pkg/runtime/proc.c:1445

goroutine 19 [runnable]:
runfinq()
    /home/peter/go/src/pkg/runtime/mgc0.c:2606
runtime.goexit()
    /home/peter/go/src/pkg/runtime/proc.c:1445
exit status 2

By default it writes them to stderr. 默认情况下,它将它们写入stderr。 You can change this with log.SetOutput(os.Writer) . 您可以使用log.SetOutput(os.Writer)更改此log.SetOutput(os.Writer) If you don't want to use 2> on the command line (say it's an application you plan on users running from the GUI filesystem instead of terminal), a good way to log to a file involves using the fact that init is run before the main program starts. 如果您不想在命令行上使用2> (比如说它是您计划从GUI文件系统而不是终端运行的用户的应用程序),那么记录到文件的好方法包括使用init之前运行的事实主程序启动。

For instance, you may do 例如,你可能会这样做

package main

import (
    "log"
    "os"
)

var logFile os.File

func init() {
    var err error
    // Of course, this name isn't unique,
    // I usually use time.Now().Unix() or something
    // to get unique log names.
    logFile,err = os.Create("logfile.txt")
    if err != nil {
        // Open a different logfile or something
    }
    log.SetOutput(logFile)
}

The difficulty is that if you panic you should probably always recover to make sure you Close the file. 困难在于,如果你恐慌,你应该总是恢复,以确保你Close文件。 This is especially true if you're doing buffered logging with bufio where if your program panics, you may not see the most recent data without explicitly flushing the buffer. 如果您使用bufio进行缓冲日志记录,尤其如此,如果您的程序发生bufio ,则可能无法在未明确刷新缓冲区的情况下看到最新数据。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM