How to retrieve log entries using log4cplus?
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.
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.
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...
}
精彩评论