这个程序不会在任何地方打印系统日志吗?

Posted

技术标签:

【中文标题】这个程序不会在任何地方打印系统日志吗?【英文标题】:This program doesn't print syslogs anywhere? 【发布时间】:2013-12-26 05:27:50 【问题描述】:

我用的是fc19,刚刚安装了syslog-ng。看起来 syslog 不再适用于 fc19。我启动了 syslog-ng 服务。 syslog-ng 的状态是运行。这是我的程序。我不知道它在哪里记录输出。

#include<iostream>
#include<ctime>
#include<syslog.h>
#include <unistd.h>

using namespace std;

class my_syslog

  private:
  int m_sys;
  public:
  my_syslog(int init);
  void get_time();
 ;

my_syslog::my_syslog(int init):m_sys(init)

  cout<<"Constructor called"<<endl;  


void my_syslog::get_time()

    time_t now = time(0);
    tm* localtm = localtime(&now);
    //cout << "SJ:The local date and time is: " << asctime(localtm) << endl;
    // Convert now to tm struct for UTC
    //tm* gmtm = gmtime(&now);
    //if (gmtm != NULL) 
    //cout << "The UTC date and time is: " << asctime(gmtm) << endl;
    //
    syslog (LOG_INFO, "SJ:The local date and time is: %s",asctime(localtm));




int main(int argc, char **argv)

    my_syslog instant(100);
    for(int i = 0; i<=10;i++)
    
      instant.get_time();
      sleep(10);

    

    cout<<"Program End"<<endl;
    return 0;

/etc/syslog-ng/syslog-ng.conf 如下。

@version:3.3

# syslog-ng configuration file.
#
# This should behave pretty much like the original syslog on RedHat. But
# it could be configured a lot smarter.
#
# See syslog-ng(8) and syslog-ng.conf(5) for more information.
#
# Note: it also sources additional configuration files (*.conf)
#       located in /etc/syslog-ng/conf.d/

options 
    flush_lines (0);
    time_reopen (10);
    log_fifo_size (1000);
    chain_hostnames (off);
    use_dns (no);
    use_fqdn (no);
    create_dirs (no);
    keep_hostname (yes);
;

source s_sys 
    file ("/proc/kmsg" program_override("kernel") flags(kernel));
    unix-dgram ("/dev/log");
    internal();
    # udp(ip(0.0.0.0) port(514));
;

destination d_cons  file("/dev/console"); ;
destination d_mesg  file("/var/log/messages"); ;
destination d_auth  file("/var/log/secure"); ;
destination d_mail  file("/var/log/maillog" flush_lines(10)); ;
destination d_spol  file("/var/log/spooler"); ;
destination d_boot  file("/var/log/boot.log"); ;
destination d_cron  file("/var/log/cron"); ;
destination d_kern  file("/var/log/kern"); ;
destination d_mlal  usertty("*"); ;

filter f_kernel      facility(kern); ;
filter f_default     level(info..emerg) and
                        not (facility(mail)
                        or facility(authpriv) 
                        or facility(cron)); ;
filter f_auth        facility(authpriv); ;
filter f_mail        facility(mail); ;
filter f_emergency   level(emerg); ;
filter f_news        facility(uucp) or
                        (facility(news) 
                        and level(crit..emerg)); ;
filter f_boot    facility(local7); ;
filter f_cron    facility(cron); ;

#log  source(s_sys); filter(f_kernel); destination(d_cons); ;
log  source(s_sys); filter(f_kernel); destination(d_kern); ;
log  source(s_sys); filter(f_default); destination(d_mesg); ;
log  source(s_sys); filter(f_auth); destination(d_auth); ;
log  source(s_sys); filter(f_mail); destination(d_mail); ;
log  source(s_sys); filter(f_emergency); destination(d_mlal); ;
log  source(s_sys); filter(f_news); destination(d_spol); ;
log  source(s_sys); filter(f_boot); destination(d_boot); ;
log  source(s_sys); filter(f_cron); destination(d_cron); ;


# Source additional configuration files (.conf extension only)
@include "/etc/syslog-ng/conf.d/*.conf"


# vim:ft=syslog-ng:ai:si:ts=4:sw=4:et:

请帮忙解决这里出了什么问题。

【问题讨论】:

在你可以使用syslog()之前你需要openlog() 【参考方案1】:

在你可以使用syslog()之前,你需要openlog()

【讨论】:

不,默认情况下它是打开的。检查以下链接 - linuxselfhelp.com/gnu/glibc/html_chapter/libc_18.html 顺便说一句,我让它运行起来了。我发现我的 syslog-ng 服务没有运行。代码中没有进行任何更改。

以上是关于这个程序不会在任何地方打印系统日志吗?的主要内容,如果未能解决你的问题,请参考以下文章

在任何地方保留任何托管对象是一个坏主意,这是真的吗?

javasystem.out打印日志在linux不打印

如何为 Python 日志记录输出着色?

logback发邮件配置

性能优化之异步日志

性能优化之异步日志