In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Network Security >
Share
Shulou(Shulou.com)05/31 Report--
This article will explain in detail how to use log4cpp for everyone. Xiaobian thinks it is quite practical, so share it with everyone for reference. I hope you can gain something after reading this article.
log4cpp is an LGPL-based open source project, is based on excellent log processing tracking project Java language log4j ported over. [@more@]
Log4j introduces a lot of documents, and it is widely used in java field, but this powerful library is not used much by domestic C++ language developers. This article introduces the library from a developer's point of view, so that developers can master this technology as quickly as possible with minimal cost. Here is a brief introduction to the advantages of this project (also log4j advantages), and then divided into principles, manual use steps, configuration file driver use steps, other considerations and other aspects of the discussion. The following discussion is based on log4cpp0.3.4b.
0. advantages
Provide application running context for easy debugging;
Extensible, multiple ways to log, including command line, file, rollback file, memory, syslog server, Win event log, etc.;
Logging levels can be dynamically controlled, adjusting in efficiency and functionality;
All configurations can be dynamically adjusted through configuration files;
Multi-language support, including Java (log4j), C++(log4cpp, log4cplus), C (log4c), python (log4p), etc.
1. principle
Log4cpp has three main components: categories, appenders, and layouts. (For the convenience of everyone, the original English words are used as much as possible in this article)
The layout class controls how output log messages are displayed (what they look like). log4cpp currently provides the following layout format:
log4cpp::BasicLayout //with "timestamp priority (described below) //category (described below) // NDC tag (nested diagnostic contexts described below): Log information ". //eg: 1056638652 INFO main : This is some infolog4cpp::PatternLayout //lets the user specify the output format according to a conversion pattern similar to the printf function in C. Format is defined in the documentation accompanying the code. log4cpp::SimpleLayout //Displayed in the format "priority-log information."
The appender class is used to output logs (formatted by layout) to some device. For example, a file, syslog service, a socket, etc. You can define your own appender class to output log information to other devices, such as your own day processing process, database, etc. The relationship between appender and layout is that layout is attached to appender. After appender class calls layout to process log messages, it records them to a certain device. log4cpp currently provides the following appenders:
log4cpp::IdsaAppender //Send to IDS or logger, see http://jade.cs.uct.ac.za/idsa/log4cpp::FileAppender for details //Output to file log4cpp::RollingFileAppender //Output to rollback file, i.e. rollback log4cpp::OstreamAppender when file reaches a certain size //output to an ostream class log4cpp::RemoteSyslogAppender //Output to remote syslog server log4cpp::StringQueueAppender //memory queue log4cpp:: SyAppender //local sylog 4cpp::Win32 DebugAppeder //Send to default system debugger log4cpp::NTEventLogAppender //Send to win event log
The category class really does the logging, and the two main components are appenders and priority. Priority controls which types of log information can be recorded by this category, the current priority is: NOTSET, DEBUG, INFO, NOTICE, WARN, ERROR, CRIT, ALERT or FATAL/EMERG. Each log message has a priority, and each category has a priority. When the priority of the message is greater than or equal to the priority of the category, the message will be recorded by the category, otherwise it will be ignored. The priority relationships are as follows. The relationship between category class and appender is that multiple appenders are attached to category, so that a log message can be output to multiple devices at the same time.
NOTSET
< DEBUG < INFO < NOTICE < WARN < ERROR < CRIT < ALERT < FATAL = EMERG category被组织成一个树,子category创建时优先级缺省NOTSET,category缺省会继承父category的appender。而如果不希望这种appender的继承关系,log4cpp允许使用additivity 标签,为false时新的appender取代category的appender列表。 为了更好的理解上面的概念下面以手动使用方式举例。 2. 手动使用步骤 手动使用log4cpp的基本步骤如下: 实例化一个layout 对象; 初始化一个appender 对象; 把layout对象附着在appender对象上; 调用log4cpp::Category::getInstance("name"). 实例化一个category对象; 把appender对象附到category上(根据additivity的值取代其他appender或者附加在其他appender后)。 设置category的优先级; // FileName: test_log4cpp1.cpp// Test log4cpp by manual operation.// Announce: use as your own risk.// Compile : g++ -otest1 -llog4cpp test_log4cpp1.cpp// Run : ./test1// Tested : RedHat 7.2 log4cpp0.3.4b// Author : liqun (liqun@nsfocus.com)// Data : 2003-6-27#include "log4cpp/Category.hh"#include "log4cpp/FileAppender.hh"#include "log4cpp/BasicLayout.hh"int main(int argc, char* argv[]){ // 1实例化一个layout 对象 log4cpp::Layout* layout = new log4cpp::BasicLayout(); // 2. 初始化一个appender 对象 log4cpp::Appender* appender = new log4cpp::FileAppender("FileAppender", "./test_log4cpp1.log"); // 3. 把layout对象附着在appender对象上 appender->setLayout(layout); // 4. instantiate a category object log4cpp::Category& warn_log = log4cpp::Category::getInstance("mywarn"); // 5. Set additive to false, replace existing appender warn_log.setAdditivity(false); // 5. Attach appender object to category warn_log.setAppender(appender); // 6. Sets the priority of category, logs below this priority are not logged warn_log.setPriority(log4cpp::Priority::WARN); //record some logs warn_log.info("Program info which cannot be wirten"); warn_log.debug("This debug message will fail to write"); warn_log.alert("Alert info"); //Other logging methods warn_log.log(log4cpp::Priority::WARN, "This will be a logged warning"); log4cpp::Priority::PriorityLevel priority; bool this_is_critical = true; if(this_is_critical) priority = log4cpp::Priority::CRIT; else priority = log4cpp::Priority::DEBUG; warn_log.log(priority,"Importance depends on context"); warn_log.critStream()
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.