Log4cplus is a C + + written open-source log system, the function is very comprehensive, used in their own development of the project will be more professional,:), this article introduced the basic concept of log4cplus, and how to install, configuration.
Introduction # # # #
Log4cplus is an open-source log system written by C + +, formerly known as the LOG4J system written by Java. protected by Apache Software license. The author is Tad E. Smith. Log4cplus has the characteristics of thread-safe, flexible, and multi-granularity control, by prioritizing information so that it can be oriented to the whole life cycle of program debugging, running, testing, and maintenance; You can choose to output information to screens, files, NT event logs, and even remote servers , regular backups of logs by specifying policies, and so on.
# # # download # # #
The latest log4cplus can be downloaded from the following URL http://log4cplus.sourceforge.net This article uses the version: 1.0.4
2. Configuration under Windows
Insert the DSP files for the "Log4cplus_dll class" Project or "Log4cplus_static class" project into your project, or simply put the libraries of the two projects compiled and the directory where the header files are located in your project's search path, if you use a static library, Please add log4cplus_static to the "project/setting/c++" Preprocessor definitions in your project.
Go to the Log4cplus-1.0.4\log4cplus-1.0.4\msvc8 directory and double-click Log4cplus.sln to open the file using Microsoft Visual Studio 2010.
To build a new dynamic library or a static library:
Copy log4cplusSD.lib (Static library) and Log4cplusD.lib + log4cplusD.dll (Dynamic library)
Note: Using static links requires only log4cplusSD.lib, and dynamic linking requires Log4cplusD.lib + Log4cplusD.dll 2 files.
3. Specific configuration
First, copy the Log4cplus header file to the Microsoft Visual Studio 2010 installation under include, such as:
Copy, Log4cplusSD.lib, and Log4cplusD.lib to the Microsoft Visual Studio 2010 installation under LIB, such as:
Copy the Log4cplusD.dll to the bin under the Microsoft Visual Studio 2010 installation, such as:
4. After the environment configuration is complete, we can configure the specific project environment.
We create a console project and configure the following:
1. Use a static link library:
Right-click Project, Properties,
Join Log4cplusSD.lib
Note: To use the static link library, you can delete the Log4cplusD.dll in the bin directory and the Log4cplusD.lib 2 files under the Lib directory.
2. Using the dynamic link library:
Right-click Project, Properties,
Note: To use the dynamic link library, you need the Log4cplusD.dll in the bin directory and the Log4cplusD.lib 2 files in the Lib directory. You can delete the Log4cplusSD.lib file in the Lib directory
5. Write a simple program and test
#include "stdafx.h"
#include <log4cplus/logger.h>
#include <log4cplus/configurator.h>
# Include <iomanip>
using namespace Log4cplus;
int _tmain (int argc, _tchar* argv[])
{
basicconfigurator config;
Config.configure ();
Logger Logger = logger::getinstance ("main");
Log4cplus_warn (Logger, "Hello, world!");
GetChar ();
return 0;
}
Attention:
Log4cplus the "multibyte Character set" used by the official source code, if your project uses the Unicode character set, you can modify the Log4cplus character set to regenerate the corresponding library file.
The following are commonly used packaged test examples:
VLog.h
#pragma once
#ifndef VLOG_H
#define VLOG_H
#include <log4cplus / logger.h>
#include <log4cplus / layout.h>
#include <log4cplus / loglevel.h>
#include <log4cplus / fileappender.h>
#include <log4cplus / consoleappender.h>
#include <log4cplus / helpers / loglog.h>
using namespace log4cplus;
using namespace helpers;
/ **
* Use log4cplus
* @param logfile The file path recorded, such as "main.log"
* @param format format prefix, default% d [% l]% -5p:% m% n
* @param bebug whether to print bebug information, default is true
* @param lv set journal level
* @return
* /
void InitLog4cplus (
const wchar_t * logfile,
const bool console = true,
const bool bebug = true,
LogLevel lv = DEBUG_LOG_LEVEL,
const wchar_t * format = L "% d:% m% n" / *% d% -5p [% c <% l]:% m% n * /
);
/ **
* Get root log
* @return
* /
Logger GetRootLogger (void);
/ **
* Get child logs
* @param child child log name, such as sub; sub.sub1
* @return
* /
Logger GetSubLogger (const wchar_t * sub);
/ **
* Turn off the logging system
* /
void ShutdownLogger (void);
#endif / * VLOG_H * /
VLog.cpp
/ ************************************************* *********************** /
/ * Rules of use: * /
/ * Requires log4cplus dynamic / static link libraries and header files * /
/ * log4cplus.lib + log4cplus.dll is a dynamic link library. log4cplusS.lib is a static link library * /
/ ************************************************* *********************** /
#include "VLog.h"
void InitLog4cplus (const wchar_t * logfile, const bool console, const bool bebug,
LogLevel lv, const wchar_t * format)
/ *
NOT_SET_LOG_LEVEL (-1): Accept the default LogLevel, if there is a parent logger, inherit its
LogLevelALL_LOG_LEVEL (0): open all log information output
TRACE_LOG_LEVEL (0): open trace information output (ie ALL_LOG_LEVEL)
DEBUG_LOG_LEVEL (10000): open debug information output
INFO_LOG_LEVEL (20000): open info information output
WARN_LOG_LEVEL (30000): open warning information output
ERROR_LOG_LEVEL (40000): open error information output
FATAL_LOG_LEVEL (50000): open fatal information output
OFF_LOG_LEVEL (60000): turn off all log information output
* / {
// 0. Log system configuration: set to display debug information
LogLog :: getLogLog ()-> setInternalDebugging (bebug);
// Create screen output Appender, do not use stderror stream, do not use the immediate write mode
SharedAppenderPtr pappender1 (new RollingFileAppender (logfile, 1024 * 1024 * 200, 50, false));
// The name of the generated log file, the maximum file size (minimum 1 * 200 M), the number of files expanded by 50, the immediate write mode is not used
// 2. Instantiate a layout object
// 2.1 create layout layout format
std :: auto_ptr <Layout> playout1 (new PatternLayout (format));
// 3. Attach the layout object to the appender object
// pappender.setLayout (std :: auto_ptr <Layout> layout);
pappender1-> setLayout (playout1);
// 4.Logger: a logger, an entity that saves and tracks changes in the log information of an object. When you need to record an object, you need to generate a logger.
Logger rootLogger = Logger :: getRoot ();
// 5. Attach the appender object to the logger object. If this step is omitted, the standard output (screen) appender object will be bound to the logger.
rootLogger.addAppender (pappender1);
if (console) {
// 1.Appenders: hooks that work closely with the layout to output messages in a specific format to the connected device terminals (such as screens, files, etc.).
SharedAppenderPtr pappender2 (new ConsoleAppender (false, false));
std :: auto_ptr <Layout> playout2 (new PatternLayout (format));
pappender2-> setLayout (playout2);
rootLogger.addAppender (pappender2);
}
// 6. Set the priority of the logger. If you omit this step, all kinds of limited level messages will be recorded.
rootLogger.setLogLevel (lv);
}
Logger GetRootLogger (void) {
return Logger :: getRoot ();
}
Logger GetSubLogger (const wchar_t * sub) {
return Logger :: getInstance (sub);
}
void ShutdownLogger (void) {
Logger :: getRoot (). Shutdown ();
}
Main.cpp
#include "VLog.h"
int _tmain (int argc, _tchar* argv[])
{
Add Log4cplus//Initialize Log4cplus initlog4cplus (L "Main.log");</p><p>//Log object: Test one Log4cplus::logger test1 = Getsublogger (L "test1"); Log object: Test a first Test Log4cplus::logger test1_01 = Getsublogger (L "test1.01");</p><p>//Type logging Log4cplus_debug ( TEST1_01, "Can log like this:" << "Hello" << + << '! '); </p><p>//close log shutdownlogger (); }