Log4cxx编译和使用

Log4cxx编译和使用

1.介绍

       Log4cxx是开放源代码项目Apache Logging Service的子项目之一,是Java社区著名的log4jc++移植版,用于为C++程序提供日志功能,以便开发者对目标程序进行调试和审计。有关log4cxx的更多信息可以从Apache Loggin Service的网站http://logging.apache.org获得。当前的最新版本为0.10.0,本文内容及示例代码都是基于此版本。

2.获取软件包

       可以从官方网站(http://logging.apache.org/log4cxx/index.html)获取源代码,下载版本为apache-log4cxx-0.10.0,下载完成后解压缩到合适目录

3.编译

       下载的原始文件包中没有包含编译后的开发库,需要自己编译生成,本文使用vs2012编译该源文件,编译时需要如下文件包:

       apr-1.2.11-win32-src.zipapr-util-1.2.10-win32-src.zip

这两个压缩包的下载地址分别如下:

       http://archive.apache.org/dist/apr/apr-1.2.11-win32-src.zip

       http://archive.apache.org/dist/apr/apr-util-1.2.10-win32-src.zip

下载完成后将这两个文件分别解压缩到和上面源代码相同的目录下。然后按照如下步骤进行编译:

     a. apr-1.2.11-win32-src.zip解压后的文件名修改为:apr

     b. apr-util-1.2.10-win32-src.zip解压后的文件名修改为:apr-util

     c.  通过vc.netDOS工具进入DOS窗口,此处直接在运行命令栏中输入cmd也可,不过为安全起见最后从.net的工具栏进入DOS界面

    d在运行命令栏中输入cmd,进入apache-log4cxx-0.10.0目录   

    e.  输入configure命令,执行configure.bat批处理文件

    f.   输入configure-aprutil命令,执行configure-aprutil.bat批处理文件,此时可能会提示找不到sed命令的错误,下载sed工具并安装(http://sourceforge.net/projects/gnuwin32/files/sed/sed-4.2-1-setup.exe/download)并将安装目录下的bin加入环境变量内;,然后重新执行configure-aprutil.bat

    g.打开log4cxx\projects目录下的log4cxx.dsw,按照提示升级项目

    h. log4cxx 设置为启动项

         1)、若出现:error C2252: 只能在命名空间范围内显式实例化模板

             双击 "输出" 窗口中的错误行, 此时会在 "代码窗口" 中出现错误的位置.

            选择 LOG4CXX_LIST_DEF, 按键盘 F12, 此时会跳转到该宏的定义

      将

              #define LOG4CXX_LIST_DEF(N, T) \

            template class LOG4CXX_EXPORT std::allocator<T>; \

            template class LOG4CXX_EXPORT std::vector<T>; \

             typedef std::vector<T> N 

       替换为:

              #define LOG4CXX_LIST_DEF(N, T) \

              typedef std::vector<T> N


       2)、 若出现:error C2079: “mip”使用未定义的struct “group_source_req”错误

              请双击第一行出错输出, 将 #if MCAST_JOIN_SOURCE_GROUP 注释, 替换为 #if defined (group_source_req)


  4.使用

a.新建工程 

b.设置属性,然后在C++选项卡中添加附加包含目录,注意该目录为../ apache-log4cxx-0.10.0\src\main\include  $(ProjectDir)\include

c.在属性的链接器输入选项卡的”附加依赖项”中添加”log4cxx.lib将log4cxx.lib和log4cxx.dll两个文件拷贝到工程目录下

d.测试代码,如下:

#include "stdafx.h"
#include "log4cxx/logger.h"

int _tmain(int argc, _TCHAR* argv[])
{
log4cxx::LoggerPtr logger(log4cxx::Logger::getLogger("MyApp"));

LOG4CXX_INFO(logger, "hello, world");

system("pause");
return 0;
}


