logging to syslog file with Zerolog (GoLang)

Viewed 223

I just started writing my first project in GoLang. I want to send logs to syslog(/var/log/syslog) file.

I am using zerolog(github.com/rs/zerolog) package to write logs. This is my how I am logging currently:

log.Error().Str("Event_name", Event_name).Msg("Invalid event")

It is printing logs to terminal but not to syslog file. Can someone give example or explain how to do that? Thanks in advance.

1 Answers

You should open the file /var/log/syslog and then set the output of your logger to that file:

output, err := os.OpenFile("/var/log/syslog", os.O_CREATE|os.O_APPEND|os.O_WRONLY, 0666)
if err != nil {
    // unhandled error
}

log.Error().Str("Event_name", Event_name).Msg("Invalid event")

log.Output(output)

By the way, you must have sudo privileges when reading/writing system files. this means that your executable must be run under sudo

Related