First, Introduction
Almost every large application contains its own logging or tracing API. Inserting a log statement into your code is a low-tech debgging method. But it may also be the only way, because debuggers is not always useful.
Apache Log4cxx is a log framework for C + + following Apache log4j. Apache Log4cxx uses Apache portable runtime as most of the platform-related code that can be used on any platform that supports Apr.
Official website: http://logging.apache.org/log4cxx/
Second, installation
Log4cxx relies on two other Apache libraries: Apache Portable Runtime (APR) and Apache Portable Runtime Utility (apr-util), so install these 2 libraries first.
1. Download the required source code package
[Email protected] ~]# Wget-c-T 0 http://apache.dataguru.cn/logging/log4cxx/0.10.0/apache-log4cxx-0.10.0.tar.gz[[ Email protected] ~]# Wget-c-T 0 http://apache.fayea.com/apr/apr-1.5.2.tar.bz2[[email protected] ~]# wget-c-T 0/http apache.fayea.com/apr/apr-util-1.5.4.tar.bz2
2. Install Apr
[Email protected] ~]# tar xvf apr-1.5.2.tar.bz2[[email protected] apr-1.5.2]#./configure--prefix=/usr/local/apr[[ Email protected] apr-1.5.2]# make && make install
3. Installing Apr-util
[Email protected] ~]# tar xvf apr-util-1.5.4.tar.bz2[[email protected] apr-util-1.5.4]#./configure--prefix=/usr/ Local/apr-util--with-apr=/usr/local/apr/[[email protected] apr-util-1.5.4]# make && make install
4. Installing Log4cxx
[[email protected] ~]# tar xvf apache-log4cxx-0.10.0.tar.gz[[email protected] apache-log4cxx-0.10.0]# ./configure --prefix=/usr/local/log4cxx --with-apr=/usr/local/apr / --with-apr-util=/usr/local/apr-util/ --with-charset=utf-8 --with-logchar=utf-8 #设置成utf-8 Solving Chinese garbled problem [[email protected] apache-log4cxx-0.10.0]# makeinputstreamreader.cpp:66: error: ' Memmove ' was not declared in this scope# here because the source code does not contain the corresponding header file error, add some header files in the source code file is good [[email protected] apache-log4cxx-0.10.0]# vim src/main/cpp/inputstreamreader.cpp #include <cstring>[[email protected] apache-log4cxx-0.10.0]# vim src/main/cpp/socketoutputstream.cpp #include <cstring>[[ email protected] apache-log4cxx-0.10.0]# vim src/examples/cpp/console.cpp#include <cstring> #include <cstdio> #这里重新开始编Translation [[email protected] apache-log4cxx-0.10.0]# make[[email protected] Apache-log4cxx-0.10.0]# make install
Third, perfect installation
There are 3 directories under the installation directory
[Email protected] ~]# ls/usr/local/log4cxx/include #头文件lib #库文件share #帮助文件
#让系统可以找到log4cxx的库文件 [[email protected] ~]# Vim/etc/ld.so.conf.d/log4cxx.conf/usr/local/log4cxx/lib[[email protected ] ~]# ldconfig-v | grep log4cxx/usr/local/log4cxx/lib:liblog4cxx.so.10-liblog4cxx.so.10.0.0# allows the system to find log4cxx header files [email protected ] ~]# ln-sv/usr/local/log4cxx/include/usr/include/log4cxx '/usr/include/log4cxx ', '/usr/local/log4cxx/include ' #启动httpd服务即可以访问帮助页面了 [[email protected] ~]# cp-a/usr/local/log4cxx/share/log4cxx/html/*/var/www/html/
The Help file is a website and this help file is very useful
650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M01/6F/23/wKioL1WTLgjQaSJ8AAWe27-30uw336.jpg "title=" 1.png " alt= "Wkiol1wtlgjqasj8aawe27-30uw336.jpg"/>
Iv. common classes and inheritance relationships in Log4cxx
1. Layouts
This class is used to format the output format of the log, can be appended to the appenders layouts to complete this function.
650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M01/6F/B9/wKioL1Wm8saSTzitAADKqchbF0E651.jpg "title=" 3.png " alt= "Wkiol1wm8sastzitaadkqchbf0e651.jpg"/>
Format Symbol Description:
%p: The priority of the output log information, which is debug,info,warn,error,fatal.
%d: the date or time of the output log time, the default format is ISO8601, or the format can be specified later, such as:%d{yyyy/mm/dd hh:mm:ss,sss}.
%r: The number of milliseconds to output the log information from the application startup to output.
%t: Outputs the name of the thread that generated the log event.
%l: The location of the output log event, equivalent to the%c.%m (%f:%l) combination, including the class name, method, file name, and number of rows in the code. For example: Test. Testlog4j.main (testlog4j.java:10).
%c: The output log information belongs to the class, which is usually the full name of the class.
%M: Outputs the method name that produces the log information.
%F: The name of the file where the output log message was generated.
%l: The line number in the output code.
%M: The specific log information specified in the output code.
%n: Output a carriage return line break, the Windows platform is "\ r \ n" and the UNIX platform is "\ n".
%x: The NDC (nested diagnostic environment) associated with the output and current line threads, especially for multi-client multithreaded applications such as Java Servlets.
%x: Output and current line threads associated MDC, such as%x{clientnumber}, where the Clientnumber is the MDC key.
Percent: Outputs a "%" character.
In addition, you can also add modifiers between% and format characters to control their minimum length, maximum length, and text alignment. Such as:
1) C: Specify the name of the output category, the minimum length is 20, if the category name length is less than 20, the default is the right alignment.
2)%-20c: "-" indicates left alignment.
3)%.30c: Specify the name of the output category, the maximum length is 30, if the category name length is greater than 30, will be the left more than the character of the cut off, but less than 30 will not fill the space.
2. Appenders
This class is used to output logs to different objects, such as console, files, syslog, etc.
650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M02/6F/AF/wKioL1WlpRuy3iWiAAJd4mAIXWg351.jpg "title=" 2.png " alt= "Wkiol1wlpruy3iwiaajd4maixwg351.jpg"/>
The Dailyrollingfileappender format is as follows:
1) '. ' YYYY-MM: Monthly
2) '. ' YYYY-WW: Weekly
3) '. ' YYYY-MM-DD: Every day
4) '. ' Yyyy-mm-dd-a: Two times a day (12 points and 24 points)
5) '. ' YYYY-MM-DD-HH: Per hour
6) '. ' YYYY-MM-DD-HH-MM: Per minute
3. Logger
A) naming rules for logger
The name of the logger is case-sensitive and follows the following rules:
For example, there are 3 logger, their names are "com", "Com.51cto", "com.51cto.www", then "com" is "com.51cto" the parent logger, "Com.51cto" is "com.51cto.www" The parent logger. The child logger will get property inheritance from the parent logger.
b) Level of logger
The loggers component is divided into six levels in this system: TRACE < DEBUG < INFO < WARN < ERROR < FATAL.
It is important to understand that these six levels are sequential and are used to specify how important this log information is, log4j has a rule:
only the output level is not lower than the set level of log information, assuming that the loggers level is set to info, the info, WARN, error, and fatal level log information will be output, and the level is lower than info debug will not output.
Get a loggerlog4cxx::loggerptr logger named "com" (Log4cxx::logger::getlogger ("com"));//Set the level to info level logger-> SetLevel (Log4cxx::level::getinfo ());//This request is valid because info < Warnlog4cxx_warn (logger, "low fuel level."); /This request is not valid because of DEBUG < Infolog4cxx_debug (logger, "starting search for Nearest"); /Get a loggerlog4cxx::loggerptr Ctologger named "Com.51cto" (Log4cxx::logger::getlogger ("Com.51cto"));//ctologger's name is " Com.51cto ", so it will inherit rank from logger that is named" com "//So this request is valid because INFO >= infolog4cxx_info (Barlogger. "Located nearest gas station.") This request is not valid because of DEBUG < Infolog4cxx_debug (Barlogger, "Exiting gas station Search")
c) Logger and root logger
Use the static function Log4cxx::logger::getrootlogger to get the root Logger pointer, the root Logger has 2 properties:
1. Whether you fall or not, root logger always exists in this way.
2. Root logger is an anonymous object
Use the static function Log4cxx::logger::getlogger to get the Logger pointer. call the GetLogger method with the same name and get the same logger pointer
Log4cxx::loggerptr x = Log4cxx::logger::getlogger ("Wombat"); Log4cxx::loggerptr y = Log4cxx::logger::getlogger (" Wombat ");
Here x and Y are 2 references to the same smart pointer
d) additivity Properties
The additivity property is used to control whether logger inherits the parent logger property , depending on the following test:
#include "log4cxx/logger.h" #include "log4cxx/basicconfigurator.h" using namespace Log4cxx;using namespace Log4cxx:: Helpers;int Main (int argc, char **argv) {loggerptr logger (Logger::getlogger ("Test")); Basicconfigurator::configure (); Log4cxx_info (Logger, "basicconfigurator test") return 0;}
#编译并执行 [[email protected] ~]# g++ test.cpp-o test-wall-o3-g3-l/usr/local/log4cxx/lib/-i/usr/include/log4cxx/-llog4 cxx [[email protected] ~]#./test0 [0x7f7088970720] INFO test null-basicconfigurator test
#include "Log4cxx/logger.h" #include "Log4cxx/basicconfigurator.h" #include <log4cxx/ fileappender.h> #include <log4cxx/simplelayout.h> #include <log4cxx/helpers/pool.h> Using namespace log4cxx;using namespace log4cxx::helpers;int main (INT&NBSP;ARGC, &NBSP;CHAR&NBSP;**ARGV) { basicconfigurator::configure (); loggerptr logger (Logger::getlogger ("Test")); loggerptr ttlogger (Logger::getlogger ("Test.tt")); //"LogFile" is the output file name, True is open append mode fileappenderptr fileappender = new fileappender (LAYOUTPTR ( New simplelayout ()), "LogFile", &nbsP;true); pool p; fileappender->activateoptions (P); ttlogger-> Addappender (Appenderptr (fileappender)); ttlogger-> Setadditivity (false); log4cxx_info (logger, " Basicconfigurator test ") log4cxx_info (ttlogger, ") Basicconfigurator test.tt "); return 0;}
According to our naming rules above, "Test.tt" should inherit the "test" consoleappender and then add a fileappender, all of which should have 2 output targets (one is the console and the other is the file). However, when Additivity is set to False, Ttlogger no longer inherits the attribute, and he only has his own fileappender output.
The default value of Additivity is true, you can comment out the setadditivity and compare the output.
#Additivity设置为false时的输出 [[email protected] ~]#./test1 [0x7f4e0a0dc720] INFO test null-basicconfigurator Test[[email Pro Tected] ~]# cat logfile Info-basicconfigurator test.tt#additivity is set to True when output [[email protected] ~]#./test0 [0x7f1950161 720] Info Test null-basicconfigurator test0 [0x7f1950161720] info test.tt null-basicconfigurator test.tt[[email Protec Ted] ~]# cat logfile info-basicconfigurator test.ttinfo-basicconfigurator test.tt #这里可以看到追加了一行
Description
In log4cxx.h see such a macro definition
#define LOG4CXX_PTR_DEF (T) typedef log4cxx::helpers::objectptrt<t> t# #Ptr
So we know that the fileappenderptr is the log4cxx::helpers::objectptrt<fileappender> smart pointer , So the new memory in the example above is not delete, but it is automatically freed by a smart pointer.
There is no place to write wrong, but also to correct! The next one will be to write a simple class based on Log4cxx, and the application ^_^
This article is from "My Favorite Technology" blog, declined reprint!
Log4cxx configuration using (i)