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.

57 lines
1.8 KiB

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