写入 panic 日志的位置在哪里?

huangapple go评论88阅读模式
英文:

where does go write panic logs?

问题

我通过epel在redhat服务器上安装了golang。我正在尝试分析panic日志,但是找不到日志存储的位置。有什么提示吗?/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)
    }
}

以上是你提供的代码。

英文:

I installed golang on a redhat server through epel. 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

      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)
	}

答案1

得分: 8

stderr

例如,

package main

import "log"

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


$ go run panic.go 2>stderr.txt

stderr.txt 文件:

<!-- language: none -->

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
英文:

To stderr.

For example,

package main

import &quot;log&quot;

func main() {
	log.Panic(&quot;log.Panic to stderr&quot;)
}


$ go run panic.go 2&gt;stderr.txt

stderr.txt File:

<!-- language: none -->

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

huangapple
  • 本文由 发表于 2014年6月9日 12:06:09
  • 转载请务必保留本文链接:https://go.coder-hub.com/24113527.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定