1

Is there any way to redirect output from panic to a file with a timestamp for each panic.

Current example result of the panic log file:

goroutine 6 [running]:
_/C_/....func·001(0x11691e80, 0x1a, 0x0, 0x0, 0x10d3550, 0x11691ec0, 0x0, 0x0)
C:/.../Logger.go:309 +0x47
path/filepath.Walk(0x11691e80, 0x1a, 0x1161defc, 0x0, 0x0)
c:/...../path.go:392 +0x97
_/C_/...../Logger.Sample(0x1168ac00, 0x59, 0x0, 0x0)
C:/...../Logger.go:322 +0x1c5
main.handleFileActions()
C:/...../main.go:453 +0x2ad
created by main.main
C:/..../main.go:278 +0x6ea

Expected result:

2017-02-27T14:24:22.627Z - goroutine 6 [running]:
_/C_/....func·001(0x11691e80, 0x1a, 0x0, 0x0, 0x10d3550, 0x11691ec0, 0x0, 0x0)
C:/.../Logger.go:309 +0x47
path/filepath.Walk(0x11691e80, 0x1a, 0x1161defc, 0x0, 0x0)
c:/...../path.go:392 +0x97
_/C_/...../Logger.Sample(0x1168ac00, 0x59, 0x0, 0x0)
C:/...../Logger.go:322 +0x1c5
main.handleFileActions()
C:/...../main.go:453 +0x2ad
created by main.main
C:/..../main.go:278 +0x6ea
Elad
  • 664
  • 1
  • 5
  • 14
  • Possible duplicate of [Capturing panic() in golang](http://stackoverflow.com/questions/34772012/capturing-panic-in-golang). – icza Feb 27 '17 at 12:58
  • I probably not explain myself right, I've managed to capture the panic, my question is, is there an option to log the panic with a time stamp like in the expected result example, i can share my code if needed. – Elad Feb 27 '17 at 13:05
  • 2
    If you used `recover()`, then you may execute a `print(time.Now().UTC().Format("2006-01-02T15:04:05.000Z - "))` before logging the panic. Otherwise, I don't think so. – icza Feb 27 '17 at 13:11
  • I will try to use `recover()`, thanks. – Elad Feb 27 '17 at 13:32

1 Answers1

0

It is possible to add a timestamp to your log file.

Because the errors are also just values, you are able to do something before you panic.

Just a simple example:

var buf bytes.Buffer
logger := log.New(&buf, "logger: ", log.Ldate|log.Ltime|log.Llongfile)
logger.Print("Error when calling that code")
fmt.Print(&buf)
logger.Panic("Error")

https://play.golang.org/p/g4mweH4Dex

Here you should not panic as soon as your error occurs. You can write some more information inside your log. For example the parameters of your function or the values of the config file.

With the correct flags inside your logger definition log.Ldate|log.Ltime|log.Llongfile you will also get a timestamp.

Edited: Thanks to reticentroot for the comment about the recover wrapper. The article Defer, Panic and Recover describes a way how to handle panicking. Here you can recover by using defer.

func main() {
    myFunc()
    fmt.Print(&buf)
}

func myFunc() {
    defer func() {
        if r := recover(); r != nil {
            logger.Println("Recovered from panic: ", r)
        }
    }()
    myPanic()
}

func myPanic() {
    panic("Panicking in myPanic")
}

https://play.golang.org/p/8swGF1SoO_

apxp
  • 5,240
  • 4
  • 23
  • 43
  • better place your code above inside a recover() wrapper so that the OP can "catch" all the panics and log them to file. Its a common pattern for web servers, but i'm sure it will work here as well. – reticentroot Feb 27 '17 at 19:23
  • How can i use the above example to catch all panics and log them to file with a time stamp? – Elad Mar 01 '17 at 09:43
  • I already had added a link to the go playground. This is the logic. Inside that logger a `bytes.Buffer` is used. Instead of printing you could write the output into a file. You could also use another `io.Writer` which writes direct into a file. – apxp Mar 01 '17 at 15:30