~~~~ 做C++好几年了,记录一个自己用的C++日志库吧,自己不记得也可以看看:log4cpp
~~~~ 这是资源路径:log4cpp 1.1.3。
~~~~ 如果资源失效,就去官网下载吧,官网吧!http://log4cpp.sourceforge.net/#download。
~~~~ 下不到的可以找我帮忙下载,也可以找我要。
~~~~ 下载好后,找个路径解压,编译就可以了,我是在ubuntu上使用,make check是测试一下编译有没有问题:
./configure
make
make check
sudo make install
~~~~ windows上:需要使用cmake编译,安装cmake直接去官网下载,安装就行了,没有什么难度的东西(附上官网下载页面地址:https://cmake.org/download/)。
~~~~ windows使用cmake就差不多了,解压压缩包,进入目录,创建build目录,进入目录,cmake …/。
E:\boost\log4cpp-1.1.3\log4cpp>mkdir build
E:\boost\log4cpp-1.1.3\log4cpp>cd build
E:\boost\log4cpp-1.1.3\log4cpp\build>cmake ../
-- Building for: Visual Studio 15 2017
-- The C compiler identification is MSVC 19.15.26726.0
-- The CXX compiler identification is MSVC 19.15.26726.0
-- Check for working C compiler: C:/Program Files (x86)/Microsoft Visual Studio/2017/Professional/VC/Tools/MSVC/14.15.26726/bin/Hostx86/x86/cl.exe
-- Check for working C compiler: C:/Program Files (x86)/Microsoft Visual Studio/2017/Professional/VC/Tools/MSVC/14.15.26726/bin/Hostx86/x86/cl.exe -- works
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Detecting C compile features
-- Detecting C compile features - done
-- Check for working CXX compiler: C:/Program Files (x86)/Microsoft Visual Studio/2017/Professional/VC/Tools/MSVC/14.15.26726/bin/Hostx86/x86/cl.exe
-- Check for working CXX compiler: C:/Program Files (x86)/Microsoft Visual Studio/2017/Professional/VC/Tools/MSVC/14.15.26726/bin/Hostx86/x86/cl.exe -- works
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Detecting CXX compile features
-- Detecting CXX compile features - done
CMake Warning (dev) in CMakeLists.txt:
No cmake_minimum_required command is present. A line of code such as
cmake_minimum_required(VERSION 3.13)
should be added at the top of the file. The version specified may be lower
if you wish to support older CMake versions for this project. For more
information run "cmake --help-policy CMP0000".
This warning is for project developers. Use -Wno-dev to suppress it.
-- Configuring done
-- Generating done
-- Build files have been written to: E:/boost/log4cpp-1.1.3/log4cpp/build
E:\boost\log4cpp-1.1.3\log4cpp\build>
~~~~ 然后用vs打开编译即可。
例子源码:demo.
~~~~ 刚刚编译好的库,我们可以写一个例子,记录下来怎么使用,这样下次不记得可以快速想起来怎么用。我这里主要还是在ubuntu上,版本是16.04。
~~~ 创建文件Clog4Util.h
#ifndef CLOG4UTIL_INCLUDE
#define CLOG4UTIL_INCLUDE
#include "log4cpp/Category.hh"
#include "log4cpp/Appender.hh"
#include "log4cpp/FileAppender.hh"
#include "log4cpp/OstreamAppender.hh"
#include "log4cpp/Layout.hh"
#include "log4cpp/BasicLayout.hh"
#include "log4cpp/Priority.hh"
#include "log4cpp/PropertyConfigurator.hh"
#include
#include
using namespace std;
namespace log4Util {
int Init(string initfilename);
void close();
void Debug(const char* tag, const char* msg);
void Debug(const char* msg);
void Info(const char* tag, const char* msg);
void Info(const char* msg);
void Error(const char* tag, const char* msg);
void Error(const char* msg);
}
#endif
~~~~ 创建文件Clog4Util.cpp
#include "Clog4Util.h"
namespace log4Util {
int Init(string initfilename) {
try {
log4cpp::PropertyConfigurator::configure(initfilename);
}
catch (log4cpp::ConfigureFailure&f) {
std::cout << "Configure Problem " << f.what() << std::endl;//失败
return -1;
}
return 0;
}
void close() {
try {
log4cpp::Category::shutdown();
}
catch (...) {
}
}
void Debug(const char* tag, const char* msg) {
try {
log4cpp::Category& t_debug = log4cpp::Category::getInstance(tag);
t_debug.debug(msg);
}
catch (...) {
}
}
void Debug(const char* msg) {
try {
log4cpp::Category& t_debug = log4cpp::Category::getInstance(string("Debug"));
t_debug.debug(msg);
}
catch (...) {
}
}
void Info(const char* tag, const char* msg) {
try {
log4cpp::Category& t_debug = log4cpp::Category::getInstance(tag);
t_debug.info(msg);
}
catch (...) {
}
}
void Info(const char* msg) {
try {
log4cpp::Category& t_info = log4cpp::Category::getInstance(string("Info"));
t_info.info(msg);
}
catch (...) {
}
}
void Error(const char* tag, const char* msg) {
try {
log4cpp::Category& t_debug = log4cpp::Category::getInstance(tag);
t_debug.error(msg);
}
catch (...) {
}
}
void Error(const char* msg) {
try {
log4cpp::Category& t_error = log4cpp::Category::getInstance(string("Error"));
t_error.error(msg);
}
catch (...) {
}
}
}
~~~~ 创建文件main.cpp:
#include
#include
#include
#include
#include
#include
#include
#include
#include "Clog4Util.h"
int main(int argc, char *argv[])
{
if(argc < 2){
printf("Usage: %s base exponent \n", argv[0]);
return 1;
}
log4Util::Init(argv[1]);
printf("test log4cpp\n");
//存放日志信息
char strLog_[1024] = {
0 };
//建日志写到strLog_中
sprintf(strLog_, "test error log, %s:%d.", __FILE__, __LINE__);
//写入日志
log4Util::Error(strLog_);
//赋空
memset(strLog_, 0, 1024);
//建日志写到strLog_中
sprintf(strLog_, "test error log, %s:%d.", __FILE__, __LINE__);
//写入日志
log4Util::Debug(strLog_);
//赋空
memset(strLog_, 0, 1024);
//建日志写到strLog_中
sprintf(strLog_, "test error log, %s:%d.", __FILE__, __LINE__);
//写入日志
log4Util::Info(strLog_);
//赋空
memset(strLog_, 0, 1024);
//建日志写到strLog_中
sprintf(strLog_, "test error log,tag:1 %s:%d.", __FILE__, __LINE__);
//写入日志
log4Util::Error("1", strLog_);
//赋空
memset(strLog_, 0, 1024);
//建日志写到strLog_中
sprintf(strLog_, "test error log,tag:2 %s:%d.", __FILE__, __LINE__);
//写入日志
log4Util::Debug("2", strLog_);
//赋空
memset(strLog_, 0, 1024);
//建日志写到strLog_中
sprintf(strLog_, "test error log,tag:3 %s:%d.", __FILE__, __LINE__);
//写入日志
log4Util::Info("3", strLog_);
log4Util::close();
return 0;
}
~~~~ 创建CMakeList.txt文件:
project(test)
#添加C++11支持
add_definitions(-std=c++11)
#添加头文件搜索路径
include_directories(/usr/local/include)
#添加库文件搜索路径
link_directories(/usr/local/lib)
#用于将当前目录下的所有源文件的名字保存在变量 DIR_SRCS 中
aux_source_directory(. DIR_SRCS)
add_executable(test ${
DIR_SRCS})
#在这里根据名字log4cpp寻找文件
target_link_libraries(test log4cpp pthread)
~~~~ 创建目录build,进入目录编译
qilimi@sharenew-desktop:~/test/log4cpp/1-test/build$ cmake ../
-- The C compiler identification is GNU 5.4.0
-- The CXX compiler identification is GNU 5.4.0
...
-- Configuring done
-- Generating done
-- Build files have been written to: /home/qilimi/test/log4cpp/1-test/build
qilimi@sharenew-desktop:~/test/log4cpp/1-test/build$ make
Scanning dependencies of target test
[ 33%] Building CXX object CMakeFiles/test.dir/main.o
[ 66%] Linking CXX executable test
[100%] Built target test
qilimi@sharenew-desktop:~/test/log4cpp/1-test/build$
~~~~ 现在还需要一个日志文件配置文件,我们创建文件log4cpp.conf:
#-------定义rootCategory的属性-------
#指定rootCategory的log优先级是ERROR,其Appenders有两个,分别是console,TESTAppender
log4cpp.rootCategory=DEBUG, console, TESTAppender
#-------定义console属性-------
#consoleAppender类型:控制台输出
#下面这三条语句表示控制台输出的log输出的布局按照指定的格式;输出格式是:[%p] %d{%H:%M:%S.%l} (%c): %m%n
log4cpp.appender.console=ConsoleAppender
log4cpp.appender.console.layout=PatternLayout
log4cpp.appender.console.layout.ConversionPattern=[%p] %d{%H:%M:%S.%l} (%c): %m%n
#-------定义TESTAppender的属性-------
#RollingFileAppender类型:输出到回卷文件,即文件到达某个大小的时候产生一个新的文件
#下面的语句表示文件输出到指定的log文件,输出的布局按照指定的格式,输出的格式是:[%d{%Y-%m-%d %H:%M:%S.%l} - %p] (%c): %m%n
log4cpp.appender.TESTAppender=RollingFileAppender
#当日志文件到达maxFileSize大小时,将会自动滚动
log4cpp.appender.TESTAppender.maxFileSize=1024000
#maxBackupIndex指定可以产生的滚动文件的最大数
log4cpp.appender.TESTAppender.maxBackupIndex=40
#fileName指定信息输出到logs/irismatch.log文件
log4cpp.appender.TESTAppender.fileName=/home/qilimi/log.log
#PatternLayout 表示可以灵活指定布局模式
log4cpp.appender.TESTAppender.layout=PatternLayout
#append=true 信息追加到上面指定的日志文件中,false表示将信息覆盖指定文件内容
log4cpp.appender.TESTAppender.append=true
log4cpp.appender.TESTAppender.layout.ConversionPattern=[%d{%Y-%m-%d %H:%M:%S.%l} - %p] (%c): %m%n
~~~~ 让我们直接运行程序:
qilimi@sharenew-desktop:~/test/log4cpp/1-test/build$ ./test log4cpp.conf
test log4cpp
[ERROR] 18:42:34.604 (Error): test error log, /home/qilimi/test/log4cpp/1-test/main.cpp:27.
[DEBUG] 18:42:34.604 (Debug): test error log, /home/qilimi/test/log4cpp/1-test/main.cpp:36.
[INFO] 18:42:34.604 (Info): test error log, /home/qilimi/test/log4cpp/1-test/main.cpp:45.
[ERROR] 18:42:34.604 (1): test error log,tag:1 /home/qilimi/test/log4cpp/1-test/main.cpp:54.
[DEBUG] 18:42:34.604 (2): test error log,tag:2 /home/qilimi/test/log4cpp/1-test/main.cpp:63.
[INFO] 18:42:34.604 (3): test error log,tag:3 /home/qilimi/test/log4cpp/1-test/main.cpp:72.
qilimi@sharenew-desktop:~/test/log4cpp/1-test/build$ cat ~/log.log
[2019-02-15 18:34:58.868 - ERROR] (Error): test error log, /home/qilimi/test/log4cpp/1-test/main.cpp:28.
[2019-02-15 18:34:58.868 - DEBUG] (Debug): test error log, /home/qilimi/test/log4cpp/1-test/main.cpp:37.
[2019-02-15 18:34:58.868 - INFO] (Info): test error log, /home/qilimi/test/log4cpp/1-test/main.cpp:46.
[2019-02-15 18:34:58.868 - ERROR] (1): test error log,tag:1 /home/qilimi/test/log4cpp/1-test/main.cpp:55.
[2019-02-15 18:34:58.868 - DEBUG] (2): test error log,tag:2 /home/qilimi/test/log4cpp/1-test/main.cpp:64.
[2019-02-15 18:34:58.868 - INFO] (3): test error log,tag:3 /home/qilimi/test/log4cpp/1-test/main.cpp:73.
[2019-02-15 18:40:26.625 - ERROR] (Error): test error log, /home/qilimi/test/log4cpp/1-test/main.cpp:28.
[2019-02-15 18:40:26.625 - DEBUG] (Debug): test error log, /home/qilimi/test/log4cpp/1-test/main.cpp:37.
[2019-02-15 18:40:26.625 - INFO] (Info): test error log, /home/qilimi/test/log4cpp/1-test/main.cpp:46.
[2019-02-15 18:40:26.625 - ERROR] (1): test error log,tag:1 /home/qilimi/test/log4cpp/1-test/main.cpp:55.
[2019-02-15 18:40:26.625 - DEBUG] (2): test error log,tag:2 /home/qilimi/test/log4cpp/1-test/main.cpp:64.
[2019-02-15 18:40:26.626 - INFO] (3): test error log,tag:3 /home/qilimi/test/log4cpp/1-test/main.cpp:73.
[2019-02-15 18:42:34.604 - ERROR] (Error): test error log, /home/qilimi/test/log4cpp/1-test/main.cpp:27.
[2019-02-15 18:42:34.604 - DEBUG] (Debug): test error log, /home/qilimi/test/log4cpp/1-test/main.cpp:36.
[2019-02-15 18:42:34.604 - INFO] (Info): test error log, /home/qilimi/test/log4cpp/1-test/main.cpp:45.
[2019-02-15 18:42:34.604 - ERROR] (1): test error log,tag:1 /home/qilimi/test/log4cpp/1-test/main.cpp:54.
[2019-02-15 18:42:34.604 - DEBUG] (2): test error log,tag:2 /home/qilimi/test/log4cpp/1-test/main.cpp:63.
[2019-02-15 18:42:34.604 - INFO] (3): test error log,tag:3 /home/qilimi/test/log4cpp/1-test/main.cpp:72.
qilimi@sharenew-desktop:~/test/log4cpp/1-test/build$
~~~~ 完成。