logger.cpp
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
21 #include "logger.hpp"
22 
23 #include <log4cxx/logger.h>
24 #include <log4cxx/basicconfigurator.h>
25 #include <log4cxx/xml/domconfigurator.h>
26 #include <log4cxx/propertyconfigurator.h>
27 #include <log4cxx/patternlayout.h>
28 #include <log4cxx/level.h>
29 #include <log4cxx/helpers/exception.h>
30 #include <log4cxx/rollingfileappender.h>
31 
32 #include <boost/algorithm/string.hpp>
33 #include <boost/filesystem.hpp>
34 
35 void
36 INIT_LOG4CXX(const std::string& log4cxxConfPath)
37 {
38  if (boost::filesystem::path(log4cxxConfPath).extension().string() == ".xml") {
39  log4cxx::xml::DOMConfigurator::configure(log4cxxConfPath);
40  }
41  else {
42  log4cxx::PropertyConfigurator::configure(log4cxxConfPath);
43  }
44 }
45 
46 void
47 INIT_LOGGERS(const std::string& logDir, const std::string& logLevel)
48 {
49  static bool configured = false;
50 
51  if (configured) {
52  return;
53  }
54 
55  log4cxx::PatternLayoutPtr
56  layout(new log4cxx::PatternLayout("%date{%s}.%date{SSS} %p: [%c] %m%n"));
57 
58  log4cxx::RollingFileAppender* rollingFileAppender =
59  new log4cxx::RollingFileAppender(layout, logDir+"/nlsr.log", true);
60 
61  rollingFileAppender->setMaxFileSize("10MB");
62  rollingFileAppender->setMaxBackupIndex(10);
63 
64  log4cxx::helpers::Pool p;
65  rollingFileAppender->activateOptions(p);
66 
67  log4cxx::BasicConfigurator::configure(log4cxx::AppenderPtr(rollingFileAppender));
68 
69  if (boost::iequals(logLevel, "none")) {
70  log4cxx::Logger::getRootLogger()->setLevel(log4cxx::Level::getOff());
71  }
72  else {
73  log4cxx::Logger::getRootLogger()->setLevel(log4cxx::Level::toLevel(logLevel));
74  }
75 
76  configured = true;
77 }
78 
79 bool
80 isValidLogLevel(const std::string& logLevel)
81 {
82  return boost::iequals(logLevel, "all") || boost::iequals(logLevel, "trace") ||
83  boost::iequals(logLevel, "debug") || boost::iequals(logLevel, "info") ||
84  boost::iequals(logLevel, "warn") || boost::iequals(logLevel, "error") ||
85  boost::iequals(logLevel, "none");
86 }
bool isValidLogLevel(const std::string &logLevel)
Definition: logger.cpp:80
void INIT_LOG4CXX(const std::string &log4cxxConfPath)
Copyright (c) 2014-2017, The University of Memphis, Regents of the University of California.
Definition: logger.cpp:36
Copyright (c) 2014-2017, The University of Memphis, Regents of the University of California.
void INIT_LOGGERS(const std::string &logDir, const std::string &logLevel)
Definition: logger.cpp:47