log4cplus: A useful logging module. compile and use

log4cplus log

Introduction

log4cplus is a C++ version of the logging library, which is an open source implementation of the Apache organization’s log4j logging library. log4cplus provides a flexible, high-performance logging system that supports multithreading, localization, and hierarchical structures.

The main features of log4cplus are as follows:

  • Flexibility: log4cplus provides a flexible configuration mechanism that can dynamically modify configuration information such as log levels, trackers, and Appenders at runtime. At the same time, log4cplus provides a more highly customized configuration through inheritance structures, filters, loggers and other mechanisms.
  • High performance: log4cplus uses multi-threading technology, which can efficiently handle a large number of logging requests, and supports asynchronous logging, reducing the impact of logging on application performance.
  • Multi-platform support: log4cplus can run across platforms and can be used on Windows, Linux, Unix and other platforms.
  • Multi-language support: log4cplus supports localization, it can output log information in different languages.

Using log4cplus, we can use different levels of logging to control the output of log information, which helps us quickly locate and fix problems. In addition, log4cplus also supports multiple loggers, allowing us to control and output log information of different modules differently.

Use directly

You can download log4cplus from the official website of log4cplus, the website address is: http://log4cplus.sourceforge.net.

On the website you can find the latest version of log4cplus along with related documentation and sample code. The method of downloading log4cplus is as follows:

  1. Open the official website of log4cplus: http://log4cplus.sourceforge.net.
  2. On the “Downloads” page, select the version suitable for your system to download. You can download source code or precompiled binaries.
  3. Unzip the downloaded file, and follow the documentation to install and configure it.
  4. If you need to use the extended functions of log4cplus, you also need to download the corresponding extended library.

Once log4cplus is installed, you can start using it. Reference the relevant header files in the code and use the API provided by log4cplus for logging

Compile

  1. Preparation
    Domestic mirror: https://gitcode.net/mirrors/log4cplus/log4cplus
    github address: https://github.com/log4cplus/log4cplus
    Note that submodules need to be updated, catch, threadpool

  2. cmake under windows

    Points to pay attention to in ps, 1. Uncheck the macro WITH_UNIT_TESTS 2. Fill in your own installation path
    Here I use vs2019, x64

  3. sln project
    Find log4cplus.sln, it is recommended to give all the current permissions (I have encountered that the folder cannot be created due to insufficient permissions).

    By the way, some people said before that the project requires a Unicode character set, and the projects generated by cmake all meet the requirements.
    Direct compilation – the lib directory is in src/Release or Debug, and the dynamic library is in the bin directory

Add log4cplus to the project

  1. Right-click the vs project INSTALL, it will be generated under the path configured on the previous cmake

    You can subdivide debug and release on this basis
  2. Add lib and include
 Add logModule\lib to the library directory under the VC directory and add .\logModule\include to the include directory
//load the library
#ifdef_WIN64
#ifdef _DEBUG
#pragma comment(lib,"logModule/lib/log4cplusUD.lib")
#else
#pragma comment(lib,"logModule/lib/log4cplusU.lib")
Project: **ISO C++20 Standard (/std:c++20)**, c++ Language Standard: **ISO C++20 Standard (/std:c++20)**
  1. specific usage
    In fact, there are many references on the Internet, which can be configured in configuration files or directly in the code.

The basic usage steps of log4cplus are as follows:

  • Import header files: include log4cplus/logger.h and log4cplus/configurator.h header files.
  • Initial configuration: Call the log4cplus configurator to configure when the program is initialized. As follows:
 #include <log4cplus/logger.h>
#include <log4cplus/configurator.h>
    log4cplus::initialize();
    log4cplus::BasicConfigurator::doConfigure();

BasicConfigurator is used here for basic configuration, and you can also use XMLConfigurator or PropertyConfigurator for more flexible configuration.

  • Create a logger: Create a logger using the getLogger() method. As follows:
 #include <log4cplus/logger.h>
log4cplus::Logger logger = log4cplus::Logger::getInstance(LOG4CPLUS_TEXT("main"));
  • Logging: Call the method of the logger, such as DEBUG, INFO, WARN, ERROR and FATAL and other logging methods. As follows:
 LOG4CPLUS_DEBUG(logger, "This is a debug message.");
    LOG4CPLUS_INFO(logger, "This is an info message.");
    LOG4CPLUS_WARN(logger, "This is a warning message.");
    LOG4CPLUS_ERROR(logger, "This is an error message.");
    LOG4CPLUS_FATAL(logger, "This is a fatal message.");
  • You can also use printf-style formatting to add parameters when recording logs, for example:
 LOG4CPLUS_DEBUG_FMT(logger, "This is a debug message with number:%d.", 123);
  • Close the log system: call the shutdown() method of log4cplus to close the log system at the end of the program.
 log4cplus::Logger::shutdown();
  • Custom configuration: If you need custom configuration, such as output to a file or use other Appenders, you can use PropertyConfigurator or XMLConfigurator for configuration. For example:
 log4cplus::PropertyConfigurator config("log4cplus.properties");
config. configure();

log4cplus.properties

 # ALL TRACE DEBUG INFO WARN ERROR FATAL OFF
log4cplus.rootLogger = TRACE,SA
log4cplus.logger.logConsole = TRACE,LC
\t
#For database stuff, I don't need to log everything, it's enough printing only errors!
#log4cplus.logger.DatabaseOperations=ERROR
#log4cplus.additivity.file=false
\t
log4cplus.appender.LC=log4cplus::ConsoleAppender
log4cplus.appender.LC.EnCoding=utf-8
log4cplus.appender.LC.layout=log4cplus::PatternLayout
log4cplus.appender.LC.layout.ConversionPattern=[%D{<!-- -->%Y-%m-%d %H:%M:%S}] %m [%l]%n
\t 
#Set the log to append to the end of the file
log4cplus.appender.SA=log4cplus::TimeBasedRollingFileAppender
//log4cplus.appender.SA.File = Mylogger.log
# The log directory must be created manually; otherwise the file cannot be created
log4cplus.appender.SA.FilenamePattern= ./log/%d{<!-- -->yyyy-MM-dd_HH-mm}.log
log4cplus.appender.SA.Schedule = MINUTELY
log4cplus.appender.SA.CreateDirs = true
log4cplus.appender.SA.MaxHistory = 9999
log4cplus.appender.SA.RollOnClose = false
#Set log file size
log4cplus.appender.SA.MaxFileSize = 100MB
#Set the maximum number of generated logs
log4cplus.appender.SA.MaxBackupIndex = 100
log4cplus.appender.SA.Append = true
log4cplus.appender.SA.layout=log4cplus::PatternLayout
log4cplus.appender.SA.layout.ConversionPattern=[%-5p][%D{<!-- -->%m/%d/%y %H:%M:%S:%Q}] [%t ] %c - %m [%l]%n
\t 
#Set log level range
log4cplus.appender.SA.filters.1=log4cplus::spi::LogLevelRangeFilter
log4cplus.appender.SA.filters.1.LogLevelMin=DEBUG
log4cplus.appender.SA.filters.1.LogLevelMax=FATAL
log4cplus.appender.SA.filters.1.AcceptOnMatch=true
log4cplus.appender.SA.filters.2=log4cplus::spi::DenyAllFilter