StdLogger.h
#ifndef STDLOGGER_H
#define STDLOGGER_H
#include <log4cplus/logger.h>
#include <log4cplus/configurator.h>
#include <log4cplus/helpers/loglog.h>
#include <log4cplus/consoleappender.h>
#include <log4cplus/fileappender.h>
class StdLogger
{
public:
StdLogger();
virtual ~StdLogger();
/// 启动日志系统
/// @param[in] properties_filename 日志系统配置文件文件名
void StartSystem();
/// 关闭日志系统
void StopSystem();
public:
void Debug(const char* pFormat, ...);
void Error(const char* pFormat, ...);
void Fatal(const char* pFormat, ...);
void Info(const char* pFormat, ...);
void Warn(const char* pFormat, ...);
void Trace(const char* pFormat, ...);
public:
static inline StdLogger* getSingletonPtr()
{
return &getSingleton();
}
static inline StdLogger& getSingleton()
{
static StdLogger _instance;
return _instance;
}
};
#define g_Logger StdLogger::getSingleton()
#define g_pLogger StdLogger::getSingleton()
//////////////////////////////////////////////////////////////////////////
// 断言日志
//////////////////////////////////////////////////////////////////////////
#define ASSERT_LOG(expr)\
if ( (expr) ) {;} else g_Logger.Error(__FILE__, __LINE__, #expr);
//////////////////////////////////////////////////////////////////////////
// 以下的宏只有VS2005以及之上的版本可以使用!因为VS2005之下的版本不支持可变参数宏
//////////////////////////////////////////////////////////////////////////
#if defined(_MSC_VER) && _MSC_VER > 1400
#define LOG_DEBUG(...) g_Logger.Debug(__FILE__, __LINE__, __VA_ARGS__);
#define LOG_ERROR(...) g_Logger.Error(__FILE__, __LINE__, __VA_ARGS__);
#define LOG_FATAL(...) g_Logger.Fatal(__FILE__, __LINE__, __VA_ARGS__);
#define LOG_INFO(...) g_Logger.Info(__FILE__, __LINE__, __VA_ARGS__);
#define LOG_WARN(...) g_Logger.Warn(__FILE__, __LINE__, __VA_ARGS__);
#define LOG_TRACE(...) g_Logger.Trace(__FILE__, __LINE__, __VA_ARGS__);
#endif
#endif
实现:
#include "StdLogger.h"
#include <cstdlib>
#include <iostream>
#include <log4cplus/config.hxx>
#include <log4cplus/logger.h>
#include <log4cplus/configurator.h>
#include <log4cplus/helpers/loglog.h>
#include <log4cplus/helpers/stringhelper.h>
#include <log4cplus/helpers/socket.h>
#include <log4cplus/helpers/threads.h>
#include <log4cplus/spi/loggerimpl.h>
#include <log4cplus/spi/loggingevent.h>
#include <log4cplus/fileappender.h>
using namespace log4cplus;
using namespace log4cplus::helpers;
StdLogger::StdLogger()
{
}
StdLogger::~StdLogger()
{
log4cplus::Logger _logger = log4cplus::Logger::getRoot();
LOG4CPLUS_INFO(_logger, "Logger System Stop Finish.");
}
#define DO_LOGGER(logLevel,pFormat, bufSize)\
log4cplus::Logger _logger = log4cplus::Logger::getRoot();\
\
if(_logger.isEnabledFor(logLevel))\
{ \
va_list args; \
va_start(args, pFormat); \
char buf[bufSize] = {0}; \
_vsnprintf(buf, sizeof(buf), pFormat, args); \
va_end(args); \
_logger.forcedLog(logLevel, buf); \
}
void StdLogger::Debug( const char* pFormat, ...)
{
DO_LOGGER(log4cplus::DEBUG_LOG_LEVEL,pFormat, 1024);
}
void StdLogger::Error( const char* pFormat, ...)
{
DO_LOGGER(log4cplus::ERROR_LOG_LEVEL,pFormat, 256);
}
void StdLogger::Fatal( const char* pFormat, ...)
{
DO_LOGGER(log4cplus::FATAL_LOG_LEVEL,pFormat, 256);
}
void StdLogger::Info(const char* pFormat,...)
{
DO_LOGGER(log4cplus::INFO_LOG_LEVEL,pFormat, 512);
}
void StdLogger::Warn(const char* pFormat, ...)
{
DO_LOGGER(log4cplus::WARN_LOG_LEVEL,pFormat, 256);
}
void StdLogger::Trace(const char* pFormat, ...)
{
DO_LOGGER(log4cplus::TRACE_LOG_LEVEL, pFormat, 1024);
}
void StdLogger::StartSystem( )
{
SharedAppenderPtr _append (new ConsoleAppender());
_append->setName("screen_test");
std::string pattern = "%d{%m/%d/%y %H:%M:%S} - %m %n";
std::string pattern2 = "%d{%m/%d/%y %H:%M:%S} - %m [%l]%n";
std::auto_ptr<Layout> _layout(new PatternLayout(pattern));
std::auto_ptr<Layout> layout1(new PatternLayout(pattern2));
std::auto_ptr<Layout> layout2(new PatternLayout(pattern));
_append->setLayout( _layout );
SharedAppenderPtr append1(new FileAppender("web_all.log"));
append1->setName("all log test");
SharedAppenderPtr append2(new FileAppender("web_info.log"));
append2->setName("info log test");
append1->setLayout( layout1 );
append2->setLayout( layout2 );
Logger logger1=Logger::getRoot();
Logger logger2 =Logger::getInstance("web");
Logger logger3 = Logger::getInstance("web.subtest");
logger1.addAppender(append1);
//logger1.setLogLevel(INFO_LOG_LEVEL);
logger2.addAppender(append2);
logger1.setLogLevel(ALL_LOG_LEVEL);
logger3.addAppender(_append);
logger3.setLogLevel(INFO_LOG_LEVEL);
}
void StdLogger::StopSystem()
{
}
///调用
g_Logger.StartSystem();
log4cplus::Logger root_logger = Logger::getRoot();
log4cplus::Logger info_logger= Logger::getInstance("web.subtest");
LOG4CPLUS_INFO(root_logger, "all log Logger Start .");
LOG4CPLUS_INFO(info_logger, "web info Logger Start .");