golang / go

The Go programming language
https://go.dev
BSD 3-Clause "New" or "Revised" License
122.88k stars 17.52k forks source link

log/syslog: local messages (syslog.New()) do not work on macOS Monterey/Ventura #59229

Open craig65535 opened 1 year ago

craig65535 commented 1 year ago

What version of Go are you using (go version)?

$ go version
1.18.10

Does this issue reproduce with the latest release?

Yes

What operating system and processor architecture are you using (go env)?

darwin/arm64

$ sw_vers
ProductName:            macOS
ProductVersion:         13.2.1
BuildVersion:           22D68

What did you do?

package main

import (
    "log/syslog"
)

func main() {
    msgStr := `hello`
    w, err := syslog.New(syslog.LOG_WARNING|syslog.LOG_DAEMON, ``)
    if err != nil {
        panic(err)
    }
    w.Err(msgStr)
    w.Close()
}

What did you expect to see?

hello logged to the macOS Console.app

What did you see instead?

Nothing is logged.

I noticed that I can get this working with cgo -

package main

// #include <syslog.h>
// void doLog(int facility, const char *msg) {
//  syslog(facility, "%s", msg);
//}
import "C"

func main() {
    msgStr := `hello`
    C.doLog(C.LOG_ERR|C.LOG_DAEMON, C.CString(msgStr))
}

So syslog is working and this is not a configuration issue. I think the problem is that macOS's logd no longer accepts syslog messages on /var/run/syslog, or perhaps the expected format has changed.

I can see that it's listening:

$ sudo lsof|grep var/run/syslog
launchd       1                  root    3u     unix 0x3a45c3be17aa6ff3          0t0                     /private//var/run/syslog
logd        313                  root    7u     unix 0x3a45c3be17aa6ff3          0t0                     /private//var/run/syslog

But I'm not sure if calling syslog(3) is using some other mechanism to reach logd.

craig65535 commented 1 year ago

This was noted by the Python people: https://github.com/python/cpython/issues/91070

craig65535 commented 1 year ago

FWIW I do have a (terrible) workaround for this that doesn't involve cgo. I exec /usr/bin/logger -p <facility>.<severity> and then feed it messages over stdin. It doesn't tag the messages as coming from my process, but it does get logs into the Console and log store.

mauri870 commented 1 year ago

This should at least be documented properly, something akin to this by the python folks:

   .. note:: On macOS 12.x (Monterey), Apple has changed the behaviour of their
      syslog daemon - it no longer listens on a domain socket. Therefore, you cannot
      expect :class:`SysLogHandler` to work on this system.

      See :gh:`91070` for more information.