如何使用log4cplus检索日志条目?

发布于 2024-12-01 10:06:19 字数 77 浏览 1 评论 0原文

log4cplus很强大,但我不知道如何用它检索日志条目?有什么特点吗? log4cplus 提供的任何 API 或类似的东西吗?提前致谢。

log4cplus is powerful, but I don't know how to retrieve log entries with it? Are there any features? Any APIs or things like that provided by log4cplus? Thanks in advance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

云巢 2024-12-08 10:06:19

好吧,我不确定你到底想要什么。 log4cplus 文档为您提供了使用它的示例。例如:

BasicConfigurator config;
config.configure();

Logger logger = Logger::getInstance("mylogger");
LOG4CPLUS_WARN(logger, "Hello, World!");

这样您将在控制台上获得日志。如果您想配置 log4cplus 将内容记录在文件中,您可以使用如下文件:

### logs.properties

# root logger

log4cplus.rootLogger=INFO, Main

# specific logger

log4cplus.logger.myloggerINFO, Main
log4cplus.additivity.mylogger=false

# appender that automatically rolls files

log4cplus.appender.Main=log4cplus::DailyRollingFileAppender
log4cplus.appender.Main.Schedule=DAILY
log4cplus.appender.Main.File=logs/mylogs.log
log4cplus.appender.Main.Append=true
log4cplus.appender.Main.MaxBackupIndex=100
log4cplus.appender.Main.MaxFileSize=100KB
log4cplus.appender.Main.layout=log4cplus::PatternLayout
log4cplus.appender.Main.layout.ConversionPattern=%D | %-5.5p | %-20.20c | %m|%n

在您的 C++ 程序中:

PropertyConfigurator config("logs.properties");
config.configure();

要记录内容,请使用提供的宏:

logger = Logger::getInstance("mylogger");

LOG4CPLUS_DEBUG(logger, message);
LOG4CPLUS_WARN(logger, message);
LOG4CPLUS_INFO(logger, message);
LOG4CPLUS_ERROR(logger, message);

请参阅 log4cplus 示例 开始。如果您需要更多信息,请告诉我。

我的 2 美分

编辑:

日志的存储取决于您的附加程序。您可以拥有一个标准文件 (FileAppender) 或一组文件 (RollingFileAppender) 等。这样您就可以通过查看文件来查看旧日志。您还可以使用系统日志,或以编程方式编写自己的附加程序

,这是使用 DailyRollingFileAppender 的一种方式。然后您可以打开特定日期对应的文件并通过读取文件内容来获取您的日志。

另一种方法是编写 log4cplus DBMS 附加程序并使用 SQL 库来读取它们。我知道没有标准 API 可以在 log4cplus 中取回日志。

Well, I'm not sure what you want exactly. The log4cplus documentation give you examples to use it. For example :

BasicConfigurator config;
config.configure();

Logger logger = Logger::getInstance("mylogger");
LOG4CPLUS_WARN(logger, "Hello, World!");

This way you will get a log on the console. If you want to configure log4cplus to log things in a file you can use a file like :

### logs.properties

# root logger

log4cplus.rootLogger=INFO, Main

# specific logger

log4cplus.logger.myloggerINFO, Main
log4cplus.additivity.mylogger=false

# appender that automatically rolls files

log4cplus.appender.Main=log4cplus::DailyRollingFileAppender
log4cplus.appender.Main.Schedule=DAILY
log4cplus.appender.Main.File=logs/mylogs.log
log4cplus.appender.Main.Append=true
log4cplus.appender.Main.MaxBackupIndex=100
log4cplus.appender.Main.MaxFileSize=100KB
log4cplus.appender.Main.layout=log4cplus::PatternLayout
log4cplus.appender.Main.layout.ConversionPattern=%D | %-5.5p | %-20.20c | %m|%n

And in your C++ program :

PropertyConfigurator config("logs.properties");
config.configure();

To log something use the macros provided :

logger = Logger::getInstance("mylogger");

LOG4CPLUS_DEBUG(logger, message);
LOG4CPLUS_WARN(logger, message);
LOG4CPLUS_INFO(logger, message);
LOG4CPLUS_ERROR(logger, message);

