void initializeLogging (const QString &name, const QString &filename, bool isDebug) { LayoutAppender *appender = 0; const size_t MAX_FILE_SIZE_BYTES = 6 * 1024 * 1024; // Size that should satisfy most email servers const unsigned int MAX_BACKUP_INDEX = 2; const bool APPEND_TO_PREVIOUS_FILE = false; // Log to file for development appender = dynamic_cast<LayoutAppender*> (new RollingFileAppender (name.toStdString (), filename.toStdString (), MAX_FILE_SIZE_BYTES, MAX_BACKUP_INDEX, APPEND_TO_PREVIOUS_FILE)); PatternLayout *layout = new PatternLayout (); layout->setConversionPattern ("%d{%H:%M:%S.%l} %-5p %c - %m%n"); appender->setLayout (layout); mainCat = &Category::getRoot (); // Levels are EMERG, FATAL, ALERT, CRIT, ERROR, WARN, NOTICE, INFO, DEBUG. // // Most trace logging is at INFO level, but methods that are called extremely often (like mouse // moves and status bar updates) are at the lower DEBUG level so they are rarely seen if (isDebug) { mainCat->setPriority (Priority::DEBUG); } else { mainCat->setPriority (Priority::INFO); } mainCat->addAppender (appender); }
int CommInpCfg :: redirectLog () { time_t curr_t; struct tm *ptm; char log_file_name[32]; time (&curr_t); ptm = gmtime (&curr_t); strftime (log_file_name, 32, "%Y%m%d_%H%M%S.log", ptm); string AppLogFile(dataOpt__.WorkingPath); AppLogFile.append("/") .append(log_file_name); Category& rootLogCategory = Category::getRoot(); rootLogCategory.removeAllAppenders(); Appender* appender = new FileAppender("Log", AppLogFile); PatternLayout* patternLayout = new PatternLayout(); patternLayout->setConversionPattern("[%d{%Y:%m:%d %H:%M:%S.%I}] %p %c %x: %m%n"); Layout* layout = dynamic_cast<Layout*> (patternLayout); appender->setLayout(layout); cout << "Redirecting logging from stdout to : " << AppLogFile << endl; rootLogCategory.addAppender(appender); return 0; }
DeplagiarismLogger::DeplagiarismLogger() { PatternLayout * consoleLogLayout = new log4cpp::PatternLayout(); PatternLayout * fileLogLayout = new log4cpp::PatternLayout(); try { consoleLogLayout->setConversionPattern("[%p] %d{%k:%M:%S} %m%n"); fileLogLayout->setConversionPattern("[%p] %d{%d.%m.%Y %k:%M:%S.%l} %m%n"); // example: // [ERROR] 10.01.2021 14:32:09.132 start MESSAGE MESSAGE MESSAGE end } catch(...){} logFileAppender = new FileAppender("DefaultAppender", DePlagiarism::DefaultValues::LOG_FILE_NAME); logFileAppender->setLayout(fileLogLayout); logConsoleAppender = new OstreamAppender("Console", &std::cout); logConsoleAppender->setLayout(consoleLogLayout); logCategory = &(Category::getInstance("Main_thread")); logCategory->setAdditivity(true); logCategory->setAppender(logConsoleAppender); logCategory->setAppender(logFileAppender); logCategory->setPriority(Priority::DEBUG); }
CommInpCfg :: CommInpCfg() { // Set up logging with stdout as destination Category& rootLogCategory = Category::getRoot(); rootLogCategory.setPriority(Priority::INFO); rootLogCategory.removeAllAppenders(); Appender* appender = new FileAppender("_", dup(fileno(stdout))); PatternLayout* patternLayout = new PatternLayout(); patternLayout->setConversionPattern("[%d{%Y:%m:%d %H:%M:%S.%I}] %p %c %x: %m%n"); Layout* layout = dynamic_cast<Layout*> (patternLayout); appender->setLayout(layout); rootLogCategory.addAppender(appender); }
void PropertyConfiguratorImpl::setLayout(Appender* appender, const std::string& appenderName) { // determine the type by appenderName std::string tempString; Properties::iterator key = _properties.find(std::string("appender.") + appenderName + ".layout"); if (key == _properties.end()) throw ConfigureFailure(std::string("Missing layout property for appender '") + appenderName + "'"); std::string::size_type length = (*key).second.find_last_of("."); std::string layoutType = (length == std::string::npos) ? (*key).second : (*key).second.substr(length+1); Layout* layout; // and instantiate the appropriate object if (layoutType == "BasicLayout") { layout = new BasicLayout(); } else if (layoutType == "SimpleLayout") { layout = new SimpleLayout(); } else if (layoutType == "PatternLayout") { // need to read the properties to configure this one PatternLayout* patternLayout = new PatternLayout(); key = _properties.find(std::string("appender.") + appenderName + ".layout.ConversionPattern"); if (key == _properties.end()) { // leave default pattern } else { // set pattern patternLayout->setConversionPattern((*key).second); } layout = patternLayout; } else { throw ConfigureFailure(std::string("Unknown layout type '" + layoutType + "' for appender '") + appenderName + "'"); } appender->setLayout(layout); }