Compilation and use of Log4cxx in Windows environment (reproduced)

Source: Internet
Author: User

1. Introduction Log4cxx is one of the sub-projects of the Open source project Apache Logging Service and is a C + + ported version of the Java community's famous log4j, which provides logging capabilities for C + + programs so that developers can debug and audit the target program. More information about Log4cxx can be obtained from the website of the Apache Loggin service http://logging.apache.org. The current latest version is 0.10.0, and the sample code is based on this version.

2. Get package can get source code from official website (http://logging.apache.org/log4cxx/index.html), download version is apache-log4cxx-0.10.0, unzip to suitable directory after download is done

3. the original package of the compilation download does not contain the compiled development library, it needs to compile the build itself, this article compiles the source file using vc.net2003, the following package is required for compiling:

Apr-1.2.11-win32-src.zip and Apr-util-1.2.10-win32-src.zip

The two compression packages are as follows:

Http://archive.apache.org/dist/apr/apr-1.2.11-win32-src.zip

Http://archive.apache.org/dist/apr/apr-util-1.2.10-win32-src.zip

After the download is complete, the two files are extracted separately into the same directory as the source code above. Then follow the steps below to compile:

A. Modify the file name of Apr-1.2.11-win32-src.zip after decompression to: Apr

B. Modify the file name of Apr-util-1.2.10-win32-src.zip after decompression to: Apr-util

C. Enter the DOS window via the Vc.net2003 DOS tool, where you can enter CMD directly in the command bar, but for security purposes, enter the DOS interface from the. net2003 toolbar. D. Use the CD command to enter the apache-log4cxx-0.10.0 directory

E. Enter the Configure command to execute the configure.bat batch file

F. Enter the Configure-aprutil command to execute the configure-aprutil.bat batch file.

Note: Due to the use of the SED command under Linux in the Configure-aprutil.bat file, this step may have errors similar to those found in the SED command, due to compilation in the Windows environment. The following two methods can be used to resolve: 1. Download an sed for Windows tool; 2. Use Cygwin; However, I strongly recommend the second method because some SED for Windows tools have bugs. For information on how to install Cygwin in a Windows environment, see the post-name description.

G. Use vc.net2003 to open the LOG4CXX.DSW project under the Projects folder, set the Log4cxx project as the startup project, and then compile

H. After compiling successfully, you can see LIB and DLL files under Projects's Debuge or Release folder.

4. There are two ways to set up the environment for a project environment: through the. net2003 toolbar or by using the Project Property bar configuration, here is only the second one, this article will show you how to set up the environment using the second method through the following example program

5. Instance code A. Open the. net2003 and create a blank Win32 console project, assuming the project is named Test

b Add a CPP file with the file name casually named

C Right-click on the test project, select Properties, then add the additional library directory to the C + + TAB and note that the directory is: /Apache-log4cxx-0.10.0\src\main\include

D Add "Log4cxx.lib" to "Additional dependencies" in the linker Input tab of the property

E Enter the following test code in the CPP file:

#include <log4cxx/logger.h>#include<log4cxx/PropertyConfigurator.h>using namespacestd;using namespaceLog4cxx;intMainintargcChar*argv[]) {stringtrace ="FA";stringproperty ="./log.properties";   Log4cxx::P ropertyconfigurator::configure (property); Loggerptr Logger=Logger::getlogger (trace); Logger->info (("How do I use ?")); Log4cxx_info (Logger, ("you say hello."));  for(inti =0; I <2; i++) {log4cxx_debug (logger,"DEBUG"); Log4cxx_info (Logger,"INFO"); Log4cxx_warn (Logger,"WARN"); Log4cxx_error (Logger,"ERROR"); }return 0; }

F Create a new text file named Log4cxx.properties, and type the following:

# set Root logger to DEBUG level, using CA and FA two x Appender log4j.rootlogger=debug, CA, FA

To set #对Appender fa:

# This is a file type of Appender,

# Its output files (file) are./output.log,

# Output mode (for cover mode,

# output format (layout) is

Patternlayout Log4j.appender.fa=org.apache.log4j.fileappender

Log4j.appender.fa.file=./output.log

Log4j.appender.fa.append=false

Log4j.appender.fa.layout=org.apache.log4j.patternlayout

log4j.appender.fa.layout.conversionpattern=%d [%t]%-5p%.16c-%m%n

#对Appender CA Settings

# This is a console type of Appender

# output format (layout) is

Patternlayout Log4j.appender.ca=org.apache.log4j.consoleappender

Log4j.appender.ca.layout=org.apache.log4j.patternlayout

log4j.appender.ca.layout.conversionpattern=%d [%t]%-5p%.16c-%m%n G.

Copy the compiled log4cxx.lib and Log4cxx.dll and the above log4cxx.properties files to the test engineering directory H. Compile and run the file, you can see the console output related logs, and in the project directory will get a output.log log file

6. The above log4cxx.properties file is a configuration file for Log4xxx, which is used primarily to set information about how to generate log files and log file formats, as described in the following documentation for configuration files:

7. Cygwin installation of the Cygwin can be described below. Assuming the installation directory is D:\cygwin, you need to set the environment variables after successful installation, you can set the environment variables as follows:

A Right-click My Computer, properties, advanced environment variables

b Select the path variable in the system variable below and add D:\cygwin\bin to the last face

Compilation and use of Log4cxx in Windows environment (reproduced)

Related Article

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.