1
2 #include <log4cplus/logger.h>
3 #include <log4cplus/consoleappender.h>
4 #include <log4cplus/layout.h>
5 #include <log4cplus/ndc.h>
6 #include <log4cplus/mdc.h>
7 #include <log4cplus/helpers/loglog.h>
8 #include <log4cplus/thread/threads.h>
9 #include <log4cplus/helpers/sleep.h>
10 #include <log4cplus/loggingmacros.h>
11 #include <iostream>
12 #include <string>
13
14 using namespace std;
15 using namespace log4cplus;
16 using namespace log4cplus::helpers;
17
18 int
main()19 main()
20 {
21 cout << "Entering main()..." << endl;
22 log4cplus::initialize ();
23 LogLog::getLogLog()->setInternalDebugging(true);
24 try {
25 SharedObjectPtr<Appender> append_1(new ConsoleAppender());
26 append_1->setName(LOG4CPLUS_TEXT("First"));
27
28 log4cplus::getMDC ().put (LOG4CPLUS_TEXT ("key"),
29 LOG4CPLUS_TEXT ("MDC value"));
30 log4cplus::tstring pattern = LOG4CPLUS_TEXT("%d{%m/%d/%y %H:%M:%S,%Q} [%t] %-5p %c{2} %%%x%% - %X{key} - %m [%l]%n");
31 // std::tstring pattern = LOG4CPLUS_TEXT("%d{%c} [%t] %-5p [%.15c{3}] %%%x%% - %m [%l]%n");
32 append_1->setLayout( std::auto_ptr<Layout>(new PatternLayout(pattern)) );
33 Logger::getRoot().addAppender(append_1);
34
35 Logger logger = Logger::getInstance(LOG4CPLUS_TEXT("test.a.long_logger_name.c.logger"));
36 LOG4CPLUS_DEBUG(logger, "This is the FIRST log message...");
37
38 sleep(1, 0);
39 {
40 NDCContextCreator ndc(LOG4CPLUS_TEXT("second"));
41 LOG4CPLUS_INFO(logger, "This is the SECOND log message...");
42 }
43
44 sleep(1, 0);
45 LOG4CPLUS_WARN(logger, "This is the THIRD log message...");
46
47 sleep(1, 0);
48 LOG4CPLUS_ERROR(logger, "This is the FOURTH log message...");
49
50 sleep(1, 0);
51 LOG4CPLUS_FATAL(logger, "This is the FOURTH log message...");
52 }
53 catch(...) {
54 cout << "Exception..." << endl;
55 Logger::getRoot().log(FATAL_LOG_LEVEL, LOG4CPLUS_TEXT("Exception occured..."));
56 }
57
58 cout << "Exiting main()..." << endl;
59 return 0;
60 }
61
62