0

so i have a go application that has 3 different log output files, and each file has its own logger. when the application panics, the panic trace will always be written to the last logger to be opened, even if the origin of the panic isn't related to the domain of this specific log file.

that happens because in order to write panics i redirect the stderr output to the logger, and the last logger to be redirected to is the one that will be used

    fh, err := os.OpenFile(filename, os.O_CREATE|os.O_APPEND|os.O_WRONLY, 0777)
    err = syscall.Dup2(int(fh.Fd()), int(os.Stderr.Fd()))
    if err != nil {
        fmt.Errorf("failed to redirect stderr to file: %v", err)
    }

is there a way to have the panic always be written to a specific file? or somehow redirecting it to all files?

Thanks

Nitzanu
  • 84
  • 8
  • Does this answer your question? [Capturing panic() in golang](https://stackoverflow.com/questions/34772012/capturing-panic-in-golang) – Nellie Danielyan Aug 24 '20 at 14:32
  • @NellieDanielyan i dont think so, as recover will not help with panics from a different go routine. and all my components use a different logger and are unaware of the other loggers – Nitzanu Aug 24 '20 at 20:37
  • You can probably recover from the panic in its goroutine and pass the message on via a channel to the needed logger component. – Nellie Danielyan Aug 25 '20 at 07:17
  • @NellieDanielyan i dont think it will work on a large scale system as go routines are being opened and close all the time, so 1. i'd hate to have ALL my structs and methods hold a channel and have a defer recover() in them. 2. its not very elegant – Nitzanu Aug 25 '20 at 07:20

0 Answers0