log4cplus使用示例

1、l4jlog.h封装头文件

#pragma once

#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 

#define MODULE_NAME			"ConsoleApplicationDemo"
#define CONFIG_PATH_NAME	"log4cplus.properties"

class BaseLog
{
public:
	BaseLog() { log4cplus::PropertyConfigurator::doConfigure(CONFIG_PATH_NAME); };
	~BaseLog() {};

	log4cplus::Logger GetLogger() { return log4cplus::Logger::getInstance(MODULE_NAME); }
	static BaseLog &GetInstance() { static BaseLog instance; return instance; }

private:
	log4cplus::Initializer initializer_;
};

#define LOG_TRACE(...)		LOG4CPLUS_TRACE_FMT(BaseLog::GetInstance().GetLogger(), __VA_ARGS__) 
#define LOG_DEBUG(...)		LOG4CPLUS_DEBUG_FMT(BaseLog::GetInstance().GetLogger(), __VA_ARGS__) 
#define LOG_INFO(...)		LOG4CPLUS_INFO_FMT(BaseLog::GetInstance().GetLogger(), __VA_ARGS__) 
#define LOG_WARN(...)		LOG4CPLUS_WARN_FMT(BaseLog::GetInstance().GetLogger(), __VA_ARGS__) 
#define LOG_ERROR(...)		LOG4CPLUS_ERROR_FMT(BaseLog::GetInstance().GetLogger(), __VA_ARGS__) 
#define LOG_FATAL(...)		LOG4CPLUS_FATAL_FMT(BaseLog::GetInstance().GetLogger(), __VA_ARGS__) 

2、ConsoleApplicationDemo.cpp文件

// ConsoleApplicationDemo.cpp : 定义控制台应用程序的入口点。
//

#include "stdafx.h"
#include "l4jlog.h"

int main()
{
	{
		LOG_TRACE("this is trace message.");
		LOG_DEBUG("this is debug message.");
		LOG_INFO("this is info message.");
		LOG_WARN("this is warn message.");
		LOG_ERROR("this is error message.");
		LOG_FATAL("this is fatal message.");

		LOG_DEBUG("hello, %s, timestamp:%ld", "this is debug message.", time(0));
	}

	system("pause");

	return 0;
}


3、日志配置文件

log4cplus.rootLogger=TRACE, STDOUT, RootAppender

log4cplus.appender.STDOUT=log4cplus::ConsoleAppender
log4cplus.appender.STDOUT.layout=log4cplus::PatternLayout
log4cplus.appender.STDOUT.layout.ConversionPattern=%D{%Y/%m/%d %H:%M:%S.%q} [%t] [%-5p] %c{2} [%l][%M] - %m%n

log4cplus.appender.RootAppender=log4cplus::RollingFileAppender
log4cplus.appender.RootAppender.File=logs/ConsoleApplicationDemo.log
log4cplus.appender.RootAppender.CreateDirs=true
log4cplus.appender.RootAppender.MaxFileSize=1MB
log4cplus.appender.RootAppender.MaxBackupIndex=10
log4cplus.appender.RootAppender.layout=log4cplus::PatternLayout
log4cplus.appender.RootAppender.layout.ConversionPattern=%D{%Y/%m/%d %H:%M:%S.%q} [%t] [%-5p] %c{2} [%l][%M] - %m%n


4、日志输出

2023/05/29 17:06:48.186 [22732] [TRACE] ConsoleApplicationDemo [ConsoleApplicationDemo.cpp:16][int __cdecl main(void)] - this is trace message.
2023/05/29 17:06:48.187 [22732] [DEBUG] ConsoleApplicationDemo [ConsoleApplicationDemo.cpp:17][int __cdecl main(void)] - this is debug message.
2023/05/29 17:06:48.187 [22732] [INFO ] ConsoleApplicationDemo [ConsoleApplicationDemo.cpp:18][int __cdecl main(void)] - this is info message.
2023/05/29 17:06:48.188 [22732] [WARN ] ConsoleApplicationDemo [ConsoleApplicationDemo.cpp:19][int __cdecl main(void)] - this is warn message.
2023/05/29 17:06:48.188 [22732] [ERROR] ConsoleApplicationDemo [ConsoleApplicationDemo.cpp:20][int __cdecl main(void)] - this is error message.
2023/05/29 17:06:48.189 [22732] [FATAL] ConsoleApplicationDemo [ConsoleApplicationDemo.cpp:21][int __cdecl main(void)] - this is fatal message.
2023/05/29 17:06:48.189 [22732] [DEBUG] ConsoleApplicationDemo [ConsoleApplicationDemo.cpp:23][int __cdecl main(void)] - hello, this is debug message., timestamp:1685351208

你可能感兴趣的:(C++,c++,开发语言)