zoukankan      html  css  js  c++  java
  • log4cplus使用

    log4cplus is a simple to use C++ logging API providing thread-safe, flexible, and arbitrarily granular control over log management and configuration. It is modelled after the Java log4j API. 下载地址http://sourceforge.net/projects/log4cplus/

    也可以看出Java/C++是互相借鉴的,没有好坏之分。

    很详细的介绍在log4cplus学习笔记

    本文记录在项目中的一个常用实例。

    定义工具文件

    Util.h

    #include <log4cplus/logger.h>
    #include <log4cplus/configurator.h>
    #include <log4cplus/fileappender.h>
    #include <log4cplus/loggingmacros.h>
    using namespace log4cplus;
    using namespace log4cplus::helpers;
    inline void InitLogger(
      const string& name = "PINGLOG",
      const string& path = "./log",
      const string& loglevel = "DEBUG",
      const string& pattern = "%-10D{%H:%M:%S.%q} : [%p] : %m%n")
    {
      log4cplus::SharedAppenderPtr append_1(new log4cplus::DailyRollingFileAppender(path, log4cplus::HOURLY , 1, true));
      append_1->setName(name);
      append_1->setLayout( std::auto_ptr<log4cplus::Layout>(new log4cplus::PatternLayout(pattern)) );
      log4cplus::Logger::getInstance(name).addAppender(append_1);
      log4cplus::Logger::getInstance(name).setLogLevel(log4cplus::LogLevelManager().fromString(loglevel));
    }
    
    #define LOG_DEBUG(msg) \
      LOG4CPLUS_DEBUG(log4cplus::Logger::getInstance("PINGLOG"), msg)
    #define LOG_INFO(msg) \
      LOG4CPLUS_INFO(log4cplus::Logger::getInstance("PINGLOG"), msg) 
    #define LOG_WARN(msg) \
      LOG4CPLUS_WARN(log4cplus::Logger::getInstance("PINGLOG"), msg)


    使用

    #include "Util.h"
    InitLogger();
    LOG_INFO("test");
    LOG_WARN("test" << " WARN");


    注意:保存路径要具体到文件,如./log,就是保存到当前路径log文件中。

  • 相关阅读:
    LeetCode -- Rectangle Area
    【转】VS常用快捷键
    C++中include<> 与 include" " 的区别
    浅析_tmain() 与 main() 函数的区别
    VS2013下配置OpenCV 3.0.0 &&& VS2013下配置Opencv2.4.9
    LeetCode -- Valid Parenthese
    杂想 · 警醒
    LeetCode -- Length of Last Word
    LeetCode -- Valid Sudoku
    LeetCode -- Binary Tree Level Order Traversal II
  • 原文地址:https://www.cnblogs.com/whuqin/p/4982020.html
Copyright © 2011-2022 走看看