log4cplus的安装与使用初步

1. 简单介绍
log4cplus是C++编写的开源的日志系统,The purpose of this project is to port the excellentLog for Java (log4j)logging library to C++
log4cplus具有灵活、强大、使用简单、多线程安全的特点,实在是杂牌军、游击队的福音。

2. 安装使用(Linux)
log4cplus安装使用很easy。从其官网:http://log4cplus.sourceforge.net/ 下载最新版本号
执行:
tar xvzf log4cplus-x.x.x.tar.gz
cd log4cplus-x.x.x
./configure --prefix=/where/to/install
make
make install
在安装文件夹下生成include和lib两个文件夹。分别为头文件和库文件
使用:
g++ -o server   /mnt/hgfs/work_vm/project/work_project/server/obj/main.o   -L../..//third/log4cplus/lib/ -L../..//third/boost/lib/-llog4cplus -lpthread -I/mnt/hgfs/work_vm/project/work_project/server/include -I../..//third/log4cplus/include/-I../..//third/boost/include/
编译參数:
-L../..//third/log4cplus/lib/
-llog4cplus 
-lpthread
-I../..//third/log4cplus/include/

3. 使用
log4cplus主要包含layout、appender、loglevel等内容。能够參考:
http://masterdog.bokee.com/153892.html
写的很nice

4. 包装
logcplus包装下用起来很方便,下面是我的包装。供參考
Log.h
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
// Log.h: interface for the Log class.
//
//////////////////////////////////////////////////////////////////////
 
#if !defined(AFX_LOG_H__B87F71E3_FFAE_4CFA_A528_3F4F2FF7D69E__INCLUDED_)
#define AFX_LOG_H__B87F71E3_FFAE_4CFA_A528_3F4F2FF7D69E__INCLUDED_
 
#include "log4cplus/loglevel.h"
#include "log4cplus/ndc.h"
#include "log4cplus/logger.h"
#include "log4cplus/configurator.h"
#include "iomanip"
#include "log4cplus/fileappender.h"
#include "log4cplus/layout.h"
 
#include "const.h"
#include "common.h"
#include "Main_config.h"
 
using namespace log4cplus;
using namespace log4cplus::helpers;
 
//日志封装
#define TRACE(p) LOG4CPLUS_TRACE(Log::_logger, p)
#define DEBUG(p) LOG4CPLUS_DEBUG(Log::_logger, p)
#define NOTICE(p) LOG4CPLUS_INFO(Log::_logger, p)
#define WARNING(p) LOG4CPLUS_WARN(Log::_logger, p)
#define FATAL(p) LOG4CPLUS_ERROR(Log::_logger, p)
 
// 日志控制类,全局共用一个日志
class Log
{
public:
    // 打开日志
    bool open_log();
 
    // 获得日志实例
    static Log& instance();
     
    static Logger _logger;
 
private:
    Log();
 
    virtual ~Log();
 
    //log文件路径及名称
    char _log_path[PATH_SIZE];
    char _log_name[PATH_SIZE];
};
 
#endif // !defined(AFX_LOG_H__B87F71E3_FFAE_4CFA_A528_3F4F2FF7D69E__INCLUDED_)
Log.cpp:
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
// Log.cpp: implementation of the Log class.
//
//////////////////////////////////////////////////////////////////////
 
#include "Log.h"
 
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
 
Logger Log::_logger = log4cplus::Logger::getInstance("main_log");
 
Log::Log()
{
    snprintf(_log_path, sizeof(_log_path), "%s""../log");
    snprintf(_log_name, sizeof(_log_name), "%s/%s.%s", _log_path, execname, "log");
}
 
Log::~Log()
{
}
 
Log& Log::instance()
{
    static Log log;
    return log;
}
 
bool Log::open_log()
{
     
    int Log_level = Main_config::instance().get_config().Read("LOG_LEVEL", 0); 
 
    /* step 1: Instantiate an appender object */
    SharedAppenderPtr _append(new FileAppender(_log_name));
    _append->setName("file log test");
 
    /* step 2: Instantiate a layout object */
    std::string pattern = "[%p] [%d{%m/%d/%y %H:%M:%S}] [%t] - %m %n";
    std::auto_ptr<Layout> _layout(new PatternLayout(pattern));
 
    /* step 3: Attach the layout object to the appender */
    _append->setLayout(_layout);
 
    /* step 4: Instantiate a logger object */
 
    /* step 5: Attach the appender object to the logger  */
    Log::_logger.addAppender(_append);
 
    /* step 6: Set a priority for the logger  */
    Log::_logger.setLogLevel(Log_level);
 
    return true;
}
int Log_level = Main_config::instance().get_config().Read("LOG_LEVEL", 0); 

Main_config是我自己包装的一个配置类(參考:http://blog.csdn.net/yfkiss/article/details/6802451)。通过读取配置设置log level。

使用:
#inlucde "Log.h"
程序初始化的时候:
    // 打开日志
    if (!Log::instance().open_log())
    {
        std::cout << "Log::open_log() failed" << std::endl;
        return false;
    }
然后使用NOTICE、FATAL等就能够打印日志到文件。

?
1
2
3
4
5
6
7
8
9
10
11
#include "Log.h"
....
    // 打开日志
    if (!Log::instance().open_log())
    {
        std::cout << "Log::open_log() failed" << std::endl;
        return false;
    }
.....
NOTICE("Server init succ");
FATAL("Server run failed");
转自:http://blog.csdn.net/yfkiss/article/details/6802422
原文地址:https://www.cnblogs.com/brucemengbm/p/6721584.html