有没有一种方法来重定向系统日志消息到标准输出? [英] Is there a way to redirect syslog messages to stdout?

查看:590
本文介绍了有没有一种方法来重定向系统日志消息到标准输出?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个可以在两种模式下运行的应用程序,或者使用CLI,或者作为一个守护进程。

I have an application that can run in two modes, either with a CLI, or as a daemon.

我使用的syslog()的记录。但是,在命令行模式下运行,我想所有的日志,除了那些标记 LOG_DEBUG ,消息被发送到控制台,而不是记录。

I am using syslog() for logging. However, when run in CLI mode I'd like all the logging, except those marked LOG_DEBUG, messages to be sent to the console instead of logged.

我曾尝试使用 setlogmask(),但这似乎并没有重定向到控制台。

I have tried to use setlogmask(), but that doesn't seem to redirect to console.

什么是做到这一点的最好方法是什么?

What is the best way to do this?

推荐答案

作为由maverik的意见建议,我写了周围的syslog的包装,确定是否将输出发送到日志或控制台。这万一有谁需要这个。

As suggested in the comments by maverik, I wrote a wrapper around syslog that determines whether to send the output to the log or console. Here it is in case anyone ever needs this.

void mylog (int level, const char *format, ...)
{
    va_list args;
    va_start (args, format);

    if (remote)
    {
        vsyslog(level, format, args);
    }   
    else
    {
        if (level == LOG_DEBUG)
            vsyslog(level, format, args);
        else
            vprintf(format, args);
    }
    va_end(args);
}

这篇关于有没有一种方法来重定向系统日志消息到标准输出?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