在test目录下新建配置文件log4j.properties,内容如下:
log4j.rootLogger=DEBUG, A1
log4j.appender.A1=org.apache.log4j.FileAppender
log4j.appender.A1.File=tjw.log
log4j.appender.A1.layout=org.apache.log4j.PatternLayout

# Print the date in ISO 8601 format
log4j.appender.A1.layout.ConversionPattern=%d %-5p %c - %m%n

# Print only messages of level WARN or above in the package com.foo.
log4j.logger.com.foo=WARN

运行后可以生成日志文件,包含内容:
2017-07-19 23:09:17,421 INFO  MyApp - hello, world

e.可以封装成类:logclass使用

/******************************************************************************
  author  : hxg
  purpose : 日志系统,以宏log_XXX开头,封装log4cxx日志。
*******************************************************************************/
#pragma once
 
 
#include "log4cxx/logger.h"
#include "log4cxx/propertyconfigurator.h"
/*
日志类
*/
 
#include<string>
using namespace std;
 
class CServerlog;
extern CServerlog* g_serverlog;
 
#define log_debug(pszFormat, ...)    g_serverlog->WriteFormatDebugLog( pszFormat, __VA_ARGS__)
#define log_info(pszFormat, ...)    g_serverlog->WriteFormatInfoLog( pszFormat, __VA_ARGS__)
#define log_warn(pszFormat, ...)    g_serverlog->WriteFormatWarnLog( pszFormat, __VA_ARGS__)
#define log_error(pszFormat, ...)    g_serverlog->WriteFormatErrorLog( pszFormat, __VA_ARGS__)
 
class CServerlog
{
public:
    CServerlog();
    ~CServerlog();
 
    void WriteFormatDebugLog(char* lpszFormat, ...);
    void WriteFormatInfoLog(char* lpszFormat, ...);    
 
    void WriteFormatWarnLog(char* lpszFormat, ...);
    void WriteFormatErrorLog(char* lpszFormat, ...);
 
protected:
 
    log4cxx::LoggerPtr infologger;
    log4cxx::LoggerPtr errorlogger;
};



//、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、
#include <Windows.h> 
#include <tchar.h> 
#include <stdio.h> 
#include "logClass.h"


CServerlog* g_serverlog = new CServerlog();

CServerlog::CServerlog()
{
	log4cxx::PropertyConfigurator::configure("log4cxx.properties");
	infologger = (log4cxx::Logger::getLogger("server"));
	errorlogger = (log4cxx::Logger::getLogger("server.error"));
}

CServerlog::~CServerlog()
{
}


void CServerlog::WriteFormatDebugLog(char* lpszFormat, ...)
{
	va_list args;
	va_start(args, lpszFormat);
	CHAR szBuffer[1024];
	vsprintf_s(szBuffer, lpszFormat, args);
	va_end(args);
	infologger->debug(szBuffer);
}


void CServerlog::WriteFormatInfoLog(char* lpszFormat, ...)
{
	va_list args;
	va_start(args, lpszFormat);
	CHAR szBuffer[1024];
	vsprintf_s(szBuffer, lpszFormat, args);
	va_end(args);
	infologger->info(szBuffer);
}

void CServerlog::WriteFormatWarnLog(char* lpszFormat, ...)
{
	va_list args;
	va_start(args, lpszFormat);
	CHAR szBuffer[1024];
	vsprintf_s(szBuffer, lpszFormat, args);
	va_end(args);

	errorlogger->warn(szBuffer);
}

void CServerlog::WriteFormatErrorLog(char* lpszFormat, ...)
{
	va_list args;
	va_start(args, lpszFormat);
	CHAR szBuffer[1024];
	vsprintf_s(szBuffer, lpszFormat, args);
	va_end(args);
	errorlogger->error(szBuffer);
}

f.其他相关文章:


http://blog.csdn.net/tujiaw/article/details/8521096


http://blog.sina.com.cn/s/blog_a459dcf501013tbn.html

猜你喜欢

转载自blog.csdn.net/u010855021/article/details/75452989
今日推荐