See log4cplus examples to start. Tell me if you need more info.

my 2 cents

EDIT:

Well logs are stored depending on your appender. You can have a standard file (FileAppender), or a set of files (RollingFileAppender), etc. This way you see old logs by looking in your files. You can also use system logs, or write your own appender

Programmatically, one way it to use a DailyRollingFileAppender. Then you can open the file corresponding to a specific date and get your logs by reading the file content.

Another way is to write a log4cplus DBMS appender and use an SQL lib to read them. I know of no standard API to get back log in log4cplus.

无可置疑 2024-12-08 10:06:19

是的,有一种方法可以做到这一点,但不是现成的。实现抽象类 Appender 并通过实现附加虚函数来完成您需要的任何操作,还为您所需的功能提供一个接口:

#ifndef STRINGAPPENDER_H
#define STRINGAPPENDER_H

#include <string>
#include <log4cplus/fileappender.h>
#include <log4cplus/config.hxx>
#include <log4cplus/appender.h>
#include <log4cplus/fstreams.h>
#include <log4cplus/helpers/property.h>
#include <log4cplus/helpers/timehelper.h>

/* I derived from FileAppender for i needed access to the stream underneath, 
*  feel free to derive from Appender as well
*/
class StringAppender : public log4cplus::FileAppender {
    public:
        log4cplus::tofstream    & out;
        StringAppender(std::string & name) : log4cplus::FileAppender (name), 
                out(log4cplus::FileAppender::out){}
        virtual void append (const log4cplus::spi::InternalLoggingEvent &event)
        {
            //implement your logic here. You could store events in std::vector
            log4cplus::FileAppender::append(event);
        }
        std::string retrieve_last_message(){/*to implement*/}
        virtual void close (){}
        virtual     ~StringAppender () {}

};
#endif  /* STRINGAPPENDER_H */

在您的客户端代码中:

#include <log4cplus/configurator.h>
#include <log4cplus/logger.h>
#include <StringAppender.h>
int main(){
  std::string filename = "test.log";
  log4cplus::SharedAppenderPtr append_1(new StringAppender(filename));
  log4cplus::Logger log = log4cplus::Logger::getRoot();
  log.addAppender(append_1);

  //reason i wanted the stream to be publicly available
  //i needed it in my library test boost::test
  unit_test_log.set_stream(((StringAppender &)*append_1.get()).out);
  //some testing folows...  
}

Yes, there's a way to do that but not out-of-the-box. Implement the abstract class Appender and do whatever you need there just by implement the append virtual function, also provide an interface for your required functionality:

#ifndef STRINGAPPENDER_H
#define STRINGAPPENDER_H

#include <string>
#include <log4cplus/fileappender.h>
#include <log4cplus/config.hxx>
#include <log4cplus/appender.h>
#include <log4cplus/fstreams.h>
#include <log4cplus/helpers/property.h>
#include <log4cplus/helpers/timehelper.h>

/* I derived from FileAppender for i needed access to the stream underneath, 
*  feel free to derive from Appender as well
*/
class StringAppender : public log4cplus::FileAppender {
    public:
        log4cplus::tofstream    & out;
        StringAppender(std::string & name) : log4cplus::FileAppender (name), 
                out(log4cplus::FileAppender::out){}
        virtual void append (const log4cplus::spi::InternalLoggingEvent &event)
        {
            //implement your logic here. You could store events in std::vector
            log4cplus::FileAppender::append(event);
        }
        std::string retrieve_last_message(){/*to implement*/}
        virtual void close (){}
        virtual     ~StringAppender () {}

};
#endif  /* STRINGAPPENDER_H */

in your cliente code:

#include <log4cplus/configurator.h>
#include <log4cplus/logger.h>
#include <StringAppender.h>
int main(){
  std::string filename = "test.log";
  log4cplus::SharedAppenderPtr append_1(new StringAppender(filename));
  log4cplus::Logger log = log4cplus::Logger::getRoot();
  log.addAppender(append_1);

  //reason i wanted the stream to be publicly available
  //i needed it in my library test boost::test
  unit_test_log.set_stream(((StringAppender &)*append_1.get()).out);
  //some testing folows...  
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文