强大的log4cxx,rapidxml,boost组合起来更加强大:
log4cxx的典型配置文件:
#include "stdafx.h"
// include log4cxx header files.
#include <log4cxx/logger.h>
#include <log4cxx/basicconfigurator.h>
#include <log4cxx/propertyconfigurator.h>
#include <log4cxx/helpers/exception.h>
#include <rapidxml.hpp>
#include <rapidxml_print.hpp>
#include <iostream>
#include <boost/format.hpp>
log4cxx::LoggerPtr logger(log4cxx::Logger::getRootLogger());
log4cxx::LoggerPtr logger2(log4cxx::Logger::getLogger("secondlog"));
log4cxx::LoggerPtr logger3(log4cxx::Logger::getLogger("thirdlog"));
std::string rapidxml2string(const rapidxml::xml_document<>& doc)
{
std::string doc_s;
rapidxml::print(std::back_inserter(doc_s), doc, 0);
return doc_s;
}
int main(int argc, char **argv)
{
int result = EXIT_SUCCESS;
try
{
LOG4CXX_INFO(logger, "Entering application.");
for ( int i=0; i<10; i++ )
{
LOG4CXX_INFO(logger, "loop index:" << i);
}
for ( int j=0; j<1000; j++ )
{
LOG4CXX_INFO(logger2, "second loop index:" << j);
}
for ( int j=0; j<1000; j++ )
{
LOG4CXX_INFO(logger3, "third loop index:" << j);
}
std::string xml = "<r><name>ken</name><birth nation=\"china\">1983-11</birth></r>";
rapidxml::xml_document<> doc; // character type defaults to char
doc.parse<0>((char*)xml.c_str()); // 0 means default parse flags
std::cout << "Name of my first node is: " << doc.first_node()->name() << "\n";
/// rapid xml control routine
// access
rapidxml::xml_node<> *node = doc.first_node()->first_node("birth");
std::cout << "Node birth has value " << node->value() << "\n";
for (rapidxml::xml_attribute<> *attr = node->first_attribute();
attr; attr = attr->next_attribute())
{
std::cout << "Node birth has attribute " << attr->name() << " ";
std::cout << "with value " << attr->value() << "\n";
}
// modify
rapidxml::xml_node<>* new_node = doc.allocate_node(rapidxml::node_element, "a", "Google");
doc.first_node()->append_node(new_node);
rapidxml::xml_attribute<> *attr = doc.allocate_attribute("href", "google.com");
new_node->append_attribute(attr);
// print
std::cout << "after modified:" << std::endl;
std::string doc_s = rapidxml2string(doc);
LOG4CXX_INFO(logger, boost::format("final xml:%s") % doc_s); // boost::format+log4cxx,powerful
LOG4CXX_INFO(logger, "Exiting application.");
}
catch(log4cxx::helpers::Exception& e)
{
result = EXIT_FAILURE;
}
getchar();
return result;
}
log4cxx的典型配置文件:
log4j.rootLogger=DEBUG, stdout, R
#log4j.rootLogger=debug, R
#log4j.rootLogger=debug, stdout
#log4j.rootLogger=debug
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
# Pattern to output the caller's file name and line number.
log4j.appender.stdout.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss,SSS} %5p [%t] (%F:%L) - %m%n
log4j.appender.R=org.apache.log4j.RollingFileAppender
log4j.appender.R.File=log/example.log
log4j.appender.R.MaxFileSize=2KB
# Keep one backup file
log4j.appender.R.MaxBackupIndex=5
log4j.appender.R.layout=org.apache.log4j.PatternLayout
log4j.appender.R.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss,SSS} %p %t %c - %m%n
#second log
log4j.logger.secondlog=DEBUG, a2
log4j.additivity.secondlog = false
log4j.appender.a2=org.apache.log4j.RollingFileAppender
log4j.appender.a2.File=log/second2.log
log4j.appender.a2.MaxFileSize=2KB
# Keep 5 backup file
log4j.appender.a2.MaxBackupIndex=5
log4j.appender.a2.layout=org.apache.log4j.PatternLayout
log4j.appender.a2.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss,SSS} %p %t %c - %m%n
#third log
log4j.logger.thirdlog=DEBUG, a3
log4j.additivity.thirdlog = false
log4j.appender.a3=org.apache.log4j.RollingFileAppender
log4j.appender.a3.File=log/thirdlog.log
log4j.appender.a3.MaxFileSize=2KB
# Keep 5 backup file
log4j.appender.a3.MaxBackupIndex=5
log4j.appender.a3.layout=org.apache.log4j.PatternLayout
log4j.appender.a3.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss,SSS} %p %t %c - %m%n