我编写了一段小代码,用于在连接到 postgres 数据库失败时使用 C Api 向系统日志发送消息。
int main ( int argc, char **argv )
{
PGconn *psql;
PGresult *res;
int flag = 0;
openlog ( "postgres", LOG_NDELAY, LOG_SYSLOG );
psql = PQconnectdb("hostaddr = '127.0.0.0' port = '5432' dbname = 'RtpDb' user = 'rtp_user_99' password = 'rtp_user' connect_timeout = '10'");
if ( PQstatus(psql) != CONNECTION_OK )
{
//Send an event to syslog for DB Connection Failure
syslog (LOG_EMERG, "%s", PQerrorMessage(psql) )
}
closelog ();
PQclear(res);
PQfinish(psql);
}
当 postgres 数据库连接失败时,即使 openlog 中未启用 LOG_CONS 选项,消息也会打印到控制台。
Broadcast message from systemd-journald@blr09 (Tue 2017-01-03 05:24:46 EST):
postgres[40933]: could not connect to server: Network is unreachable
Is the server running on host "127.0.0.0" and accepting
TCP/IP connections on port 5432?
Message from syslogd@blr09 at Jan 3 05:24:46 ...
postgres:could not connect to server: Network is unreachable#012#011Is the server running on host "127.0.0.0" and accepting#012#011TCP/IP connections on port 5432?
您能帮助我如何避免在控制台上打印消息吗?
在@alk提供的提示之后,我做了一些更多的研究,并找到了如何避免在控制台上打印消息。
Broadcast message from systemd-journald@blr09 (Tue 2017-01-03 05:24:46 EST):
postgres[40933]: could not connect to server: Network is unreachable
Is the server running on host "127.0.0.0" and accepting
TCP/IP connections on port 5432?
Message from syslogd@blr09 at Jan 3 05:24:46 ...
postgres:could not connect to server: Network is unreachable#012#011Is the server running on host "127.0.0.0" and accepting#012#011TCP/IP connections on port 5432?
上述消息有两部分:
来自systemd-journald的广播消息 => 当发送紧急消息时,这些消息将通过journalctl打印在控制台上。要禁用这些消息,我们需要禁用 ForwardToWall,即ForwardToWall=否 in /etc/systemd/journald.conf
-
来自 syslogd 的消息 => 由于 /etc/rsyslog.conf 中的以下配置行,这些消息由 rsyslog 打印
*.emerg :omusrmsg:*
这个选择器动作状态“紧急消息通常会发送给当前在线的所有用户,通知他们系统发生了异常情况。要指定此 wall(1) 功能,请使用“:omusrmsg:*”。”注释掉这一行。
执行上述操作后,控制台上未打印消息。由于由于安全威胁而不允许这些操作,因此我正在向Alert优先事项。
syslog ( LOG_ALERT, "%s", PQerrorMessage(psql) );
感谢@alk。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)