- Log4. Brief introduction
Log4. is a series of C + + porting versions based on log4j, using the LOG4J schema structure, which currently has the following versions:
1. Log4cxx, is currently to 0.10.0, under the Apache incubation project, can not be compiled independently, dependent on Apr, the compilation is more troublesome, after 2008 has not been updated
2. Log4cplus, currently the latest version is 1.2.0, most recent update 2016-01-16, independent of third-party libraries, download location https://sourceforge.net/projects/log4cplus/files/ log4cplus-stable/1.2.0/
3. Log4cpp, current version 1.1.2, most recent update 2016-08-02, also does not rely on third-party libraries, similar to Log4cplus functions, but simplifies some implementations, adds some features, download location https://sourceforge.net /projects/log4cpp/files/?source=navbar
Because the basic structure is roughly similar, there is no significant difference in performance, the following is the selection of log4cpp to introduce
LOG4CPP Basic Structure Log4cpp is divided into log categories (category), log appends (Appender), log layouts (layout), and log levels (priority) from a large structure. In addition, some help classes are provided to simplify our code setting for the log.
1, category, log output main class, provides a way to output various log levels, and can set the category priority, lower than this priority class is no longer output, can have more than one classification, but each application has a root category, the other categories are the root classification of sub-categories, A bit like Java's single-inheritance structure, the output of the subcategories is also output to the parent category.
// Root classification root
log4cpp :: Category & root = log4cpp :: Category :: getRoot ();
// Subcategory subCat1
log4cpp :: Category & sub1 = log4cpp :: Category :: getInstance ("sub1");
root.error ("test root error");
root.warn ("test root error");
root.info ("test root error");
sub1.fatal ("test sub1 error");
sub1.notice ("test sub1 error");
sub1.debug ("test sub1 error");
2. Appender is mainly used to determine the log output behavior, such as where to output and how to output. Class Appender is loaded by Category. A Category can load multiple Appenders and output to multiple places. An Appender can also be loaded by multiple Categories, but some Appender classes are not set to be mutually exclusive. Multi-thread support for logs is mainly in Do the processing in Category, so don't use one Appender to load into multiple categories.
// OstreamAppender: output to the output stream (output to the console)
// FileAppender: output to a file
// RollingFileAppender: output to a file and can set how much the file exceeds to generate a new file
// DailyRollingFileAppender: generate a new file every day
// NTEventLogAppender: output the log to the Windows event log
// ...
log4cpp :: Appender * appender = new log4cpp :: FileAppender ("test", "D: /log4cppTest/log4cppTest.log");
root.addAppender (appender);
3. Layout, set the log output style, there are BasicLayout, SimpleLayout, and PatternLayout. BasicLayout and SimpleLayout are mainly formed simple log styles, which are basically not used in practice, mainly using PatternLayout to format the log output, Layout is loaded Go to Appender.
// %% outputs a%
//% c outputs the category name
//% d output date
//% m output message
//% n output newline
//% p output Priority
//% d {% Y-% m-% d% H:% M:% S:% l} customize the output date, year, month, day, hour, minute, second, millisecond
//% t thread ID
log4cpp :: PatternLayout * patternLayout = new log4cpp :: PatternLayout ();
patternLayout-> setConversionPattern ("% d {% Y-% m-% d% H:% M:% S.% l}% t [% p]% m% n");
appender-> setLayout (patternLayout);
4. Priority, log4cpp has the following log levels
typedef enum
{
EMERG = 0, // emergency
FATAL = 0, // fatal
ALERT = 100, // alert
CRIT = 200, // critical
ERROR = 300, // error
WARN = 400, // wanning
NOTICE = 500, // notice
INFO = 600, // infomation
DEBUG = 700, // debug
NOTSET = 800
} PriorityLevel;
Each category has a level. Logs below this level and above will not be output. For example, if the category is set to priority, the logs of WARN and NOTICE output using this category will not be output. When Category When Priority is set to NOTSET, the Category inherits the priority of the parent Category; each Appender can also set its threshold. When the value of the priority of the output log is greater than the threshold, the Appender will not output this log
// Code settings
root.setPriority (log4cpp :: Priority :: DEBUG);
appender-> setThreshold ()
1. The help class, log4cpp mainly provides the following classes to simplify the code settings: SimpleConfigurator, BasicConfigurator, PropertyConfigurator, among which SimpleConfigurator and BasicConfigurator, mainly use the code, some simple default configuration, and PropertyConfigurator, through the configuration file The way to configure the log is also the most commonly used. The following is the format of the configuration file:
#The file name is logTest.Property
#Configure the root category's Priority to DEBUG and Appender to rootAppender
log4cpp.rootCategory = DEBUG, rootAppender
# Configure the priority of Category sub1 as DEBUG and Appender as A1
log4cpp.category.sub1 = DEBUG, A1
#Configure rootAppender as FileAppender
log4cpp.appender.rootAppender = FileAppender
#The configuration log file is named test1.log and is stored in the root directory of drive D.
log4cpp.appender.rootAppender.fileName = D: /test1.log
#Configure layout as PatternLayout
log4cpp.appender.rootAppender.layout = PatternLayout
#Set log output style
log4cpp.appender.rootAppender.layout.ConversionPattern =% d {% Y-% m-% d% H:% M:% S.% l}% t [% p]% m% n
#Configure A1 as RollingFileAppender
log4cpp.appender.A1 = RollingFileAppender
#The configuration log file is named test2.log and is stored in the root directory of drive D.
log4cpp.appender.A1.fileName = D: /test2.log
#Configure the log file cannot exceed 1M
log4cpp.appender.A1.maxFileSize = 1024 * 1024
#The log file stores up to 3 files, and the old ones will be deleted
log4cpp.appender.A1.maxBackupIndex = 2
#Set log style
log4cpp.appender.A1.layout = PatternLayout
log4cpp.appender.A1.layout.ConversionPattern =% d {% Y-% m-% d% H:% M:% S.% l}% t [% p]% m% n
// Code setting configuration file
log4cpp :: PropertyConfigurator :: configure (logTest.Property);
Code usage
under windows
After obtaining the source code, find the msvc10 directory in the source code directory, use VS2010 to open the msvc10.sln solution file, compile the source code to obtain log4cpp.DLL and log4cpp.lib; there is an include folder in the source code directory, copy the entire include to us Under the project directory, add include to the additional include directory in the project properties (QT project is added to INCLUDEPATH), then it can be used normally.
linux environment
1. Download log4cpp-1.1.1.tar.gz
2. Installation: First drag log4cpp-1.1.1.tar.gz into the user's home directory (~),
Then perform the following steps:
$ tar zxvf log4cpp-1.1.1.tar.gz
$ cd ~ / log4cpp /
$ ./configure
$ make
$ make check
$ sudo make install
It has been successfully installed here.
The default lib library path is: / usr / local / lib /
Default header file location: / usr / local / include / log4cpp
3. Use:
3.1 When compiling the CPP file using the log4cpp library, you must add the library file to successfully compile and pass, as shown in the following example
$ G ++ log4test.cpp -llog4cpp -lpthread
3.2 At run time, if the log4cpp library file is missing, it means that the log4cpp dynamic library cannot be found. The following settings are required
Log in to the terminal as an administrator and perform the following operations:
A. $ Sudo vim /etc/ld.so.conf
B. At the end of the opened file, add a new line to the path of the dynamic library log4cpp (here is / usr / local / lib), then save and exit
Execute the command ldconfig to make the setting effective.
C. $ Sudo ldconfig // Update the cache information of the library file
Test case
#include <stdio.h>
#include <time.h>
#include <iostream>
#include <boost / circular_buffer.hpp>
#include <thread>
#include <mutex>
#include <vector>
#include <stdio.h>
#include <log4cpp / Category.hh>
#include <log4cpp / FileAppender.hh>
#include <log4cpp / SimpleLayout.hh>
#include "log4cpp / PropertyConfigurator.hh"
using namespace std;
std :: mutex g_mutex; // protects g_i
// 1. Whether circular_buffer needs to be locked under multi-thread
// ---- thread1 write
// ---- thread2 read
// ---- thread3 write
boost :: circular_buffer <int> cb (3);
void thread1_foo ()
{
int i = 0;
while (i ++ <1000)
{
cb.push_back (i);
{
lock_guard <std :: mutex> lock (g_mutex);
cout << "thread1 write:" << i << endl;
}
}
}
void log_test ()
{
try
{
log4cpp :: PropertyConfigurator :: configure ("./ log4cpp.conf");
}
catch (log4cpp :: ConfigureFailure & f)
{
std :: cout << "Configure Problem" << f.what () << std :: endl;
}
log4cpp :: Category & root = log4cpp :: Category :: getRoot ();
log4cpp :: Category & sub3 = log4cpp :: Category :: getInstance (std :: string ("sub3"));
for (int i = 0; i <100; i ++)
{
sub3.fatal ("some test");
}
}
void thread2_foo ()
{
int i = 0;
while (i ++ <2000)
{
if (cb.empty ())
{
continue;
}
int a = cb [0];
cb.pop_front ();
{
lock_guard <std :: mutex> lock (g_mutex);
cout << "thread2 read:" << a << endl;
}
}
}
#define LOGFILE "./test.log"
int main () {
/ * Setting up Appender, layout and Category * /
// log4cpp :: Appender * appender = new log4cpp :: FileAppender ("FileAppender", LOGFILE); // The first parameter is the name of appender, the second is the name of the log file
// log4cpp :: Layout * layout = new log4cpp :: SimpleLayout ();
// // log4cpp :: Layout * layout = new log4cpp :: BasicLayout ();
// log4cpp :: Category & category = log4cpp :: Category :: getInstance ("abc");
//
// appender-> setLayout (layout);
// category.setAppender (appender);
// category.setPriority (log4cpp :: Priority :: INFO);
//
// / * The actual logging * /
// category.info ("This is for tracing the flow");
// category.notice ("This is to notify certain events");
// category.warn ("This is to generate certain warnings");
log_test ();
return 0;
}
g ++ -std = c ++ 11 -I / usr / local / include -I / users / jiangxf / 3rdParty / boost -o log_test log_test.cpp liblog4cpp.a
# Define 3 category sub1, sub2, sub3
# Among them, the additivity property of sub2 and sub3 is set to false; the additivity property of sub1 is set to true by default
rootCategory = DEBUG, rootAppender
category.sub1 =, A1
category.sub2 = INFO, A2
additivity.sub2 = false
category.sub3 = ERROR, A3
additivity.sub3 = false
# Define the rootAppender type and layout properties, BasicLayout is used here
appender.rootAppender = org.apache.log4cpp.ConsoleAppender
appender.rootAppender.layout = org.apache.log4cpp.BasicLayout
#Define the attributes of A1, SimpleLayout is used here
appender.A1 = org.apache.log4cpp.FileAppender
appender.A1.fileName =. / log / A1.log
appender.A1.layout = org.apache.log4cpp.SimpleLayout
#Define the attributes of A2, PatternLayout is used here
appender.A2 = org.apache.log4cpp.ConsoleAppender
appender.A2.layout = org.apache.log4cpp.PatternLayout
appender.A2.layout.ConversionPattern = The message ‘% m’ at time% d% n
#Define A3 attributes
# appender.A3 = org.apache.log4cpp.RollingFileAppender
appender.A3 = org.apache.log4cpp.ConsoleAppender
appender.A3.fileName =. / log / A3.log
appender.A3.maxFileSize = 50
appender.A3.maxBackupIndex = 3
appender.A3.backupPattern =% Y-% m-% d
appender.A3.layout = org.apache.log4cpp.PatternLayout
# appender.A3.layout.ConversionPattern =% d {% Y-% m-% d% H:% M:% S} [% p]: [% c]% m% n
appender.A3.layout.ConversionPattern =% d,% p,% m% n
#% c sub3
#% p fatal
#% m log content
#% n carriage return and line feed
log4cpp learning
http://blog.csdn.net/liuhong135541/article/category/1496383
Use of log4cpp library in C ++