I want to pass application printf log messages to the /var/log/messages. Because kernel debug messages can be visible to /var/log/messages.But i am not getting how to pass application printf log messages to the /var/log/messages. Can anyone please suggest me how to do this.
-
3`printf()` prints to `stdout`. It's just about the worst "logging" mechanism possible, especially for long-lived processes. If you want to print to `/var/adm/messages`, see the man page for `syslog()`: http://man7.org/linux/man-pages/man3/syslog.3.html – Andrew Henle Dec 17 '15 at 17:53
1 Answers
Change from, for example:
printf("The error was %s\n", strerror(errno));
to:
syslog(LOG_ERR, "The error was %s", strerror(errno));
Note that syslog is always sent a complete line at a time (and no need for \n
).
For more control over your logging options use the openlog()
function before any calls to syslog()
. See the openlog man page for more details.
Syslog routes messages based on the message priority -- the first argument to syslog
(eg LOG_ERR
in the sample above) -- and the facility. The facility either set in a call to openlog()
or it defaults to LOG_USER
. The basic syntax of the syslog configuration file is lines of the form:
selector[;selector] destination
where selector is:
facility[,facility].priority
(facility and/or priority can be the wildcard *
). A priority implicitly includes all higher priorities.
The destination can be a file, a remotehost, a program or a (list of) users. Some examples:
*.* ihateyou -- every message sent to this user
*.debug * -- to every logged in user!
*.emerg root,bob,tom -- emergencies to these three
*.err /var/log/all-errors -- all LOG_ERR and above to this file
cron.info |/some/program -- pipe these to /some/program
user.* @some.host.com -- send these to this host
cron,user.crit /this/file -- an example with multiple facilities
lpr.err;ftp.warn /other/file -- an example with multiple selectors
There may be some additional options, (eg, "none" as priority, a specific priority like =warn, and negation like !=warn), see your syslog.conf manpage for details on those.
See your syslog configuration file (usually /etc/syslog.conf) for how your system is routing its syslog messages. Note: some systems run a variant of syslog like nsyslog
or rsyslog
-- these have more options and hence a potentially more complex configuration file.
The known facilities are: LOG_AUTH, LOG_AUTHPRIV, LOG_CRON, LOG_DAEMON, LOG_FTP, LOG_KERN, LOCAL_LOCAL0 .. LOG_LOCAL7, LOG_LPR, LOG_MAIL, LOG_NEWS, LOG_SYSLOG, LOG_USER, LOG_UUCP. (LOG_KERN is not available to user processes) Generally, one of LOG_DAEMON, LOG_LOCALn or LOG_USER is usually the best choice.
The known priorities (also called severities) are (highest to lowest): LOG_EMERG, LOG_ALERT, LOG_CRIT, LOG_ERR, LOG_WARNING, LOG_NOTICE, LOG_INFO, LOG_DEBUG.

- 9,176
- 6
- 48
- 72