What is the appropriate way for applications (for example Node) to log under Linux? With Windows there is the event log which applications can write to.
Should we just create log files under
var/log for each application?
Super User is a question and answer site for computer enthusiasts and power users. It only takes a minute to sign up.Sign up to join this community
The traditional API on Unix-like systems is syslog(3).
openlog("myapp", 0, LOG_DAEMON); ... syslog(LOG_INFO, "Thing '%s' happened", thing->name);
Using the syslog() API will send messages to the currently installed system logger, which then allows the sysadmin to filter messages, apply rotation policies, forward to a remote server, and so on.
The corresponding Node module appears to be node-syslog, but you can implement it manually by sending messages to the
/dev/log Unix socket.
Many Linux distributions also have systemd-journal with its own API, which allows metadata to be attached (for example, all messages about a single request can be marked with a metadata field "MYAPP_REQUEST_ID=[...]" and then easily looked up later).
sd_journal_send("MESSAGE=Thing '%s' happened", thing->name, "PRIORITY=%i", LOG_INFO, NULL);
There is a Node module called systemd-journald that provides access to this API.
A simple option is to write all log messages to stderr. Ideally it should use a simple format with just one message per line – no colours, no timestamps, no fancy prefixes/markers; the service manager will add its own. In this mode the only thing you should add is the syslog-style priority number (e.g. 3 corresponding to LOG_ERR):
<3>Something bad happened.
For general-purpose messages, creating your own file and especially inventing your own log format is not recommended; it'll make life harder for the sysadmin. On the other hand, it is a good idea to do so when you're about to log massive amounts of messages which would otherwise overwhelm the syslog. (For example, webservers like Apache will log every request to the /var/log/apache/access.log file by default.)