Use of log4cxx (2)

Source: Internet
Author: User

I have already explained how to use log4cxx for logging. Today the problem is a little complicated. The reason is that a DLL used in the system has used log4cxx. We also want to use log4cxx during the development process, but we don't want to write the logs in the DLL to the same file. The problem arises. How can we print the logs to different files, the DLL adopts the getrootlogger method.

The article in this URL is good, the solution mainly refer to this article: http://www.open-open.com/doc/view/4bf2bd4f517c4044b1f7d4e2d22eccaf

Log4cxx consists of three parts: loggers, appenders, and layouts. these three main components enable collaborative processing to output logs based on actual needs. They define the types and levels of log information, controls the log information composition and log recording output modes when an application is running.

Logger is the core of log4cxx. Logger has a hierarchy of inheritance relationships. The top layer is rootlogger. Each logger has a level, and each logger can append multiple appender. appender represents the log output target. You can use layout to configure the format of each appender.

Logger naming: Keeping logger consistent with its class name is the best naming policy currently known.

An example of logger inheritance:

# Set the root logger to the debug level and use Ca and FA appender

Log4j. rootlogger = debug, CA, Fa

# Set list Logger

Log4j. Logger. List = debug, listapp

# Set list. Voice Logger

Log4j. Logger. List. Voice = info, listvoice, listvoicebak

List is list. voice's father logger, list. voice is the list's son logger. rootlogger is the root logger. In this example, list and list. both voice logger and rootlogger are inherited, but appenders are inherited, and level is not followed. (This solution is to be verified)

 

Check the configuration file: log4cxx. properties.

# Set the root logger to the debug level. The Ca and FA appender are used.
Log4j. rootlogger = debug, fa, CA
# Set. listapp logger to shield level inheritance of logger-list
Log4j. Logger. listapp = debug, listapp

# Set. listapp2 logger to shield level inheritance of logger-list
Log4j. Logger. listapp2 = debug, listapp2

# Shield The appender inheritance of listapp
Log4j. additi.pdf. listapp = false

# Shield The appender inheritance of listapp2
Log4j. additi.pdf. listapp2 = false

 

# Set appender FA: # This is a file-type appender,
Log4j. appender. Fa = org. Apache. log4j. fileappender
Log4j. appender. Fa. immediateflush = true

# Its output file (File) is runlog. log,
Log4j. appender. Fa. File = runlog. Log
Log4j. appender. Fa. layout = org. Apache. log4j. patternlayout

# The output mode (append) is the overwrite mode,
Log4j. appender. Fa. append = false

# The output format (layout) is patternlayout.
Log4j. appender. Fa. layout. conversionpattern = % d {yyyy-mm-dd hh: mm: SS} %-5 p %. 16C-% m % N

 

 

# Set the appender CA:

# This is a console-type appender.
Log4j. appender. CA = org. Apache. log4j. leleappender

# The output format (layout) is patternlayout.
Log4j. appender. Ca. layout = org. Apache. log4j. patternlayout
Log4j. appender. Ca. layout. conversionpattern = % d {yyyy-mm-dd hh: mm: SS} %-5 p %. 16C-% m % N

 

# Set the appender listapp # This is a file-type appender,
Log4j. appender. listapp = org. Apache. log4j. fileappender

# Writing log files now
Log4j. appender. listapp. immediateflush = true

# The output file is listapp. log.
Log4j. appender. listapp. File = listapp. Log

# The output mode (append) is the overwrite mode,
Log4j. appender. listapp. append = false
Log4j. appender. listapp. layout = org. Apache. log4j. patternlayout
Log4j. appender. listapp. layout. conversionpattern = % d {yyyy-mm-dd hh: mm: SS} %-5 p %. 16C-% m % N

 

# Set appender listapp2
Log4j. appender. listapp2 = org. Apache. log4j. fileappender
Log4j. appender. listapp2.immediateflush = true
Log4j. appender. listapp2.file = listapp2.log
Log4j. appender. listapp2.append = false
Log4j. appender. listapp2.layout = org. Apache. log4j. patternlayout
Log4j. appender. listapp2.layout. conversionpattern = % d {yyyy-mm-dd hh: mm: SS} %-5 p %. 16C-% m % N

 

 

The following is the implementation code:

// Testlog4cxx. cpp: defines the entry point of the console application.

//

 

#include "stdafx.h"

#include <iostream>

using namespace std;

#include <log4cxx/logger.h>

#include <log4cxx/propertyconfigurator.h>

 

using namespace log4cxx;

using namespace log4cxx::helpers;

 

int _tmain(int argc, _TCHAR* argv[])

{

 

    PropertyConfigurator::configure("log4cxx.properties");

 

    LOG4CXX_INFO(log4cxx::Logger::getLogger("list5"), "list,log4cxx!");

    LOG4CXX_INFO(log4cxx::Logger::getLogger("list5"), "list,mylog4cxx!");

 

    LOG4CXX_INFO(log4cxx::Logger::getLogger("listApp"), "listApp,log4cxx!");

 

    LOG4CXX_INFO(log4cxx::Logger::getLogger("listApp2"), "listApp2, hello world!");

 

    return 0;

}

 

Place log4cxx.propertiesin the same directory as testlog4cxx.exe. After running the command, three files are generated under the Directory: their file names and content are

Runlog. log:

2012-04-06 21:13:56 info list5-list, log4cxx!
2012-04-06 21:13:56 info list5-list, mylog4cxx!

 

Listapp. log:

21:13:56 info listapp-listapp, log4cxx!

21:13:56 info listapp-listapp timersp. nerrno: 123

 

Listapp2.log:

2012-04-06 21:13:56 info listapp2-listapp2, hello World!

 

It can be seen that all logger is inherited from the appender of rootlogger by default, and generally (level-level permits) will always be in the runlog. log output. If you want to make some exceptions, You need to configure log4j. additi.pdf. appendername = false removes the default inheritance.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.