Using the Log4cplus tutorial under Windows

Source: Internet
Author: User
Download Log4cplus
The latest log4cplus can be downloaded from the following Web site http://sourceforge.net/projects/log4cplus/files/log4cplus-stable/
The version used in this article is: 1.1.3
Compiling the build DLL
After downloading, unpack, open Log4cplus.sln file, select Log4cplus project, right-"Properties-" Configuration Properties-"character set, select the Unicode character set, as shown in the following figure." (The Log4cplus character set is used by default, and the VS New Project uses the Unicode character set by default, and if you do not modify this here, the newly created project will need to be manually modified to use the generated DLL to modify the new item as a multibyte character set, otherwise it would be an error.)
Then, compile the project to generate Log4cplusD.dll and log4cplusD.lib files in Msvc10\win32\bin. In the debug directory, if you use a static library to compile the following Log4cpluss project, this article uses the form of a dynamic link library.
Using DLLs
Create a new solution and project, copy the Include folder in the download file to the new solution folder, and add the newly copied include folder path to the general additional include directory in the project-Properties-C + +-, as shown in the following figure
To copy the Log4cplusD.dll and log4cplusD.lib files that you just generated into the solution, I created a new Lib folder and added the path to the General additional library directory in item-property-linker-, as shown in the following illustration
Then continue to select Item-"Properties-" linker-"input, add log4cplusD.lib in additional dependencies as shown in the following figure
Finally, add path= to the path of your Lib folder in the project-"Properties-" debugging environment, as shown in the following figure
Of course, you can also directly put include and LIB, DLLs in the appropriate directory under the development environment system, so that you do not need to configure the above several paths, only need to configure the dependencies, the compiler can also find these files.
Programming test
Here directly using the example of the Fileappender_test code, my project is currently used to log to the file, testing the next no problem, the other did not test, the following is the code
#include <log4cplus/logger.h> #include <log4cplus/fileappender.h> #include <log4cplus/layout.h> # Include <log4cplus/ndc.h> #include <log4cplus/helpers/loglog.h> #include <log4cplus/loggingmacros.h

> Using namespace Log4cplus;


const int loop_count = 20000;
    int main () {log4cplus::initialize ();
    Helpers::loglog::getloglog ()->setinternaldebugging (true);
    Sharedappenderptr append_1 (New Rollingfileappender (Log4cplus_text ("Test.log"), 5*1024, 5));
    Append_1->setname (Log4cplus_text ("a");
    Append_1->setlayout (std::auto_ptr<layout> (New Ttcclayout ()));

    Logger::getroot (). Addappender (Append_1);
    Logger root = Logger::getroot ();
    Logger test = logger::getinstance (Log4cplus_text ("Test"));

    Logger subtest = logger::getinstance (Log4cplus_text ("test.subtest"));
        for (int i=0; i<loop_count; ++i) {ndccontextcreator _context (log4cplus_text ("LOOP")); Log4cplus_debug (subtest, "EnterinG Loop # "<< i");
return 0;
 }

Finally compiled to run, if the run appears to be unable to find the DLL problem, and then copy a Lib and DLL to your generated EXE directory can be resolved. The result of the operation is five files test.log,test.log.1~test.log.5 under the project path, which can be opened to see the contents inside.

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.