Use Apache log4cxx in C ++ to record log posts)

Source: Internet
Author: User
1. Introduction

Log4cxx is a sub-project of Apache logging service, an open-source project. It is a C ++ porting version of log4j, a well-known Java Community, used to provide log functions for C ++ programs, this allows developers to debug and audit the target program.

More information about log4cxx can be obtained from the website http://logging.apache.org of the Apache logging service. The current stable version is 0.9.7. The content of this article and the sample code are based on this version. In addition, the compiling environment of the sample code is Microsoft Visual C ++. NET 2003 in windows.

2. Compile

In this example, vc6.0 is used for implementation, or vs2003 can be used for compilation. Because the release package does not include the compiled code, we need to compile its static library and dynamic library first. In msvc.

| Msvc \ static: This project generates the log4cxx static link library (lib4cxx. lib and lib4cxxs. Lib );

| Msvc \ dll: This project generates the dynamic link library of log4cxx (lib4cxx. dll );

Note: The compilation path cannot contain folders with spaces. Because this is not allowed in Unix systems, you are advised to select a root directory for compilation during compilation.

5. Configure the environment

Create a new project and set the include path and static library path (tool-> Option-> directory) of the VC compiling environment to include the preceding three files.

Note:
In addition, you must add ", Unicode" to "Project-> setting-> C/C ++-> Project preprocessing definition ".

4. Sample Code

[CoDe]

// Log4cpptest. cpp: defines the entry point for the console application.
//

# Include "stdafx. H"
# Include <string>
# Include <log4cxx/logger. h>
# Include <log4cxx/propertyconfigurator. h>
# Include <log4cxx/helpers/exception. h>
Using namespace log4cxx;
Using namespace log4cxx: helpers;

Int main (INT argc, char * argv [])
{
String MSG = _ T ("E: \ log4cpptest \ log4cxx. properties ");
 
Loggerptr rootlogger = logger: getrootlogger ();

Log4cxx: propertyconfigurator: Configure (MSG );

Rootlogger-> Info (_ T ("entering application ."));

// Log4cxx_info (rootlogger, _ T ("He does work "));

Return 0;
}

[/CODe]

Create a text file named log4cxx. properties and enter the following content:

[CoDe]

# Set the root logger to the debug level. The Ca and FA appender are used.
Log4j. rootlogger = debug, CA, Fa
 
# Set appender FA:
# This is a file-type appender,
# Its output file (File) is./output. log,
# The output mode (append) is the overwrite mode,
# The output format (layout) is patternlayout.
Log4j. appender. Fa = org. Apache. log4j. fileappender
Log4j. appender. Fa. File =./output. Log
Log4j. appender. Fa. append = true
Log4j. appender. Fa. layout = org. Apache. log4j. patternlayout
Log4j. appender. Fa. layout. conversionpattern = % d [% T] %-5 p %. 16C-% m % N
 
# Set the appender CA:
# This is a console-type appender.
# The output format (layout) is patternlayout.
Log4j. appender. CA = org. Apache. log4j. leleappender
Log4j. appender. Ca. layout = org. Apache. log4j. patternlayout
Log4j. appender. Ca. layout. conversionpattern = % d [% T] %-5 p %. 16C-% m % N

[/CODe]

Copy log4cxx. DLL to the output directory. In the dynamic connection mode, the application needs to be able to find the library file.

Run the generated file to view the running result.

Note: log4cxx contains a vulnerability in the Code. If log4cxx. Info (*) is used, memory overflow may occur, but rootlogger-> Info (*) is not used.

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.