Introduction
Log4cplus is a C + + ported version of log4j and is a good library of print logs in C + +. It is compared with another C + + log library Log4cxx, the benefit is not dependent on LIBAPR and libaprutil, can be statically linked to the program, easy to deploy.
installation
Tar xvzf log4cplus-x.x.x.tar.gz
CD log4cplus-x.x.x
./configure--prefix=/where/to/install
Make
Make install
Here I take the default installation path:/usr/local
Header files in the/usr/local/include/log4cplus directory, when used to connect to the dynamic library or static library can be,/USR/LOCAL/LIB/LIBLOG4CPLUS.A (static library) or Log4cplus (Dynamic Library).
Configuration Instructions
First look at an instance
Files: log.properties
Log4cplus.rootlogger=error, R
log4cplus.appender.r=log4cplus::D ailyrollingfileappender
Log4cplus.appender.r.file=./log/error.log
log4cplus.appender.r.schedule=hourly
Log4cplus.appender.r.append=true
log4cplus.appender.r.layout=log4cplus::P atternlayout
log4cplus.appender.r.layout.conversionpattern=[%d{%y-%m-%d%h:%m:%s,%q}] [%t]%-5p-%m%n
Log4cplus provides a class "Propertyconfigurator" to read the configuration from a file, the parameter is the file name. Other parameters are default.
The contents of the configuration file are as follows:
1. Select Logger
Logger Logger = Logger::getroot (); This is used to choose Rootlogger.
The others can also be configured with more Logger,logger Logger = log4cplus::logger::getinstance ("test");
In general, it is not necessary.
2. Log Output Level TRACE
The first parameter after the logger name can control the log output, such as in the debug environment, output a variety of debug information, and in the online environment, you only need to output info and various types of error information. Log4cplus supports the following log levels, which can be used to see the CPP code.
Trace Debug Info warn error fatal
In each of these levels, the importance is incremented from the top down. The level in the configuration file masks the log output that is less important than it.
3. Appender Configuration
Each logger can add appender, and the second parameter after logger is Apppender
As this example: Log4cplus.rootlogger=error, R
Appender with the name R
The log output location, log path, layout, and so on can be set on Appender
A Log file name
By configuration item: Log4cplus.appender.r.file=./log/error.log
4. Appender Output Position
(1) Console output
Consoleappender
(2) File output
Fileappender/rollingfileappender/dailyrollingfileappender.
5. Layout
Layout is the control of the log output format
Log4cplus.appender.r.layout=log4cplus::P atternlayout
LOG4CPLUS.APPENDER.R.LAYOUT.CONVERSIONPATTERN=[%D{%Y-%M-%D%H:%M:%S,%Q}] [%t]%-5p-%m%n
There are three types of formats:
1.) Simplelayout is a simple-format layout that adds LogLevel and a "-" before the original information is output.
2.) ttcclayout its format is made up of time, thread Id,logger and NDC.
3.) Patternlayout is a pattern layout with lexical analysis functions, similar to regular expressions. Special predefined identifiers that begin with "%" will produce special formatting information.
The following options are available in the Patternlayout format:
(1) "percent", escaped as%.
(2) "%c", output logger name, such as Test.subtest. You can also control the display hierarchy of the logger name, such as "%c{1}" when output "test", where the number represents the hierarchy.
(3) "%d", showing the local time, for example: "2004-10-16 18:55:45",%d display standard time.
Can pass%d{...} Define a more detailed display format, such as%d{%h:%m:%s} to show hours: minutes: seconds. The predefined identifiers that can be displayed in curly braces are as follows:
%a--to indicate the number of weeks, abbreviations, such as "Fri"
%A--a prayer, such as "Friday."
%b--For months, abbreviations in English, such as "OCT"
%B--For a few months, "October"
%c-Standard date + time format, such as "Sat Oct 16 18:56:19 2004"
%d-Indicates today is the number of this month (1-31) "16"
%H-Indicates when the current time is (0-23), such as "18"
%I-Indicates when the current time is (1-12), such as "6"
%j--Which day is today (1-366), such as "290"
%m-The month of January (1-12), such as "10"
%M--Indicates which minute (0-59) The current moment is, such as "59"
%p-Indicates whether it's morning or afternoon, am or PM
%q-Represents the millisecond portion of the current moment (0-999), such as "237"
%Q--Represents the millisecond portion of the current moment with decimals (0-999.999), such as "430.732"
%s--Indicates how many seconds (0-59) of the current moment, such as "32"
%u – this week is the first week of the year, starting with Sunday (0-53), such as "41"
%w-Indicates the number of weeks, (0-6, Sunday is 0), such as "6"
%W-This week is the first week of the year, starting with Monday (0-53), like "41"
%x-standard date format, such as "10/16/04"
%x-Standard Time format, such as "19:02:34"
%y--two-digit year (0-99), such as "04"
%Y--four-digit year, such as "2004"
%Z--time zone name, such as "GMT"
(4) "%F", output the name of the file where the current logger is located, such as "main.cpp"
(5) "%l", output the file line number where the current logger is located, such as "51"
(6) "%l", output the file name and line number where the current logger is located, such as "main.cpp:51"
(7) "%m", output the original information.
(8) "%n", line break.
(9) "%p", output loglevel, such as "DEBUG"
(Ten) "%t", the thread ID of the output logger, e.g. "1075298944"
(one) "%x", nested the diagnostic context NDC (nested diagnostic context) output, POPs contextual information from the stack, and NDC can differentiate it with the log information (simultaneous) Cross output of different sources.
(12) Format alignment, such as "%-10m" when the left alignment, width is 10, of course, other control characters can also be used in the same way, such as "%-12d", "%-5p" and so on.
This example uses: Log4cplus.appender.r.layout.conversionpattern=[%d{%y-%m-%d%h:%m:%s,%q}] [%t]%-5p-%m%n
instance
Configure Log.properties
Log4cplus.rootlogger=error, R
log4cplus.appender.r=log4cplus::D ailyrollingfileappender
Log4cplus.appender.r.file=./log/error.log
log4cplus.appender.r.schedule=hourly
Log4cplus.appender.r.append=true
log4cplus.appender.r.layout=log4cplus::P atternlayout
log4cplus.appender.r.layout.conversionpattern=[%d{%y-%m-%d%h:%m:%s,%q}] [%t]%-5p-%m%n[%p]
Log4cplus.logger.test=trace, RR
log4cplus.appender.rr=log4cplus::D ailyrollingfileappender
Log4cplus.appender.rr.file=./log/test.log
log4cplus.appender.rr.maxbackupindex=192 #8 *
Log4cplus.appender.rr.schedule=hourly
log4cplus.appender.rr.append=true
log4cplus.appender.RR.layout =log4cplus::P atternlayout
log4cplus.appender.rr.layout.conversionpattern=%p%d{%y-%m-%d%H:%M:%S.%q}:%t%F:% L "%m"%n
Test.cpp
#include <cstdlib> #include <log4cplus/logger.h> #include <log4cplus/configurator.h> #include <
Log4cplus/helpers/loglog.h> #include <log4cplus/helpers/stringhelper.h> using namespace std;
using namespace Log4cplus;
using namespace Log4cplus::helpers;
int main () {propertyconfigurator::d oconfigure (Log4cplus_text ("log.properties"));
Logger Logger = Logger::getroot ();
Trace Log4cplus_trace (Logger, "Trace and get the fingerprint:" << "random integer:" << random ());
Debug Log4cplus_debug (Logger, "This is DEBUG log:" << "random integer:" << random ()); Info Log4cplus_info (Logger, "The information centry ..." << "[1 + 1 =" << 1 + 1 << "]
");
Warn Log4cplus_warn (logger, "Writing warning messages to log ...");
Error Log4cplus_error (logger, "ooooooh, there is an ERROR ...."); Fatal Log4cplus_fAtal (logger, "Oh, my god!
The fatal error occur!!!!!!!!! ");
Logger loggertest = log4cplus::logger::getinstance ("test");
Log4cplus_trace (Loggertest, "anther logger,trace");
Log4cplus_error (Loggertest, "anther logger,error");
return 0; }
Makefile
cc:=g++
cflags:=-G
src:= test.cpp
dest:=client
include_path:=/usr/local/include
lib_path:=/ Usr/local/lib
user_libraries:=log4cplus
#
H_lib_path: = $ (foreach n,$ (Lib_path),-l$ (n))
H_user_ LIBRARIES: = $ (foreach n,$ (user_libraries),-l$ (n))
H_inc_path: = $ (foreach n,$ (include_path),-i$ (n))
objs:= $ (SRC:.CPP=.O)
#
All:tips $ (OBJS) $ (DEST)
tips:
@echo "Begin Compile"
$ (OBJS):%.o:%.cpp
$ (CC) $ (CFLAGS) $ (H_inc_path)-C $?-o $@
$ (DEST): $ (OBJS) $ (CC) $ (CFLAGS) $ (
H_lib_path) $ (h_user_ LIBRARIES) $? -O $@
# clean
:
rm-rf $ (DEST) $ (OBJS)
Under the current directory:
Make, compile the executable client
Build a log directory
./client
Your log's out.
Package
Every time I use Log4cplus_trace, Log4cplus_error certainly uncomfortable, we can do a simple package, please refer to the next log log4cplus package