Log4cplusInitalizer.cpp 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. #include "StdAfx.h"
  2. #include "Log4cplusInitalizer.h"
  3. #include <log4cplus/logger.h>
  4. #include <log4cplus/configurator.h>
  5. #include <log4cplus/loggingmacros.h>
  6. #include <log4cplus/consoleappender.h>
  7. #include <log4cplus/layout.h>
  8. #include <log4cplus/fileappender.h>
  9. #include <log4cplus/helpers/loglog.h>
  10. #include <log4cplus/helpers/property.h>
  11. #include <memory>
  12. using namespace log4cplus;
  13. using namespace std;
  14. //Log4cplusInitalizer _log4cplusInitalizer;
  15. bool Log4cplusInitalizer::initalized = false;
  16. Log4cplusInitalizer::Log4cplusInitalizer(void)
  17. {
  18. }
  19. Log4cplusInitalizer::~Log4cplusInitalizer(void)
  20. {
  21. }
  22. void Log4cplusInitalizer::initalize()
  23. {
  24. if (initalized)return;
  25. initalized = true;
  26. std::locale::global(std::locale(""));
  27. log4cplus::initialize();
  28. helpers::LogLog::getLogLog()->setInternalDebugging(false);
  29. // 定义一个文件Appender
  30. SharedAppenderPtr pFileAppender(new FileAppender(LOG4CPLUS_TEXT("SmartEvaluationLogic.log"), std::ios_base::app));
  31. // 定义Logger
  32. Logger pTestLogger = Logger::getInstance(LOG4CPLUS_TEXT("LoggerName"));
  33. tstring pattern = LOG4CPLUS_TEXT("%D{%m/%d/%y %H:%M:%S} - %m [%l]%n");
  34. std::auto_ptr<Layout> _layout(new PatternLayout(pattern));
  35. pFileAppender->setLayout(_layout);
  36. // 将需要关联Logger的Appender添加到Logger上
  37. pTestLogger.addAppender(pFileAppender);
  38. // 输出日志信息
  39. LOG4CPLUS_INFO_STR(pTestLogger, LOG4CPLUS_TEXT("---->开始写日志------------>>>>>>>>"));
  40. LOG4CPLUS_INFO_FMT(pTestLogger, LOG4CPLUS_TEXT("---->开始写日志------------>>>>>>>>"));
  41. }