输出到控制台
#include <iostream>
#include <log4cplus/helpers/loglog.h>
using namespace std;
using namespace log4cplus::helpers;
void print_message()
{
cout << "Entering print_message()" << endl;
LogLog::getLogLog()->debug(LOG4CPLUS_TEXT("This is a Debug statement"));
LogLog::getLogLog()->warn(LOG4CPLUS_TEXT("This is a Warning"));
LogLog::getLogLog()->error(LOG4CPLUS_TEXT("This is a Error"));
cout << "Exiting print_message()" << endl << endl;
}
int main()
{
print_message();
cout << "Turning on debug" << endl;
LogLog::getLogLog()->setInternalDebugging(true);
print_message();
cout << "Turning on quiet mode" << endl;
LogLog::getLogLog()->setQuietMode(true);
print_message();
system("pause");
return 0;
} 另外一个例子:
#include "log4cplus/logger.h"
#include "log4cplus/consoleappender.h"
#include "log4cplus/loglevel.h"
#include <iomanip>
using namespace std;
using namespace log4cplus;
int main()
{
SharedAppenderPtr append_1(new ConsoleAppender());
append_1->setName(LOG4CPLUS_TEXT("First"));
Logger::getRoot().addAppender(append_1);
Logger root = Logger::getRoot();
Logger test = Logger::getInstance(LOG4CPLUS_TEXT("test"));
LOG4CPLUS_DEBUG(root,
"This is"
<< " a reall"
<< "y long message." << endl
<< "Just testing it out" << endl
<< "What do you think?");
test.setLogLevel(NOT_SET_LOG_LEVEL);
LOG4CPLUS_DEBUG(test, "This is a bool: " << true);
LOG4CPLUS_INFO(test, "This is a char: " << 'x');
LOG4CPLUS_INFO(test, "This is a short: " << (short)-100);
LOG4CPLUS_INFO(test, "This is a unsigned short: " << (unsigned short)100);
LOG4CPLUS_INFO(test, "This is a int: " << (int)1000);
LOG4CPLUS_INFO(test, "This is a unsigned int: " << (unsigned int)1000);
LOG4CPLUS_INFO(test, "This is a long(hex): " << hex << (long)100000000);
LOG4CPLUS_INFO(test, "This is a unsigned long: " << (unsigned long)100000000);
LOG4CPLUS_WARN(test, "This is a float: " << (float)1.2345);
LOG4CPLUS_ERROR(test,
"This is a double: "
<< setprecision(15)
<< (double)1.2345234234);
LOG4CPLUS_FATAL(test,
"This is a long double: "
<< setprecision(15)
<< (long double)123452342342.342);
system("pause");
return 0;
}
再来看几个输出到日志的小例子:
#include <log4cplus/logger.h>
#include <log4cplus/fileappender.h>
#include <log4cplus/layout.h>
#include <log4cplus/ndc.h>
#include <log4cplus/helpers/loglog.h>
using namespace log4cplus;
const int LOOP_COUNT = 20000;
int main()
{
helpers::LogLog::getLogLog()->setInternalDebugging(true);
SharedAppenderPtr append_1(new RollingFileAppender(LOG4CPLUS_TEXT("Test.log"),5*1024,5));
append_1->setName(LOG4CPLUS_TEXT("LOG4PLUS"));
append_1->setLayout(std::auto_ptr<Layout>(new TTCCLayout()));
Logger::getRoot().addAppender(append_1);
Logger root = Logger::getRoot();
Logger test = Logger::getInstance(LOG4CPLUS_TEXT("test"));
Logger subTest = Logger::getInstance(LOG4CPLUS_TEXT("test.subtest"));
for(int i=0; i<LOOP_COUNT; ++i)
{
NDCContextCreator _context(LOG4CPLUS_TEXT("loop"));
LOG4CPLUS_DEBUG(subTest, "Entering loop #" << i);
}
return 0
}
几点说明:
1.setInternalDebuging 是启用内部调试日志,不过具体作用不是很明确
2.Appender是实现日志输出的策略描述
一般情况下,参考以上例子就足够了