Golang application issue with log/syslog

Hi,

I wrote sample golang code that send application logs to syslog server.

Example Go Code:

package main

import (
“fmt”
“log/syslog”
“time”
)

type syslogImpl struct {
Writer *syslog.Writer
SyslogNetwork string
SyslogRaddr string
}

var (
logWriter *syslogImpl
)

func main() {
fmt.Println(“inside main …”)
fmt.Println("\nLogging application information to syslog.")
rADDR := “localhost:514”
fmt.Println("Syslog server address : " + rADDR)
hook, err := logHook(“tcp”, rADDR, syslog.LOG_ERR|syslog.LOG_INFO, “TESTING”)
if err != nil {
fmt.Println("Error initializing syslog Address : ", rADDR, " , Error : ", err)
}
logWriter = hook
fmt.Println("Writing to location " + logWriter.SyslogRaddr)
logWriter.Writer.Info(“main …”)
time.Sleep(5 * time.Second)
fmt.Println(“Program Termination”)
}

func logHook(network string, raddr string, priority syslog.Priority, tag string) (*syslogImpl, error) {
hook, err := syslog.Dial(network, raddr, priority, tag)
if err != nil {
fmt.Println("Error while attempting to connect syslog server at ", network, “. Please check syslog configuration value.”)
panic(err)
}
fmt.Fprintf(hook, “This is a daemon warning with demotag.”)
hook.Emerg(“And this is a daemon emergency with demotag.”)
return &syslogImpl{hook, network, raddr}, err
}

Output Detail : Here i got output on “localhost:514”. But got message after 5 seconds at the time of application terminated.
I want messages when Info() method called.
Is there anyways to solve this issue.?
Eagerly waiting for some one response.

Thanks,
Parth

According to docs https://golang.org/pkg/log/syslog/#Priority

The Priority is a combination of the syslog facility and severity. For example, LOG_ALERT | LOG_FTP sends an alert severity message from the FTP facility. The default severity is LOG_EMERG; the default facility is LOG_KERN.

But you uses two severities. Try changing to syslog.LOG_INFO | syslog.LOG_USER for example

Tried.
Still is not working as per expectation.

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.