如何将应用程序printf消息传递给/ var / log / messages

时间:2021-03-15 09:29:05

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.

我想将应用程序printf日志消息传递给/ var / log / messages。因为/var/log/messages可以看到内核调试消息。但是我没有得到如何将应用程序printf日志消息传递给/ var / log / messages。任何人都可以建议我如何做到这一点。

1 个解决方案

#1


3  

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).

请注意,syslog总是一次发送一个完整的行(并且不需要\ 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()之前使用openlog()函数。有关更多详细信息,请参见openlog手册页。

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:

Syslog根据消息优先级路由消息 - syslog的第一个参数(例如上面示例中的LOG_ERR) - 以及设施。该工具要么设置对openlog()的调用,要么默认为LOG_USER。 syslog配置文件的基本语法是以下形式的行:

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.

可能还有一些其他选项(例如,“none”作为优先级,特定优先级,如= warn,以及否定,如!= warn),请参阅syslog.conf联机帮助页以获取有关这些选项的详细信息。

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.

有关系统如何路由其syslog消息,请参阅syslog配置文件(通常为/etc/syslog.conf)。注意:某些系统运行syslog的变体,如nsyslog或rsyslog - 它们有更多选项,因此可能是更复杂的配置文件。

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.

已知的设施有: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不可用于用户进程)通常,LOG_DAEMON,LOG_LOCALn或LOG_USER之一通常是最佳选择。

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.

已知优先级(也称为严重性)是(从最高到最低):LOG_EMERG,LOG_ALERT,LOG_CRIT,LOG_ERR,LOG_WARNING,LOG_NOTICE,LOG_INFO,LOG_DEBUG。

#1


3  

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).

请注意,syslog总是一次发送一个完整的行(并且不需要\ 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()之前使用openlog()函数。有关更多详细信息,请参见openlog手册页。

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:

Syslog根据消息优先级路由消息 - syslog的第一个参数(例如上面示例中的LOG_ERR) - 以及设施。该工具要么设置对openlog()的调用,要么默认为LOG_USER。 syslog配置文件的基本语法是以下形式的行:

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.

可能还有一些其他选项(例如,“none”作为优先级,特定优先级,如= warn,以及否定,如!= warn),请参阅syslog.conf联机帮助页以获取有关这些选项的详细信息。

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.

有关系统如何路由其syslog消息,请参阅syslog配置文件(通常为/etc/syslog.conf)。注意:某些系统运行syslog的变体,如nsyslog或rsyslog - 它们有更多选项,因此可能是更复杂的配置文件。

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.

已知的设施有: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不可用于用户进程)通常,LOG_DAEMON,LOG_LOCALn或LOG_USER之一通常是最佳选择。

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.

已知优先级(也称为严重性)是(从最高到最低):LOG_EMERG,LOG_ALERT,LOG_CRIT,LOG_ERR,LOG_WARNING,LOG_NOTICE,LOG_INFO,LOG_DEBUG。