Logging is an effective tool for all software debugging and positioning issues, and having a good journaling system can help developers quickly locate where the problem may be. A good log system can be arbitrarily controlled according to the user's wishes which log can be output, when it can be output. The same is also possible through the configuration file to control the log output level arbitrarily.
Log4cplus is an excellent open source log library based on C/s + +. Log4cplus has thread-safe, do not need but the heart in a multithreaded state to write the log problem, using flexible, can set the log-level output location through the configuration file, but also can dynamically set the log output level while the program is running, control the output of the log, and control the characteristics of the multiple granularity. By prioritizing the information so that it can be oriented to the whole lifecycle of program debugging, running, testing, and maintenance, you can optionally export the information to the screen, file, NT event log, or even a remote server, and periodically back up the log by specifying a policy. Can meet the needs of most developers on the log system, full-featured.
First, install under Linux
Download the latest Log4cplus source on the official website and install it through the following steps:
1, extract the source code
Tar xvzf log4cplus-x.x.x.tar.gz
2, into the decompression directory installation
CD log4cplus-x.x.x
2.1, configure the compilation options, you can configure with--help parameters to see all configuration options Help
./configure
2.2. Compile and install
Make
Make install
Here I use the default installation path:/usr/local, you can specify the installation path by--prefix=/install/path.
Ii. introduction of Log4cplus content and usage of configuration files
Log4cplus Content Introduction
1. Logger Object
The Logger object has a hierarchy, distinguished by name, with the following code:
In the logger storage mechanism in Log4cplus, all logger are organized through a hierarchical structure, with a root-level logger, which can be obtained in the following ways:
Logger root = Logger::getroot ();
User-defined logger have a name corresponding to them, such as:
Logger test = logger::getinstance ("test");
You can define the child logger of the logger:
Logger subtest = logger::getinstance ("Test.subtest");
Note that the root level of Logger is only obtained through the Getroot method, and Logger::getinstance ("root") obtains its child objects. With these logger, you can set their loglevel separately, for example:
Root.setloglevel (...);
Test.setloglevel (...);
Subtest.setloglevel (...);
2. Priority level
Log4cplus priority is low to High:
Not_set_log_level: Accept the default loglevel, and if a parent logger inherits its loglevel
All_log_level: Open all LOG information output
Trace_log_level: Open trace information output (i.e. All_log_level)
Debug_log_level: Open Debug Information output
Info_log_level: Open Info Information output
Warn_log_level: Open Warning information output
Error_log_level: Open Error information output
Fatal_log_level: Open FATAL Information output
Off_log_level: Turn off all LOG information output
3, the following is the use of configuration file configuration log output level and output location instances:
Using log configuration to initialize log logs
#define INIT_LOG (FilePath) Log4cplus::P ropertyconfigurator::d oconfigure (FilePath)
Set Log Level
#define SET_LOG_LEVEL (Level) Log4cplus::logger::getroot (). Setloglevel (Level)
Define log output
#define LOG_TRACE (Logs) Log4cplus_trace (Log4cplus::logger::getroot (), logs)
#define LOG_DEBUG (Event) Log4cplus_debug (Log4cplus::logger::getroot (), event)
#define LOG_INFO (Logs) Log4cplus_info (Log4cplus::logger::getroot (), logs)
#define LOG_ERROR (Logs) Log4cplus_error (Log4cplus::logger::getroot (), logs)
Profiles (other log level configurations are the same):
Log4cplus.rootlogger=trace, All_msgs, Debug_msgs, Error_msgs, Trace_msgs, info_msgs
#设置日志追加到文件尾
Log4cplus.appender.debug_msgs=log4cplus::rollingfileappender
#设置日志文件大小
Log4cplus.appender.DEBUG_MSGS. maxfilesize=100mb
#设置生成日志最大个数
Log4cplus.appender.DEBUG_MSGS. maxbackupindex=10
#设置输出日志路径
Log4cplus.appender.DEBUG_MSGS. File=/mnt/hgfs/2.linuxfile/8.object/systemdata/log/system.debug
Log4cplus.appender.debug_msgs.layout=log4cplus::P atternlayout
#设置日志打印格式
log4cplus.appender.debug_msgs.layout.conversionpattern=|%d:%d{%q}|%p|%t|%l|%m|%n
Log4cplus.appender.debug_msgs.filters.1=log4cplus::spi::loglevelmatchfilter
#匹配相同日志级别, only the debug log is entered in the file
Log4cplus.appender.debug_msgs.filters.1.logleveltomatch=debug
Log4cplus.appender.debug_msgs.filters.1.acceptonmatch=true
LOG4CPLUS.APPENDER.DEBUG_MSGS.FILTERS.2=LOG4CPLUS::SPI::D enyallfilter
Simple to use:
int main ()
{
Init_log (Log_config_path);
Set_log_level (Log4cplus::all_log_level);
Setsignal ();
Log_debug ("DEBUG");
Log_error ("ERROR");
Log_info ("INFO");
Log_trace ("TRACE");
return 0;
}