hhkbp2/go-logging

Redirect stdout and stderr to the logging file ?

Closed this issue · 1 comments

Is it possible to redirect sdtout and stderr to the logging file ?
Is it also possible to redirect the default log output to the logging file ?

I wish panics could be written to the logging file. I'm also unsure about flushing in case of program crash.

Hi,

A logging facility usually writes messages to stdout/stderr. If stdout/stderr needs to be redirected to a file, it's usually done in OS level, e.g., redirecting file descriptors 1 or 2 to a file using operator > in Linux shell commands.

"default log", I guess you meant the root logger in this module. It's possible to access it and add a handler(which represent an output destination) to it, using codes like

logger := logging.GetLogger("")  // get the default root logger
handler := logging.NewStdoutHandler()
logger.AddHandler(handler)
logger.Infof("message", ...)

Panics are handled by the runtime. If you want to write log message for it, you could put logging codes in panic handler.

The Shutdown() function will flush all log messages and close log files, it's usually involved before the program exits. Make sure it's called in case of program crash, and you get all log messages out there.