2 * @file logger_impl.cpp
3 * @brief logger module implementation class.
5 * L7VSD: Linux Virtual Server for Layer7 Load Balancing
6 * Copyright (C) 2008 NTT COMWARE Corporation.
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with this library; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
23 **********************************************************************/
28 #include <log4cxx/logmanager.h>
29 #include <log4cxx/helpers/loglog.h>
30 #include <log4cxx/rolling/rollingfileappender.h>
31 #include <log4cxx/rolling/fixedwindowrollingpolicy.h>
32 #include <log4cxx/rolling/sizebasedtriggeringpolicy.h>
33 #include <log4cxx/rolling/timebasedrollingpolicy.h>
34 #include <log4cxx/consoleappender.h>
38 #include "logger_impl.h"
39 #include "parameter.h"
40 #include "lexical_cast.h"
41 #include "strict_time_based_rolling_policy.h"
42 #include "time_and_size_based_rolling_policy.h"
44 #define LOGGER_LAYOUT "%d{%Y/%m/%d %H:%M:%S} [%p] %c %m %t %F:%L%n"
45 #define LOGGER_DEFAULT_BUFFER_SIZE (8 * 1024)
46 #define LOGGER_SYSLOG_FACILITY "USER"
47 #define LOGGER_BACKUP_INDEX_LOWER_LIMIT (1)
48 #define LOGGER_BACKUP_INDEX_LIMIT (12)
49 #define LOGGER_FILESIZE_LOWER_LIMIT (65535)
50 #define LOGGER_FILE_PATTERN "%i"
52 #if defined(LOGGER_PROCESS_VSD)
53 #define LOGGER_LOG_FILENAME_KEY "l7vsd_log_filename"
54 #define LOGGER_ROTATION_KEY "l7vsd_rotation"
55 #define LOGGER_MAX_BACKUP_INDEX_KEY "l7vsd_max_backup_index"
56 #define LOGGER_MAX_FILE_SIZE_KEY "l7vsd_max_filesize"
57 #define LOGGER_ROTATION_TIMING_KEY "l7vsd_rotation_timing"
58 #define LOGGER_ROTATION_TIMING_VALUE_KEY "l7vsd_rotation_timing_value"
60 #elif defined(LOGGER_PROCESS_ADM)
61 #define LOGGER_LOG_FILENAME_KEY "l7vsadm_log_filename"
62 #define LOGGER_ROTATION_KEY "l7vsadm_rotation"
63 #define LOGGER_MAX_BACKUP_INDEX_KEY "l7vsadm_max_backup_index"
64 #define LOGGER_MAX_FILE_SIZE_KEY "l7vsadm_max_filesize"
65 #define LOGGER_ROTATION_TIMING_KEY "l7vsadm_rotation_timing"
66 #define LOGGER_ROTATION_TIMING_VALUE_KEY "l7vsadm_rotation_timing_value"
67 #else //LOGGER_PROCESS_SNM
68 #define LOGGER_LOG_FILENAME_KEY "snmpagent_log_filename"
69 #define LOGGER_ROTATION_KEY "snmpagent_rotation"
70 #define LOGGER_MAX_BACKUP_INDEX_KEY "snmpagent_max_backup_index"
71 #define LOGGER_MAX_FILE_SIZE_KEY "snmpagent_max_filesize"
72 #define LOGGER_ROTATION_TIMING_KEY "snmpagent_rotation_timing"
73 #define LOGGER_ROTATION_TIMING_VALUE_KEY "snmpagent_rotation_timing_value"
76 //! for transration between log4cxx::LevelPtr and LOGER_LEVEL_TAG
77 char l7vs::LoggerImpl::categoryTable[][LOGGER_CATEGORY_NUM] = {
80 "l7vsd_network.bandwidth",
81 "l7vsd_network.num_connection",
83 "l7vsd_virtual_service",
86 "l7vsd_real_server.balancing",
90 "l7vsd_system.memory",
91 "l7vsd_system.socket",
92 "l7vsd_system.signal",
94 "l7vsd_environment.parameter",
104 "l7vsadm_communicate",
105 "l7vsadm_config_result",
111 "snmpagent_start_stop",
112 "snmpagent_manager_receive",
113 "snmpagent_manager_send",
114 "snmpagent_l7vsd_receive",
115 "snmpagent_l7vsd_send",
117 "snmpagent_parameter"
120 //! for transration between string and LOGGER_CATEGORY_TAG
121 log4cxx::LevelPtr l7vs::LoggerImpl::levelTable[LOGGER_LEVEL_NUM];
124 * returns single instance.
129 l7vs::LoggerImpl& l7vs::LoggerImpl::getInstance()
132 instance = new LoggerImpl;
137 //! static Logger instance pointer initialized by 0.
138 l7vs::LoggerImpl* l7vs::LoggerImpl::instance = 0;
141 * initialize function.
142 * logger initialized to use syslogappender and fileappender(/dev/console)
145 * @retval true succeed
146 * @retval false failed
148 bool l7vs::LoggerImpl::init()
151 if (initialized) return false;
154 log4cxx::LoggerPtr root = log4cxx::Logger::getRootLogger();
158 log4cxx::LayoutPtr layout =
159 new log4cxx::PatternLayout(LOGGER_LAYOUT);
160 log4cxx::net::SyslogAppenderPtr syslogAppender =
161 new log4cxx::net::SyslogAppender(layout, log4cxx::net::SyslogAppender::getFacility(LOGGER_SYSLOG_FACILITY));
162 root->addAppender(syslogAppender);
163 log4cxx::WriterAppender* consoleAppender =
164 new log4cxx::ConsoleAppender( layout, log4cxx::ConsoleAppender::getSystemErr() );
165 root->addAppender(consoleAppender);
167 for (LOG_CATEGORY_TAG cat = LOG_CAT_NONE; cat <= getCategoryRangeEnd(LOG_MOD_SNMPAGENT); ++cat) {
168 log4cxx::Logger::getLogger(categoryTable[cat])->setLevel(categoryLevel[cat]);
171 catch (const std::exception& e) {
172 std::ostringstream oss;
173 oss << "Logger Initialization Failed : " << e.what();
174 errorConf(6, oss.str(), __FILE__, __LINE__);
178 ret = gethostname(hostname, HOST_NAME_LEN);
180 if (LOG_LV_WARN >= this->getLogLevel(loggerCategory)) {
181 this->putLogWarn(loggerCategory,1, "Fail to get Hostname", __FILE__, __LINE__);
185 if (LOG_LV_INFO >= this->getLogLevel(loggerCategory)) {
186 this->putLogInfo(loggerCategory,1, "Logger Initialized.", __FILE__, __LINE__);
194 * error handling function.
195 * if error occured, switch appenders to syslogappender and fileappender(/dev/console)
196 * message will output to syslog/fileappender appender
198 * @param log message id
200 * @param current file
201 * @param current line
204 void l7vs::LoggerImpl::errorConf(unsigned int message_id, const std::string& errorMessage, const char* file, int line)
207 log4cxx::LogManager::resetConfiguration();
208 log4cxx::LoggerPtr root = log4cxx::Logger::getRootLogger();
211 log4cxx::LayoutPtr layout =
212 new log4cxx::PatternLayout(LOGGER_LAYOUT);
213 log4cxx::net::SyslogAppenderPtr syslogAppender =
214 new log4cxx::net::SyslogAppender(layout, log4cxx::net::SyslogAppender::getFacility(LOGGER_SYSLOG_FACILITY));
215 log4cxx::ConsoleAppender* consoleAppender = new log4cxx::ConsoleAppender(layout, log4cxx::ConsoleAppender::getSystemErr() );
216 root->addAppender(consoleAppender);
217 root->addAppender(syslogAppender);
219 for (LOG_CATEGORY_TAG cat = LOG_CAT_NONE; cat <= getCategoryRangeEnd(LOG_MOD_SNMPAGENT); ++cat) {
220 log4cxx::Logger::getLogger(categoryTable[cat])->setLevel(categoryLevel[cat]);
224 snprintf(buf, BUF_LEN, "%s%d%03d%04d %s %s", LOGGER_PROCESS_ID, LOG_LV_FATAL, loggerCategory, message_id, errorMessage.c_str(), hostname);
225 log4cxx::Logger::getLogger(categoryTable[loggerCategory])->forcedLog(log4cxx::Level::getFatal(), buf, log4cxx::spi::LocationInfo(file, "", line));
227 catch (const std::exception& e) {
228 std::ostringstream oss;
229 oss << "Logger Error Output Failed : " << e.what() << "\n";
230 fputs(oss.str().c_str(), stderr);
235 * load the logger parameter.
236 * get settings from parameter component, and configure log4cxx property
241 void l7vs::LoggerImpl::loadConf()
246 if (l7vs::Parameter::getInstance().isStringExist(PARAM_COMP_LOGGER, LOGGER_LOG_FILENAME_KEY)) {
247 logFilename = l7vs::Parameter::getInstance().getStringValue(PARAM_COMP_LOGGER, LOGGER_LOG_FILENAME_KEY);
250 if (LOG_LV_ERROR >= this->getLogLevel(loggerCategory)) {
251 this->putLogError(loggerCategory,1, "Not Exist Log Filename Setting.", __FILE__, __LINE__);
253 throw std::logic_error("Not Exist Log Filename Setting.");
257 if (l7vs::Parameter::getInstance().isStringExist(PARAM_COMP_LOGGER, LOGGER_ROTATION_KEY)) {
258 std::string rotationStr = l7vs::Parameter::getInstance().getStringValue(PARAM_COMP_LOGGER, LOGGER_ROTATION_KEY);
259 if ("size" == rotationStr) rotation = LOG_ROT_SIZE;
260 else if ("date" == rotationStr) rotation = LOG_ROT_DATE;
261 else if ("datesize" == rotationStr) rotation = LOG_ROT_DATESIZE;
263 if (LOG_LV_ERROR >= this->getLogLevel(loggerCategory)) {
264 this->putLogError(loggerCategory,2, "Invalid Log Rotation Setting.", __FILE__, __LINE__);
266 throw std::logic_error("Invalid Log Rotation Setting.");
270 if (LOG_LV_ERROR >= this->getLogLevel(loggerCategory)) {
271 this->putLogError(loggerCategory,3, "Not Exist Log Rotation Setting.", __FILE__, __LINE__);
273 throw std::logic_error("Not Exist Log Rotation Setting.");
276 //get max backup index
277 if (l7vs::Parameter::getInstance().isStringExist(PARAM_COMP_LOGGER, LOGGER_MAX_BACKUP_INDEX_KEY)) {
278 std::string maxBackupIndexStr = l7vs::Parameter::getInstance().getStringValue(PARAM_COMP_LOGGER, LOGGER_MAX_BACKUP_INDEX_KEY);
280 maxBackupIndex = lexical_cast<unsigned int>(maxBackupIndexStr);
282 catch (const l7vs::bad_lexical_cast& bc) {
283 if (LOG_LV_ERROR >= this->getLogLevel(loggerCategory)) {
284 this->putLogError(loggerCategory,4, "Invalid MaxBackupIndex Value.", __FILE__, __LINE__);
286 throw std::logic_error("Invalid MaxBackupIndex Value.");
288 if (LOGGER_BACKUP_INDEX_LOWER_LIMIT > maxBackupIndex) {
289 std::ostringstream oss;
290 oss << "Max Backup Index must at least " << LOGGER_BACKUP_INDEX_LOWER_LIMIT << ".";
291 if (LOG_LV_ERROR >= this->getLogLevel(loggerCategory)) {
292 this->putLogError(loggerCategory,5, oss.str(), __FILE__, __LINE__);
294 throw std::logic_error(oss.str());
296 if (LOGGER_BACKUP_INDEX_LIMIT < maxBackupIndex) {
297 std::ostringstream oss;
298 oss << "Max Backup Index must at most " << LOGGER_BACKUP_INDEX_LIMIT << ".";
299 if (LOG_LV_ERROR >= this->getLogLevel(loggerCategory)) {
300 this->putLogError(loggerCategory,6, oss.str(), __FILE__, __LINE__);
302 throw std::logic_error(oss.str());
306 if (LOG_LV_ERROR >= this->getLogLevel(loggerCategory)) {
307 this->putLogError(loggerCategory,7, "Not Exist Log MaxBackupIndex Setting.", __FILE__, __LINE__);
309 throw std::logic_error("Not Exist Log MaxBackupIndex Setting.");
312 if (LOG_ROT_SIZE == rotation || LOG_ROT_DATESIZE == rotation) {
314 std::string maxFileSizeStr;
315 if (l7vs::Parameter::getInstance().isStringExist(PARAM_COMP_LOGGER, LOGGER_MAX_FILE_SIZE_KEY)) {
316 maxFileSizeStr = l7vs::Parameter::getInstance().getStringValue(PARAM_COMP_LOGGER, LOGGER_MAX_FILE_SIZE_KEY);
319 if (LOG_LV_ERROR >= this->getLogLevel(loggerCategory)) {
320 this->putLogError(loggerCategory,8, "Not Exist Log MaxFileSize Setting.", __FILE__, __LINE__);
322 throw std::logic_error("Not Exist Log MaxFileSize Setting.");
325 std::string size_val;
326 std::string last_str = maxFileSizeStr.substr(maxFileSizeStr.length() - 1, 1);
327 // when unit was specified
328 if (("K" == last_str) || ("M" == last_str) || ("G" == last_str)) {
329 size_val = maxFileSizeStr.substr(0, maxFileSizeStr.length() - 1);
332 size_val = maxFileSizeStr;
336 maxFileSize = lexical_cast<size_t>(size_val);
338 catch (const l7vs::bad_lexical_cast& bc) {
339 if (LOG_LV_ERROR >= this->getLogLevel(loggerCategory)) {
340 this->putLogError(loggerCategory,9, "Invalid FileSize Value.", __FILE__, __LINE__);
342 throw std::logic_error("Invalid FileSize Value.");
345 if ("K" == last_str) {
346 if ((ULLONG_MAX / 1024) < maxFileSize) {
347 if (LOG_LV_ERROR >= this->getLogLevel(loggerCategory)) {
348 this->putLogError(loggerCategory,10, "Invalid FileSize Value.", __FILE__, __LINE__);
350 throw std::logic_error("Invalid FileSize Value.");
352 maxFileSize = maxFileSize * 1024;
354 else if ("M" == last_str) {
355 if ((ULLONG_MAX / 1024 / 1024) < maxFileSize) {
356 if (LOG_LV_ERROR >= this->getLogLevel(loggerCategory)) {
357 this->putLogError(loggerCategory,11, "Invalid FileSize Value.", __FILE__, __LINE__);
359 throw std::logic_error("Invalid FileSize Value.");
361 maxFileSize = maxFileSize * 1024 * 1024;
363 else if ("G" == last_str) {
364 if ((ULLONG_MAX / 1024 / 1024 / 1024) < maxFileSize) {
365 if (LOG_LV_ERROR >= this->getLogLevel(loggerCategory)) {
366 this->putLogError(loggerCategory,12, "Invalid FileSize Value.", __FILE__, __LINE__);
368 throw std::logic_error("Invalid FileSize Value.");
370 maxFileSize = maxFileSize * 1024 * 1024 * 1024;
372 if (LOGGER_FILESIZE_LOWER_LIMIT > maxFileSize) {
373 int limit = LOGGER_FILESIZE_LOWER_LIMIT;
374 std::ostringstream oss;
375 oss << "FileSize must at least " << limit << " bytes.";
376 if (LOG_LV_ERROR >= this->getLogLevel(loggerCategory)) {
377 this->putLogError(loggerCategory,13, oss.str(), __FILE__, __LINE__);
379 throw std::logic_error(oss.str());
383 if (LOG_ROT_DATE == rotation || LOG_ROT_DATESIZE == rotation) {
384 // get rotation timing
385 if (l7vs::Parameter::getInstance().isStringExist(PARAM_COMP_LOGGER, LOGGER_ROTATION_TIMING_KEY)) {
386 std::string rotationTimingStr = l7vs::Parameter::getInstance().getStringValue(PARAM_COMP_LOGGER, LOGGER_ROTATION_TIMING_KEY);
387 if ("year" == rotationTimingStr) rotationTiming = LOG_TIM_YEAR;
388 else if ("month" == rotationTimingStr) rotationTiming = LOG_TIM_MONTH;
389 else if ("week" == rotationTimingStr) rotationTiming = LOG_TIM_WEEK;
390 else if ("date" == rotationTimingStr) rotationTiming = LOG_TIM_DATE;
391 else if ("hour" == rotationTimingStr) rotationTiming = LOG_TIM_HOUR;
393 if (LOG_LV_ERROR >= this->getLogLevel(loggerCategory)) {
394 this->putLogError(loggerCategory,14, "Invalid Log RotationTiming Setting.", __FILE__, __LINE__);
396 throw std::logic_error("Invalid Log RotationTiming Setting.");
400 if (LOG_LV_ERROR >= this->getLogLevel(loggerCategory)) {
401 this->putLogError(loggerCategory,15, "Not Exist Log RotaionTiming Setting.", __FILE__, __LINE__);
403 throw std::logic_error("Not Exist Log RotaionTiming Setting.");
406 if (LOG_TIM_YEAR == rotationTiming) {
407 if (l7vs::Parameter::getInstance().isStringExist(PARAM_COMP_LOGGER, LOGGER_ROTATION_TIMING_VALUE_KEY)) {
408 ret = l7vs::Parameter::getInstance().getStringValue(PARAM_COMP_LOGGER, LOGGER_ROTATION_TIMING_VALUE_KEY);
410 std::string::size_type fpos = 0;
411 std::string::size_type rpos = 0;
417 rpos = ret.find_first_of('/', fpos);
418 if (std::string::npos != rpos) {
419 std::string monthStr = ret.substr(fpos, rpos - fpos);
421 month = lexical_cast<int>(monthStr);
423 catch (const l7vs::bad_lexical_cast& bc) {
424 if (LOG_LV_ERROR >= this->getLogLevel(loggerCategory)) {
425 this->putLogError(loggerCategory,16, "Parse Timing Year Error.", __FILE__, __LINE__);
427 throw std::logic_error("Parse Timing Year Error.");
429 if (1 > month || month > 12) {
430 if (LOG_LV_ERROR >= this->getLogLevel(loggerCategory)) {
431 this->putLogError(loggerCategory,17, "Parse Timing Year Error.", __FILE__, __LINE__);
433 throw std::logic_error("Parse Timing Year Error.");
437 rpos = ret.find_first_of(' ', fpos);
438 if (std::string::npos != rpos) {
439 std::string dateStr = ret.substr(fpos, rpos - fpos);
441 date = lexical_cast<int>(dateStr);
443 catch (const l7vs::bad_lexical_cast& bc) {
444 if (LOG_LV_ERROR >= this->getLogLevel(loggerCategory)) {
445 this->putLogError(loggerCategory,18, "Parse Timing Year Error.", __FILE__, __LINE__);
447 throw std::logic_error("Parse Timing Year Error.");
449 if (1 > date || date > 31) {
450 if (LOG_LV_ERROR >= this->getLogLevel(loggerCategory)) {
451 this->putLogError(loggerCategory,19, "Parse Timing Year Error.", __FILE__, __LINE__);
453 throw std::logic_error("Parse Timing Year Error.");
455 int dates[] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
456 if (date > dates[month - 1]) {
457 if (LOG_LV_ERROR >= this->getLogLevel(loggerCategory)) {
458 this->putLogError(loggerCategory,20, "Parse Timing Year Error.", __FILE__, __LINE__);
460 throw std::logic_error("Parse Timing Year Error.");
464 rpos = ret.find_first_of(':', fpos);
465 if (std::string::npos != rpos) {
466 std::string hourStr = ret.substr(fpos, rpos - fpos);
468 hour = lexical_cast<int>(hourStr);
470 catch (const l7vs::bad_lexical_cast& bc) {
471 if (LOG_LV_ERROR >= this->getLogLevel(loggerCategory)) {
472 this->putLogError(loggerCategory,21, "Parse Timing Year Error.", __FILE__, __LINE__);
474 throw std::logic_error("Parse Timing Year Error.");
476 if (0 > hour || hour > 23) {
477 if (LOG_LV_ERROR >= this->getLogLevel(loggerCategory)) {
478 this->putLogError(loggerCategory,22, "Parse Timing Year Error.", __FILE__, __LINE__);
480 throw std::logic_error("Parse Timing Year Error.");
483 std::string minuteStr = ret.substr(rpos + 1);
485 minute = lexical_cast<int>(minuteStr);
487 catch (const l7vs::bad_lexical_cast& bc) {
488 if (LOG_LV_ERROR >= this->getLogLevel(loggerCategory)) {
489 this->putLogError(loggerCategory,23, "Parse Timing Year Error.", __FILE__, __LINE__);
491 throw std::logic_error("Parse Timing Year Error.");
493 if (0 > minute || minute > 59) {
494 if (LOG_LV_ERROR >= this->getLogLevel(loggerCategory)) {
495 this->putLogError(loggerCategory,24, "Parse Timing Year Error.", __FILE__, __LINE__);
497 throw std::logic_error("Parse Timing Year Error.");
501 if (LOG_LV_ERROR >= this->getLogLevel(loggerCategory)) {
502 this->putLogError(loggerCategory,25, "Parse Timing Year Error.", __FILE__, __LINE__);
504 throw std::logic_error("Parse Timing Year Error.");
508 if (LOG_LV_ERROR >= this->getLogLevel(loggerCategory)) {
509 this->putLogError(loggerCategory,26, "Parse Timing Year Error.", __FILE__, __LINE__);
511 throw std::logic_error("Parse Timing Year Error.");
515 if (LOG_LV_ERROR >= this->getLogLevel(loggerCategory)) {
516 this->putLogError(loggerCategory,27, "Parse Timing Year Error.", __FILE__, __LINE__);
518 throw std::logic_error("Parse Timing Year Error.");
521 // format to internal rotation timing value expresson
522 std::ostringstream oss;
523 oss << std::setfill('0') << std::setw(2) << month
524 << std::setfill('0') << std::setw(2) << date
525 << std::setfill('0') << std::setw(2) << hour
526 << std::setfill('0') << std::setw(2) << minute;
528 rotationTimingValue = oss.str();
532 if (LOG_LV_ERROR >= this->getLogLevel(loggerCategory)) {
533 this->putLogError(loggerCategory,28, "Not Exist Log RotaionTiming Year Setting.", __FILE__, __LINE__);
535 throw std::logic_error("Not Exist Log RotaionTiming Year Setting.");
539 if (LOG_TIM_MONTH == rotationTiming) {
540 if (l7vs::Parameter::getInstance().isStringExist(PARAM_COMP_LOGGER, LOGGER_ROTATION_TIMING_VALUE_KEY)) {
541 ret = l7vs::Parameter::getInstance().getStringValue(PARAM_COMP_LOGGER, LOGGER_ROTATION_TIMING_VALUE_KEY);
543 std::string::size_type fpos = 0;
544 std::string::size_type rpos = 0;
549 rpos = ret.find_first_of(' ', fpos);
550 if (std::string::npos != rpos) {
551 std::string dateStr = ret.substr(fpos, rpos - fpos);
553 date = lexical_cast<int>(dateStr);
555 catch (const l7vs::bad_lexical_cast& bc) {
556 if (LOG_LV_ERROR >= this->getLogLevel(loggerCategory)) {
557 this->putLogError(loggerCategory,29, "Parse Timing Month Error.", __FILE__, __LINE__);
559 throw std::logic_error("Parse Timing Month Error.");
561 if (1 > date || date > 31) {
562 if (LOG_LV_ERROR >= this->getLogLevel(loggerCategory)) {
563 this->putLogError(loggerCategory,30, "Parse Timing Month Error.", __FILE__, __LINE__);
565 throw std::logic_error("Parse Timing Month Error.");
569 rpos = ret.find_first_of(':', fpos);
570 if (std::string::npos != rpos) {
571 std::string hourStr = ret.substr(fpos, rpos - fpos);
573 hour = lexical_cast<int>(hourStr);
575 catch (const l7vs::bad_lexical_cast& bc) {
576 if (LOG_LV_ERROR >= this->getLogLevel(loggerCategory)) {
577 this->putLogError(loggerCategory,31, "Parse Timing Month Error.", __FILE__, __LINE__);
579 throw std::logic_error("Parse Timing Month Error.");
581 if (0 > hour || hour > 23) {
582 if (LOG_LV_ERROR >= this->getLogLevel(loggerCategory)) {
583 this->putLogError(loggerCategory,32, "Parse Timing Month Error.", __FILE__, __LINE__);
585 throw std::logic_error("Parse Timing Month Error.");
588 std::string minuteStr = ret.substr(rpos + 1);
590 minute = lexical_cast<int>(minuteStr);
592 catch (const l7vs::bad_lexical_cast& bc) {
593 if (LOG_LV_ERROR >= this->getLogLevel(loggerCategory)) {
594 this->putLogError(loggerCategory,33, "Parse Timing Month Error.", __FILE__, __LINE__);
596 throw std::logic_error("Parse Timing Month Error.");
598 if (0 > minute || minute > 59) {
599 if (LOG_LV_ERROR >= this->getLogLevel(loggerCategory)) {
600 this->putLogError(loggerCategory,34, "Parse Timing Month Error.", __FILE__, __LINE__);
602 throw std::logic_error("Parse Timing Month Error.");
606 if (LOG_LV_ERROR >= this->getLogLevel(loggerCategory)) {
607 this->putLogError(loggerCategory,35, "Parse Timing Month Error.", __FILE__, __LINE__);
609 throw std::logic_error("Parse Timing Month Error.");
613 if (LOG_LV_ERROR >= this->getLogLevel(loggerCategory)) {
614 this->putLogError(loggerCategory,36, "Parse Timing Month Error.", __FILE__, __LINE__);
616 throw std::logic_error("Parse Timing Month Error.");
619 // format to internal rotation timing value expresson
620 std::ostringstream oss;
621 oss << std::setfill('0') << std::setw(2) << date
622 << std::setfill('0') << std::setw(2) << hour
623 << std::setfill('0') << std::setw(2) << minute;
625 rotationTimingValue = oss.str();
629 if (LOG_LV_ERROR >= this->getLogLevel(loggerCategory)) {
630 this->putLogError(loggerCategory,37, "Not Exist Log RotaionTiming Month Setting.", __FILE__, __LINE__);
632 throw std::logic_error("Not Exist Log RotaionTiming Month Setting.");
636 if (LOG_TIM_WEEK == rotationTiming) {
637 if (l7vs::Parameter::getInstance().isStringExist(PARAM_COMP_LOGGER, LOGGER_ROTATION_TIMING_VALUE_KEY)) {
638 ret = l7vs::Parameter::getInstance().getStringValue(PARAM_COMP_LOGGER, LOGGER_ROTATION_TIMING_VALUE_KEY);
640 std::string::size_type fpos = 0;
641 std::string::size_type rpos = 0;
645 rpos = ret.find_first_of(' ', fpos);
647 if (std::string::npos != rpos) {
648 std::string weekStr = ret.substr(fpos, rpos - fpos);
650 if ("sun" == weekStr) week = 0;
651 else if ("mon" == weekStr) week = 1;
652 else if ("tue" == weekStr) week = 2;
653 else if ("wed" == weekStr) week = 3;
654 else if ("thu" == weekStr) week = 4;
655 else if ("fri" == weekStr) week = 5;
656 else if ("sat" == weekStr) week = 6;
658 if (LOG_LV_ERROR >= this->getLogLevel(loggerCategory)) {
659 this->putLogError(loggerCategory,38, "Parse Timing Week Error.", __FILE__, __LINE__);
661 throw std::logic_error("Parse Timing Week Error.");
665 rpos = ret.find_first_of(':', fpos);
666 if (std::string::npos != rpos) {
667 std::string hourStr = ret.substr(fpos, rpos - fpos);
669 hour = lexical_cast<int>(hourStr);
671 catch (const l7vs::bad_lexical_cast& bc) {
672 if (LOG_LV_ERROR >= this->getLogLevel(loggerCategory)) {
673 this->putLogError(loggerCategory,39, "Parse Timing Week Error.", __FILE__, __LINE__);
675 throw std::logic_error("Parse Timing Week Error.");
677 if (0 > hour || hour > 23) {
678 if (LOG_LV_ERROR >= this->getLogLevel(loggerCategory)) {
679 this->putLogError(loggerCategory,40, "Parse Timing Week Error.", __FILE__, __LINE__);
681 throw std::logic_error("Parse Timing Week Error.");
684 std::string minuteStr = ret.substr(rpos + 1);
686 minute = lexical_cast<int>(minuteStr);
688 catch (const l7vs::bad_lexical_cast& bc) {
689 if (LOG_LV_ERROR >= this->getLogLevel(loggerCategory)) {
690 this->putLogError(loggerCategory,41, "Parse Timing Week Error.", __FILE__, __LINE__);
692 throw std::logic_error("Parse Timing Week Error.");
694 if (0 > minute || minute > 59) {
695 if (LOG_LV_ERROR >= this->getLogLevel(loggerCategory)) {
696 this->putLogError(loggerCategory,42, "Parse Timing Week Error.", __FILE__, __LINE__);
698 throw std::logic_error("Parse Timing Week Error.");
702 if (LOG_LV_ERROR >= this->getLogLevel(loggerCategory)) {
703 this->putLogError(loggerCategory,43, "Parse Timing Week Error.", __FILE__, __LINE__);
705 throw std::logic_error("Parse Timing Week Error.");
709 if (LOG_LV_ERROR >= this->getLogLevel(loggerCategory)) {
710 this->putLogError(loggerCategory,44, "Parse Timing Week Error.", __FILE__, __LINE__);
712 throw std::logic_error("Parse Timing Week Error.");
715 // format to internal rotation timing value expresson
716 std::ostringstream oss;
717 oss << std::setfill('0') << std::setw(1) << week
718 << std::setfill('0') << std::setw(2) << hour
719 << std::setfill('0') << std::setw(2) << minute;
721 rotationTimingValue = oss.str();
725 if (LOG_LV_ERROR >= this->getLogLevel(loggerCategory)) {
726 this->putLogError(loggerCategory,45, "Not Exist Log RotaionTiming Week Setting.", __FILE__, __LINE__);
728 throw std::logic_error("Not Exist Log RotaionTiming Week Setting.");
732 if (LOG_TIM_DATE == rotationTiming) {
733 if (l7vs::Parameter::getInstance().isStringExist(PARAM_COMP_LOGGER, LOGGER_ROTATION_TIMING_VALUE_KEY)) {
734 ret = l7vs::Parameter::getInstance().getStringValue(PARAM_COMP_LOGGER, LOGGER_ROTATION_TIMING_VALUE_KEY);
736 std::string::size_type fpos = 0;
737 std::string::size_type rpos = 0;
741 rpos = ret.find_first_of(':', fpos);
742 if (std::string::npos != rpos) {
743 std::string hourStr = ret.substr(fpos, rpos - fpos);
745 hour = lexical_cast<int>(hourStr);
747 catch (const l7vs::bad_lexical_cast& bc) {
748 if (LOG_LV_ERROR >= this->getLogLevel(loggerCategory)) {
749 this->putLogError(loggerCategory,46, "Parse Timing Date Error.", __FILE__, __LINE__);
751 throw std::logic_error("Parse Timing Date Error.");
753 if (0 > hour || hour > 23) {
754 if (LOG_LV_ERROR >= this->getLogLevel(loggerCategory)) {
755 this->putLogError(loggerCategory,47, "Parse Timing Date Error.", __FILE__, __LINE__);
757 throw std::logic_error("Parse Timing Date Error.");
760 std::string minuteStr = ret.substr(rpos + 1);
762 minute = lexical_cast<int>(minuteStr);
764 catch (const l7vs::bad_lexical_cast& bc) {
765 if (LOG_LV_ERROR >= this->getLogLevel(loggerCategory)) {
766 this->putLogError(loggerCategory,48, "Parse Timing Date Error.", __FILE__, __LINE__);
768 throw std::logic_error("Parse Timing Date Error.");
770 if (0 > minute || minute > 59) {
771 if (LOG_LV_ERROR >= this->getLogLevel(loggerCategory)) {
772 this->putLogError(loggerCategory,49, "Parse Timing Date Error.", __FILE__, __LINE__);
774 throw std::logic_error("Parse Timing Date Error.");
778 if (LOG_LV_ERROR >= this->getLogLevel(loggerCategory)) {
779 this->putLogError(loggerCategory,50, "Parse Timing Date Error.", __FILE__, __LINE__);
781 throw std::logic_error("Parse Timing Date Error.");
784 // format to internal rotation timing value expresson
785 std::ostringstream oss;
786 oss << std::setfill('0') << std::setw(2) << hour
787 << std::setfill('0') << std::setw(2) << minute;
789 rotationTimingValue = oss.str();
793 if (LOG_LV_ERROR >= this->getLogLevel(loggerCategory)) {
794 this->putLogError(loggerCategory,51, "Not Exist Log RotaionTiming Date Setting.", __FILE__, __LINE__);
796 throw std::logic_error("Not Exist Log RotaionTiming Date Setting.");
800 if (LOG_TIM_HOUR == rotationTiming) {
801 if (l7vs::Parameter::getInstance().isStringExist(PARAM_COMP_LOGGER, LOGGER_ROTATION_TIMING_VALUE_KEY)) {
802 ret = l7vs::Parameter::getInstance().getStringValue(PARAM_COMP_LOGGER, LOGGER_ROTATION_TIMING_VALUE_KEY);
807 minute = lexical_cast<int>(ret);
809 catch (const l7vs::bad_lexical_cast& bc) {
810 if (LOG_LV_ERROR >= this->getLogLevel(loggerCategory)) {
811 this->putLogError(loggerCategory,52, "Parse Timing Hour Error.", __FILE__, __LINE__);
813 throw std::logic_error("Parse Timing Hour Error.");
815 if (0 > minute || minute > 59) {
816 if (LOG_LV_ERROR >= this->getLogLevel(loggerCategory)) {
817 this->putLogError(loggerCategory,53, "Parse Timing Hour Error.", __FILE__, __LINE__);
819 throw std::logic_error("Parse Timing Hour Error.");
822 // format to internal rotation timing value expresson
823 std::ostringstream oss;
824 oss << std::setfill('0') << std::setw(2) << minute;
826 rotationTimingValue = oss.str();
830 if (LOG_LV_ERROR >= this->getLogLevel(loggerCategory)) {
831 this->putLogError(loggerCategory,54, "Not Exist Log RotaionTiming Hour Setting.", __FILE__, __LINE__);
833 throw std::logic_error("Not Exist Log RotaionTiming Hour Setting.");
839 // reset current configuration
840 log4cxx::helpers::Pool pool;
841 log4cxx::LogManager::resetConfiguration();
842 log4cxx::LoggerPtr root = log4cxx::Logger::getRootLogger();
844 throw std::logic_error("getRootLogger Failed.");
846 log4cxx::LayoutPtr layout =
847 new log4cxx::PatternLayout(LOGGER_LAYOUT);
852 // create FixedWindowRollingPolicy
853 log4cxx::rolling::FixedWindowRollingPolicyPtr fixedRollingPolicy =
854 new log4cxx::rolling::FixedWindowRollingPolicy();
857 fixedRollingPolicy->setMinIndex(1);
860 fixedRollingPolicy->setMaxIndex(maxBackupIndex);
862 // setting FileNamePattern
863 std::ostringstream sizeFile;
864 sizeFile << logFilename << "." << LOGGER_FILE_PATTERN;
865 fixedRollingPolicy->setFileNamePattern(sizeFile.str());
867 // create SizeBasedTriggeringPolicy
868 log4cxx::rolling::SizeBasedTriggeringPolicyPtr sizeTriggeringPolicy =
869 new log4cxx::rolling::SizeBasedTriggeringPolicy();
871 // setting maxFileSize
872 sizeTriggeringPolicy->setMaxFileSize(maxFileSize);
874 // create RollingFileAppender
875 log4cxx::rolling::RollingFileAppenderPtr sizeAppender =
876 new log4cxx::rolling::RollingFileAppender();
879 sizeAppender->setLayout(layout);
882 sizeAppender->setRollingPolicy(fixedRollingPolicy);
884 // set TriggeringPolicy
885 sizeAppender->setTriggeringPolicy(sizeTriggeringPolicy);
888 sizeAppender->setFile(logFilename, true, false, LOGGER_DEFAULT_BUFFER_SIZE, pool);
890 // activate appender options
891 sizeAppender->activateOptions(pool);
893 // add size_base_appender to rootLogger
894 root->addAppender(sizeAppender);
900 // create StrictTimeBasedRollingPolicy
901 log4cxx::rolling::StrictTimeBasedRollingPolicyPtr strictRollingPolicy =
902 new log4cxx::rolling::StrictTimeBasedRollingPolicy();
905 strictRollingPolicy->setMinIndex(1);
908 strictRollingPolicy->setMaxIndex(maxBackupIndex);
910 // setting FileNamePattern
911 std::ostringstream dateFile;
912 dateFile << logFilename << "." << LOGGER_FILE_PATTERN;
913 strictRollingPolicy->setFileNamePattern(dateFile.str());
915 // setting Rotation Timing
916 strictRollingPolicy->setRotationTiming(rotationTiming);
918 // setting Rotation Timing Value
919 strictRollingPolicy->setRotationTimingValue(rotationTimingValue);
921 //create RollingFileAppender
922 log4cxx::rolling::RollingFileAppenderPtr dateAppender =
923 new log4cxx::rolling::RollingFileAppender();
926 dateAppender->setLayout(layout);
928 // set RollingPolicy (TriggeringPolicy also included RollingPolicy)
929 dateAppender->setRollingPolicy(strictRollingPolicy);
932 dateAppender->setFile(logFilename, true, false, LOGGER_DEFAULT_BUFFER_SIZE, pool);
934 // activate appender options
935 dateAppender->activateOptions(pool);
937 // add date_based_appender to rootRogger
938 root->addAppender(dateAppender);
942 default: //LOG_ROT_DATESIZE:
944 // create TimeAndSizeBasedRollingPolicy
945 log4cxx::rolling::TimeAndSizeBasedRollingPolicyPtr timeSizeRollingPolicy =
946 new log4cxx::rolling::TimeAndSizeBasedRollingPolicy();
949 timeSizeRollingPolicy->setMinIndex(1);
952 timeSizeRollingPolicy->setMaxIndex(maxBackupIndex);
954 // setting FileNamePattern
955 std::ostringstream dateSizeFile;
956 dateSizeFile << logFilename << "." << LOGGER_FILE_PATTERN;
957 timeSizeRollingPolicy->setFileNamePattern(dateSizeFile.str());
959 // setting Rotation Timing
960 timeSizeRollingPolicy->setRotationTiming(rotationTiming);
962 // setting Rotation Timing Value
963 timeSizeRollingPolicy->setRotationTimingValue(rotationTimingValue);
965 // setting MaxFileSize
966 timeSizeRollingPolicy->setMaxFileSize(maxFileSize);
968 // create Rolling FileAppender
969 log4cxx::rolling::RollingFileAppenderPtr dateSizeAppender =
970 new log4cxx::rolling::RollingFileAppender();
973 dateSizeAppender->setLayout(layout);
975 // set RollingPolicy (TriggeringPolicy also included RollingPolicy)
976 dateSizeAppender->setRollingPolicy(timeSizeRollingPolicy);
979 dateSizeAppender->setFile(logFilename, true, false, LOGGER_DEFAULT_BUFFER_SIZE, pool);
981 // activate appender options
982 dateSizeAppender->activateOptions(pool);
984 // add time_and_size_based_appender to rootLogger
985 root->addAppender(dateSizeAppender);
988 //set default log level
989 for (LOG_CATEGORY_TAG cat = LOG_CAT_NONE; cat <= getCategoryRangeEnd(LOG_MOD_SNMPAGENT); ++cat) {
990 log4cxx::Logger::getLogger(categoryTable[cat])->setLevel(categoryLevel[cat]);
994 for (LOG_CATEGORY_TAG cat = getCategoryRangeStart(loggerProcess); cat <= getCategoryRangeEnd(loggerProcess); ++cat) {
995 if (l7vs::Parameter::getInstance().isStringExist(PARAM_COMP_LOGGER, categoryTable[cat])) {
996 std::string levelStr = l7vs::Parameter::getInstance().getStringValue(PARAM_COMP_LOGGER, categoryTable[cat]);
997 if ("debug" == levelStr) {
998 categoryLevel[cat] = log4cxx::Level::getDebug();
999 log4cxx::Logger::getLogger(categoryTable[cat])->setLevel(categoryLevel[cat]);
1001 else if ("info" == levelStr) {
1002 categoryLevel[cat] = log4cxx::Level::getInfo();
1003 log4cxx::Logger::getLogger(categoryTable[cat])->setLevel(categoryLevel[cat]);
1005 else if ("warn" == levelStr) {
1006 categoryLevel[cat] = log4cxx::Level::getWarn();
1007 log4cxx::Logger::getLogger(categoryTable[cat])->setLevel(categoryLevel[cat]);
1009 else if ("error" == levelStr) {
1010 categoryLevel[cat] = log4cxx::Level::getError();
1011 log4cxx::Logger::getLogger(categoryTable[cat])->setLevel(categoryLevel[cat]);
1013 else if ("fatal" == levelStr) {
1014 categoryLevel[cat] = log4cxx::Level::getFatal();
1015 log4cxx::Logger::getLogger(categoryTable[cat])->setLevel(categoryLevel[cat]);
1018 std::ostringstream oss;
1019 oss << "Invalid Log Category Setting : " << categoryTable[cat];
1020 if (LOG_LV_WARN >= this->getLogLevel(loggerCategory)) {
1021 this->putLogWarn(loggerCategory,2, oss.str(), __FILE__, __LINE__);
1023 categoryLevel[cat] = log4cxx::Level::getInfo();
1024 log4cxx::Logger::getLogger(categoryTable[cat])->setLevel(categoryLevel[cat]);
1028 std::ostringstream oss;
1029 oss << "Not Exist Log Category Setting : " << categoryTable[cat];
1030 if (LOG_LV_WARN >= this->getLogLevel(loggerCategory)) {
1031 this->putLogWarn(loggerCategory,3, oss.str(), __FILE__, __LINE__);
1033 categoryLevel[cat] = log4cxx::Level::getInfo();
1034 log4cxx::Logger::getLogger(categoryTable[cat])->setLevel(categoryLevel[cat]);
1038 catch (const std::exception& e) {
1039 std::ostringstream oss;
1040 oss << "Logger Reload Config Failed : " << e.what();
1041 errorConf(7, oss.str(), __FILE__, __LINE__);
1042 throw std::logic_error(oss.str());
1045 if (LOG_LV_INFO >= this->getLogLevel(loggerCategory)) {
1046 this->putLogInfo(loggerCategory,2, "Logger Configuration Succeed.", __FILE__, __LINE__);
1048 initLogLevelTable();
1052 * initialize LogLevel table.
1053 * initialize the LogLevel table.
1058 void l7vs::LoggerImpl::initLogLevelTable(void)
1062 for (;nCounter < LOGGER_CATEGORY_NUM; nCounter++) {
1063 loglevel[nCounter] = LOG_LV_NONE;