You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

61 lines
1.9 KiB

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