How can I syslog without PID

55 views
Skip to first unread message

Dean Schulze

unread,
Apr 16, 2020, 11:16:17 PM4/16/20
to golang-nuts
I have a C program with a syslog command like this:

syslog(LOG_INFO, "slurmctrld: initializing resources");

It outputs the first line below in the syslog.  This C code executes a golang binary that also does syslog commands:

    Syslogger, err = syslog.New(syslog.LOG_INFO, "slurmctld")
    ....
    Syslogger.Info("slurmctld: initializing resources - completed")

The golang code writes the last 3 entries in the syslog, but they include a PID or thread id:

Apr 16 19:18:37 slurmctld slurmctld: slurmctrld: initializing resources
Apr 16 19:19:12 slurmctld slurmctld[24785]: slurmctrld: creating resource group
Apr 16 19:19:15 slurmctld slurmctld[24785]: slurmctrld: creating machines
Apr 16 19:20:28 slurmctld slurmctld[24785]: slurmctld: initializing resources - completed

In golang is there a way to get syslog messages without the PID so they look the same as from C code?

Dan Kortschak

unread,
Apr 17, 2020, 12:24:02 AM4/17/20
to Dean Schulze, golang-nuts
You can get the PID with https://golang.org/pkg/os/#Getpid and use that
you construct a string to hand in as tag to syslog.New.
> --
> You received this message because you are subscribed to the Google
> Groups "golang-nuts" group.
> To unsubscribe from this group and stop receiving emails from it,
> send an email to golang-nuts...@googlegroups.com.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/golang-nuts/18e63047-8b38-4276-8584-17d0a5714c21%40googlegroups.com
> .


Ian Lance Taylor

unread,
Apr 17, 2020, 12:25:01 AM4/17/20
to Dean Schulze, golang-nuts
No, there isn't. The Go log/syslog package in effect acts like the C
function syslog if LOG_PID is passed to openlog.

If this is important to you I recommend that you just copy the
log/syslog package and edit it for your needs. It's about 400 lines.

Ian
Reply all
Reply to author
Forward
0 new messages