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 #define LOGGER_LOG_FILENAME_KEY "sslproxy_log_filename"
53 #define LOGGER_ROTATION_KEY "sslproxy_rotation"
54 #define LOGGER_MAX_BACKUP_INDEX_KEY "sslproxy_max_backup_index"
55 #define LOGGER_MAX_FILE_SIZE_KEY "sslproxy_max_filesize"
56 #define LOGGER_ROTATION_TIMING_KEY "sslproxy_rotation_timing"
57 #define LOGGER_ROTATION_TIMING_VALUE_KEY "sslproxy_rotation_timing_value"
59 #define LOGGER_CONN_LOG_FILENAME_KEY "conn_log_filename"
60 #define LOGGER_CONN_ROTATION_KEY "conn_rotation"
61 #define LOGGER_CONN_MAX_BACKUP_INDEX_KEY "conn_max_backup_index"
62 #define LOGGER_CONN_MAX_FILE_SIZE_KEY "conn_max_filesize"
63 #define LOGGER_CONN_ROTATION_TIMING_KEY "conn_rotation_timing"
64 #define LOGGER_CONN_ROTATION_TIMING_VALUE_KEY "conn_rotation_timing_value"
66 //! for transration between log4cxx::LevelPtr and LOGER_LEVEL_TAG
67 char l7vs::LoggerImpl::categoryTable[][LOGGER_CATEGORY_NUM] = {
74 "sslproxy_connection",
79 //! for transration between string and LOGGER_CATEGORY_TAG
80 log4cxx::LevelPtr l7vs::LoggerImpl::levelTable[LOGGER_LEVEL_NUM];
83 * returns single instance.
88 l7vs::LoggerImpl& l7vs::LoggerImpl::getInstance()
91 instance = new LoggerImpl;
96 //! static Logger instance pointer initialized by 0.
97 l7vs::LoggerImpl* l7vs::LoggerImpl::instance = 0;
100 * initialize function.
101 * logger initialized to use syslogappender and fileappender(/dev/console)
104 * @retval true succeed
105 * @retval false failed
107 bool l7vs::LoggerImpl::init()
110 if (initialized) return false;
113 log4cxx::LoggerPtr root = log4cxx::Logger::getRootLogger();
117 log4cxx::LayoutPtr layout =
118 new log4cxx::PatternLayout(LOGGER_LAYOUT);
119 log4cxx::net::SyslogAppenderPtr syslogAppender =
120 new log4cxx::net::SyslogAppender(layout, log4cxx::net::SyslogAppender::getFacility(LOGGER_SYSLOG_FACILITY));
121 root->addAppender(syslogAppender);
122 log4cxx::WriterAppender* consoleAppender =
123 new log4cxx::ConsoleAppender( layout, log4cxx::ConsoleAppender::getSystemErr() );
124 root->addAppender(consoleAppender);
126 for (LOG_CATEGORY_TAG cat = LOG_CAT_NONE; cat < LOG_CAT_END; ++cat) {
127 log4cxx::Logger::getLogger(categoryTable[cat])->setLevel(categoryLevel[cat]);
130 catch (const std::exception& e) {
131 std::ostringstream oss;
132 oss << "Logger Initialization Failed : " << e.what();
133 errorConf(6, oss.str(), __FILE__, __LINE__);
137 ret = gethostname(hostname, HOST_NAME_LEN);
139 if (LOG_LV_WARN >= this->getLogLevel(loggerCategory)) {
140 this->putLogWarn(loggerCategory,1, "Fail to get Hostname", __FILE__, __LINE__);
144 if (LOG_LV_INFO >= this->getLogLevel(loggerCategory)) {
145 this->putLogInfo(loggerCategory,1, "Logger Initialized.", __FILE__, __LINE__);
153 * error handling function.
154 * if error occured, switch appenders to syslogappender and fileappender(/dev/console)
155 * message will output to syslog/fileappender appender
157 * @param log message id
159 * @param current file
160 * @param current line
163 void l7vs::LoggerImpl::errorConf(unsigned int message_id, const std::string& errorMessage, const char* file, int line)
166 log4cxx::LogManager::resetConfiguration();
167 log4cxx::LoggerPtr root = log4cxx::Logger::getRootLogger();
170 log4cxx::LayoutPtr layout =
171 new log4cxx::PatternLayout(LOGGER_LAYOUT);
172 log4cxx::net::SyslogAppenderPtr syslogAppender =
173 new log4cxx::net::SyslogAppender(layout, log4cxx::net::SyslogAppender::getFacility(LOGGER_SYSLOG_FACILITY));
174 log4cxx::ConsoleAppender* consoleAppender = new log4cxx::ConsoleAppender(layout, log4cxx::ConsoleAppender::getSystemErr() );
175 root->addAppender(consoleAppender);
176 root->addAppender(syslogAppender);
178 for (LOG_CATEGORY_TAG cat = LOG_CAT_NONE; cat < LOG_CAT_END; ++cat) {
179 log4cxx::Logger::getLogger(categoryTable[cat])->setLevel(categoryLevel[cat]);
183 snprintf(buf, BUF_LEN, "%s%d%03d%04d %s %s", LOGGER_PROCESS_ID, LOG_LV_FATAL, loggerCategory, message_id, errorMessage.c_str(), hostname);
184 log4cxx::Logger::getLogger(categoryTable[loggerCategory])->forcedLog(log4cxx::Level::getFatal(), buf, log4cxx::spi::LocationInfo(file, "", line));
186 catch (const std::exception& e) {
187 std::ostringstream oss;
188 oss << "Logger Error Output Failed : " << e.what() << "\n";
189 fputs(oss.str().c_str(), stderr);
194 * load the logger parameter.
195 * get settings from parameter component, and configure log4cxx property
200 void l7vs::LoggerImpl::loadConf()
202 for (LOG_CATEGORY_TAG cat = LOG_CAT_NONE; cat < LOG_CAT_END; ++cat) {
203 if (cat == LOG_CAT_SSLPROXY_CONNECTION) {
204 // Connection category Logger setting.
205 log_filename_key = LOGGER_CONN_LOG_FILENAME_KEY;
206 rotation_key = LOGGER_CONN_ROTATION_KEY;
207 max_backup_index_key = LOGGER_CONN_MAX_BACKUP_INDEX_KEY;
208 max_file_size_key = LOGGER_CONN_MAX_FILE_SIZE_KEY;
209 rotation_timing_key = LOGGER_CONN_ROTATION_TIMING_KEY;
210 rotation_timing_value_key = LOGGER_CONN_ROTATION_TIMING_VALUE_KEY;
211 /*-------- DEBUG LOG for sslproxy --------*/
212 if (LOG_LV_DEBUG >= this->getLogLevel(loggerCategory)) {
213 std::ostringstream oss;
214 oss << "function : void l7vs::LoggerImpl::loadConf() : "
215 << "Connection category Logger setting. "
217 this->putLogDebug(loggerCategory, 1, oss.str(), __FILE__, __LINE__);
219 /*------ DEBUG LOG END for sslproxy ------*/
221 // Other category Logger setting.
222 log_filename_key = LOGGER_LOG_FILENAME_KEY;
223 rotation_key = LOGGER_ROTATION_KEY;
224 max_backup_index_key = LOGGER_MAX_BACKUP_INDEX_KEY;
225 max_file_size_key = LOGGER_MAX_FILE_SIZE_KEY;
226 rotation_timing_key = LOGGER_ROTATION_TIMING_KEY;
227 rotation_timing_value_key = LOGGER_ROTATION_TIMING_VALUE_KEY;
228 /*-------- DEBUG LOG for sslproxy --------*/
229 if (LOG_LV_DEBUG >= this->getLogLevel(loggerCategory) &&
230 cat != LOG_CAT_SSLPROXY_LOGGER) {
231 std::ostringstream oss;
232 oss << "function : void l7vs::LoggerImpl::loadConf() : "
233 << "Other category Logger setting. "
235 this->putLogDebug(loggerCategory, 2, oss.str(), __FILE__, __LINE__);
237 /*------ DEBUG LOG END for sslproxy ------*/
239 loadCategoryLoggerConf(cat);
241 if (LOG_LV_INFO >= this->getLogLevel(loggerCategory)) {
242 std::ostringstream oss;
243 oss << "Logger Configuration Succeed.";
244 this->putLogInfo(loggerCategory,2, oss.str(), __FILE__, __LINE__);
247 /*-------- DEBUG LOG for sslproxy --------*/
248 if (LOG_LV_DEBUG >= this->getLogLevel(loggerCategory)) {
249 std::ostringstream oss;
250 oss << "out_function : void l7vs::LoggerImpl::loadConf() : "
251 << "loadCategoryLoggerConf() END.";
252 this->putLogDebug(loggerCategory, 3, oss.str(), __FILE__, __LINE__);
254 /*------ DEBUG LOG END for sslproxy ------*/
258 * load the category logger parameter.
259 * get settings from parameter component of each category, and configure log4cxx property
261 * @param[in] catnum LOG_CATEGORY_TAG
264 void l7vs::LoggerImpl::loadCategoryLoggerConf(LOG_CATEGORY_TAG catnum)
269 if (l7vs::Parameter::getInstance().isStringExist(PARAM_COMP_LOGGER, log_filename_key)) {
270 logFilename = l7vs::Parameter::getInstance().getStringValue(PARAM_COMP_LOGGER, log_filename_key);
273 if (LOG_LV_ERROR >= this->getLogLevel(loggerCategory)) {
274 this->putLogError(loggerCategory,1, "Not Exist Log Filename Setting.", __FILE__, __LINE__);
276 throw std::logic_error("Not Exist Log Filename Setting.");
280 if (l7vs::Parameter::getInstance().isStringExist(PARAM_COMP_LOGGER, rotation_key)) {
281 std::string rotationStr = l7vs::Parameter::getInstance().getStringValue(PARAM_COMP_LOGGER, rotation_key);
282 if ("size" == rotationStr) rotation = LOG_ROT_SIZE;
283 else if ("date" == rotationStr) rotation = LOG_ROT_DATE;
284 else if ("datesize" == rotationStr) rotation = LOG_ROT_DATESIZE;
286 if (LOG_LV_ERROR >= this->getLogLevel(loggerCategory)) {
287 this->putLogError(loggerCategory,2, "Invalid Log Rotation Setting.", __FILE__, __LINE__);
289 throw std::logic_error("Invalid Log Rotation Setting.");
293 if (LOG_LV_ERROR >= this->getLogLevel(loggerCategory)) {
294 this->putLogError(loggerCategory,3, "Not Exist Log Rotation Setting.", __FILE__, __LINE__);
296 throw std::logic_error("Not Exist Log Rotation Setting.");
299 //get max backup index
300 if (l7vs::Parameter::getInstance().isStringExist(PARAM_COMP_LOGGER, max_backup_index_key)) {
301 std::string maxBackupIndexStr = l7vs::Parameter::getInstance().getStringValue(PARAM_COMP_LOGGER, max_backup_index_key);
303 maxBackupIndex = lexical_cast<unsigned int>(maxBackupIndexStr);
305 catch (const l7vs::bad_lexical_cast& bc) {
306 if (LOG_LV_ERROR >= this->getLogLevel(loggerCategory)) {
307 this->putLogError(loggerCategory,4, "Invalid MaxBackupIndex Value.", __FILE__, __LINE__);
309 throw std::logic_error("Invalid MaxBackupIndex Value.");
311 if (LOGGER_BACKUP_INDEX_LOWER_LIMIT > maxBackupIndex) {
312 std::ostringstream oss;
313 oss << "Max Backup Index must at least " << LOGGER_BACKUP_INDEX_LOWER_LIMIT << ".";
314 if (LOG_LV_ERROR >= this->getLogLevel(loggerCategory)) {
315 this->putLogError(loggerCategory,5, oss.str(), __FILE__, __LINE__);
317 throw std::logic_error(oss.str());
319 if (LOGGER_BACKUP_INDEX_LIMIT < maxBackupIndex) {
320 std::ostringstream oss;
321 oss << "Max Backup Index must at most " << LOGGER_BACKUP_INDEX_LIMIT << ".";
322 if (LOG_LV_ERROR >= this->getLogLevel(loggerCategory)) {
323 this->putLogError(loggerCategory,6, oss.str(), __FILE__, __LINE__);
325 throw std::logic_error(oss.str());
329 if (LOG_LV_ERROR >= this->getLogLevel(loggerCategory)) {
330 this->putLogError(loggerCategory,7, "Not Exist Log MaxBackupIndex Setting.", __FILE__, __LINE__);
332 throw std::logic_error("Not Exist Log MaxBackupIndex Setting.");
335 if (LOG_ROT_SIZE == rotation || LOG_ROT_DATESIZE == rotation) {
337 std::string maxFileSizeStr;
338 if (l7vs::Parameter::getInstance().isStringExist(PARAM_COMP_LOGGER, max_file_size_key)) {
339 maxFileSizeStr = l7vs::Parameter::getInstance().getStringValue(PARAM_COMP_LOGGER, max_file_size_key);
342 if (LOG_LV_ERROR >= this->getLogLevel(loggerCategory)) {
343 this->putLogError(loggerCategory,8, "Not Exist Log MaxFileSize Setting.", __FILE__, __LINE__);
345 throw std::logic_error("Not Exist Log MaxFileSize Setting.");
348 std::string size_val;
349 std::string last_str = maxFileSizeStr.substr(maxFileSizeStr.length() - 1, 1);
350 // when unit was specified
351 if (("K" == last_str) || ("M" == last_str) || ("G" == last_str)) {
352 size_val = maxFileSizeStr.substr(0, maxFileSizeStr.length() - 1);
355 size_val = maxFileSizeStr;
359 maxFileSize = lexical_cast<size_t>(size_val);
361 catch (const l7vs::bad_lexical_cast& bc) {
362 if (LOG_LV_ERROR >= this->getLogLevel(loggerCategory)) {
363 this->putLogError(loggerCategory,9, "Invalid FileSize Value.", __FILE__, __LINE__);
365 throw std::logic_error("Invalid FileSize Value.");
368 if ("K" == last_str) {
369 if ((ULLONG_MAX / 1024) < maxFileSize) {
370 if (LOG_LV_ERROR >= this->getLogLevel(loggerCategory)) {
371 this->putLogError(loggerCategory,10, "Invalid FileSize Value.", __FILE__, __LINE__);
373 throw std::logic_error("Invalid FileSize Value.");
375 maxFileSize = maxFileSize * 1024;
377 else if ("M" == last_str) {
378 if ((ULLONG_MAX / 1024 / 1024) < maxFileSize) {
379 if (LOG_LV_ERROR >= this->getLogLevel(loggerCategory)) {
380 this->putLogError(loggerCategory,11, "Invalid FileSize Value.", __FILE__, __LINE__);
382 throw std::logic_error("Invalid FileSize Value.");
384 maxFileSize = maxFileSize * 1024 * 1024;
386 else if ("G" == last_str) {
387 if ((ULLONG_MAX / 1024 / 1024 / 1024) < maxFileSize) {
388 if (LOG_LV_ERROR >= this->getLogLevel(loggerCategory)) {
389 this->putLogError(loggerCategory,12, "Invalid FileSize Value.", __FILE__, __LINE__);
391 throw std::logic_error("Invalid FileSize Value.");
393 maxFileSize = maxFileSize * 1024 * 1024 * 1024;
395 if (LOGGER_FILESIZE_LOWER_LIMIT > maxFileSize) {
396 int limit = LOGGER_FILESIZE_LOWER_LIMIT;
397 std::ostringstream oss;
398 oss << "FileSize must at least " << limit << " bytes.";
399 if (LOG_LV_ERROR >= this->getLogLevel(loggerCategory)) {
400 this->putLogError(loggerCategory,13, oss.str(), __FILE__, __LINE__);
402 throw std::logic_error(oss.str());
406 if (LOG_ROT_DATE == rotation || LOG_ROT_DATESIZE == rotation) {
407 // get rotation timing
408 if (l7vs::Parameter::getInstance().isStringExist(PARAM_COMP_LOGGER, rotation_timing_key)) {
409 std::string rotationTimingStr = l7vs::Parameter::getInstance().getStringValue(PARAM_COMP_LOGGER, rotation_timing_key);
410 if ("year" == rotationTimingStr) rotationTiming = LOG_TIM_YEAR;
411 else if ("month" == rotationTimingStr) rotationTiming = LOG_TIM_MONTH;
412 else if ("week" == rotationTimingStr) rotationTiming = LOG_TIM_WEEK;
413 else if ("date" == rotationTimingStr) rotationTiming = LOG_TIM_DATE;
414 else if ("hour" == rotationTimingStr) rotationTiming = LOG_TIM_HOUR;
416 if (LOG_LV_ERROR >= this->getLogLevel(loggerCategory)) {
417 this->putLogError(loggerCategory,14, "Invalid Log RotationTiming Setting.", __FILE__, __LINE__);
419 throw std::logic_error("Invalid Log RotationTiming Setting.");
423 if (LOG_LV_ERROR >= this->getLogLevel(loggerCategory)) {
424 this->putLogError(loggerCategory,15, "Not Exist Log RotaionTiming Setting.", __FILE__, __LINE__);
426 throw std::logic_error("Not Exist Log RotaionTiming Setting.");
429 if (LOG_TIM_YEAR == rotationTiming) {
430 if (l7vs::Parameter::getInstance().isStringExist(PARAM_COMP_LOGGER, rotation_timing_value_key)) {
431 ret = l7vs::Parameter::getInstance().getStringValue(PARAM_COMP_LOGGER, rotation_timing_value_key);
433 std::string::size_type fpos = 0;
434 std::string::size_type rpos = 0;
440 rpos = ret.find_first_of('/', fpos);
441 if (std::string::npos != rpos) {
442 std::string monthStr = ret.substr(fpos, rpos - fpos);
444 month = lexical_cast<int>(monthStr);
446 catch (const l7vs::bad_lexical_cast& bc) {
447 if (LOG_LV_ERROR >= this->getLogLevel(loggerCategory)) {
448 this->putLogError(loggerCategory,16, "Parse Timing Year Error.", __FILE__, __LINE__);
450 throw std::logic_error("Parse Timing Year Error.");
452 if (1 > month || month > 12) {
453 if (LOG_LV_ERROR >= this->getLogLevel(loggerCategory)) {
454 this->putLogError(loggerCategory,17, "Parse Timing Year Error.", __FILE__, __LINE__);
456 throw std::logic_error("Parse Timing Year Error.");
460 rpos = ret.find_first_of(' ', fpos);
461 if (std::string::npos != rpos) {
462 std::string dateStr = ret.substr(fpos, rpos - fpos);
464 date = lexical_cast<int>(dateStr);
466 catch (const l7vs::bad_lexical_cast& bc) {
467 if (LOG_LV_ERROR >= this->getLogLevel(loggerCategory)) {
468 this->putLogError(loggerCategory,18, "Parse Timing Year Error.", __FILE__, __LINE__);
470 throw std::logic_error("Parse Timing Year Error.");
472 if (1 > date || date > 31) {
473 if (LOG_LV_ERROR >= this->getLogLevel(loggerCategory)) {
474 this->putLogError(loggerCategory,19, "Parse Timing Year Error.", __FILE__, __LINE__);
476 throw std::logic_error("Parse Timing Year Error.");
478 int dates[] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
479 if (date > dates[month - 1]) {
480 if (LOG_LV_ERROR >= this->getLogLevel(loggerCategory)) {
481 this->putLogError(loggerCategory,20, "Parse Timing Year Error.", __FILE__, __LINE__);
483 throw std::logic_error("Parse Timing Year Error.");
487 rpos = ret.find_first_of(':', fpos);
488 if (std::string::npos != rpos) {
489 std::string hourStr = ret.substr(fpos, rpos - fpos);
491 hour = lexical_cast<int>(hourStr);
493 catch (const l7vs::bad_lexical_cast& bc) {
494 if (LOG_LV_ERROR >= this->getLogLevel(loggerCategory)) {
495 this->putLogError(loggerCategory,21, "Parse Timing Year Error.", __FILE__, __LINE__);
497 throw std::logic_error("Parse Timing Year Error.");
499 if (0 > hour || hour > 23) {
500 if (LOG_LV_ERROR >= this->getLogLevel(loggerCategory)) {
501 this->putLogError(loggerCategory,22, "Parse Timing Year Error.", __FILE__, __LINE__);
503 throw std::logic_error("Parse Timing Year Error.");
506 std::string minuteStr = ret.substr(rpos + 1);
508 minute = lexical_cast<int>(minuteStr);
510 catch (const l7vs::bad_lexical_cast& bc) {
511 if (LOG_LV_ERROR >= this->getLogLevel(loggerCategory)) {
512 this->putLogError(loggerCategory,23, "Parse Timing Year Error.", __FILE__, __LINE__);
514 throw std::logic_error("Parse Timing Year Error.");
516 if (0 > minute || minute > 59) {
517 if (LOG_LV_ERROR >= this->getLogLevel(loggerCategory)) {
518 this->putLogError(loggerCategory,24, "Parse Timing Year Error.", __FILE__, __LINE__);
520 throw std::logic_error("Parse Timing Year Error.");
524 if (LOG_LV_ERROR >= this->getLogLevel(loggerCategory)) {
525 this->putLogError(loggerCategory,25, "Parse Timing Year Error.", __FILE__, __LINE__);
527 throw std::logic_error("Parse Timing Year Error.");
531 if (LOG_LV_ERROR >= this->getLogLevel(loggerCategory)) {
532 this->putLogError(loggerCategory,26, "Parse Timing Year Error.", __FILE__, __LINE__);
534 throw std::logic_error("Parse Timing Year Error.");
538 if (LOG_LV_ERROR >= this->getLogLevel(loggerCategory)) {
539 this->putLogError(loggerCategory,27, "Parse Timing Year Error.", __FILE__, __LINE__);
541 throw std::logic_error("Parse Timing Year Error.");
544 // format to internal rotation timing value expresson
545 std::ostringstream oss;
546 oss << std::setfill('0') << std::setw(2) << month
547 << std::setfill('0') << std::setw(2) << date
548 << std::setfill('0') << std::setw(2) << hour
549 << std::setfill('0') << std::setw(2) << minute;
551 rotationTimingValue = oss.str();
555 if (LOG_LV_ERROR >= this->getLogLevel(loggerCategory)) {
556 this->putLogError(loggerCategory,28, "Not Exist Log RotaionTiming Year Setting.", __FILE__, __LINE__);
558 throw std::logic_error("Not Exist Log RotaionTiming Year Setting.");
562 if (LOG_TIM_MONTH == rotationTiming) {
563 if (l7vs::Parameter::getInstance().isStringExist(PARAM_COMP_LOGGER, rotation_timing_value_key)) {
564 ret = l7vs::Parameter::getInstance().getStringValue(PARAM_COMP_LOGGER, rotation_timing_value_key);
566 std::string::size_type fpos = 0;
567 std::string::size_type rpos = 0;
572 rpos = ret.find_first_of(' ', fpos);
573 if (std::string::npos != rpos) {
574 std::string dateStr = ret.substr(fpos, rpos - fpos);
576 date = lexical_cast<int>(dateStr);
578 catch (const l7vs::bad_lexical_cast& bc) {
579 if (LOG_LV_ERROR >= this->getLogLevel(loggerCategory)) {
580 this->putLogError(loggerCategory,29, "Parse Timing Month Error.", __FILE__, __LINE__);
582 throw std::logic_error("Parse Timing Month Error.");
584 if (1 > date || date > 31) {
585 if (LOG_LV_ERROR >= this->getLogLevel(loggerCategory)) {
586 this->putLogError(loggerCategory,30, "Parse Timing Month Error.", __FILE__, __LINE__);
588 throw std::logic_error("Parse Timing Month Error.");
592 rpos = ret.find_first_of(':', fpos);
593 if (std::string::npos != rpos) {
594 std::string hourStr = ret.substr(fpos, rpos - fpos);
596 hour = lexical_cast<int>(hourStr);
598 catch (const l7vs::bad_lexical_cast& bc) {
599 if (LOG_LV_ERROR >= this->getLogLevel(loggerCategory)) {
600 this->putLogError(loggerCategory,31, "Parse Timing Month Error.", __FILE__, __LINE__);
602 throw std::logic_error("Parse Timing Month Error.");
604 if (0 > hour || hour > 23) {
605 if (LOG_LV_ERROR >= this->getLogLevel(loggerCategory)) {
606 this->putLogError(loggerCategory,32, "Parse Timing Month Error.", __FILE__, __LINE__);
608 throw std::logic_error("Parse Timing Month Error.");
611 std::string minuteStr = ret.substr(rpos + 1);
613 minute = lexical_cast<int>(minuteStr);
615 catch (const l7vs::bad_lexical_cast& bc) {
616 if (LOG_LV_ERROR >= this->getLogLevel(loggerCategory)) {
617 this->putLogError(loggerCategory,33, "Parse Timing Month Error.", __FILE__, __LINE__);
619 throw std::logic_error("Parse Timing Month Error.");
621 if (0 > minute || minute > 59) {
622 if (LOG_LV_ERROR >= this->getLogLevel(loggerCategory)) {
623 this->putLogError(loggerCategory,34, "Parse Timing Month Error.", __FILE__, __LINE__);
625 throw std::logic_error("Parse Timing Month Error.");
629 if (LOG_LV_ERROR >= this->getLogLevel(loggerCategory)) {
630 this->putLogError(loggerCategory,35, "Parse Timing Month Error.", __FILE__, __LINE__);
632 throw std::logic_error("Parse Timing Month Error.");
636 if (LOG_LV_ERROR >= this->getLogLevel(loggerCategory)) {
637 this->putLogError(loggerCategory,36, "Parse Timing Month Error.", __FILE__, __LINE__);
639 throw std::logic_error("Parse Timing Month Error.");
642 // format to internal rotation timing value expresson
643 std::ostringstream oss;
644 oss << std::setfill('0') << std::setw(2) << date
645 << std::setfill('0') << std::setw(2) << hour
646 << std::setfill('0') << std::setw(2) << minute;
648 rotationTimingValue = oss.str();
652 if (LOG_LV_ERROR >= this->getLogLevel(loggerCategory)) {
653 this->putLogError(loggerCategory,37, "Not Exist Log RotaionTiming Month Setting.", __FILE__, __LINE__);
655 throw std::logic_error("Not Exist Log RotaionTiming Month Setting.");
659 if (LOG_TIM_WEEK == rotationTiming) {
660 if (l7vs::Parameter::getInstance().isStringExist(PARAM_COMP_LOGGER, rotation_timing_value_key)) {
661 ret = l7vs::Parameter::getInstance().getStringValue(PARAM_COMP_LOGGER, rotation_timing_value_key);
663 std::string::size_type fpos = 0;
664 std::string::size_type rpos = 0;
668 rpos = ret.find_first_of(' ', fpos);
670 if (std::string::npos != rpos) {
671 std::string weekStr = ret.substr(fpos, rpos - fpos);
673 if ("sun" == weekStr) week = 0;
674 else if ("mon" == weekStr) week = 1;
675 else if ("tue" == weekStr) week = 2;
676 else if ("wed" == weekStr) week = 3;
677 else if ("thu" == weekStr) week = 4;
678 else if ("fri" == weekStr) week = 5;
679 else if ("sat" == weekStr) week = 6;
681 if (LOG_LV_ERROR >= this->getLogLevel(loggerCategory)) {
682 this->putLogError(loggerCategory,38, "Parse Timing Week Error.", __FILE__, __LINE__);
684 throw std::logic_error("Parse Timing Week Error.");
688 rpos = ret.find_first_of(':', fpos);
689 if (std::string::npos != rpos) {
690 std::string hourStr = ret.substr(fpos, rpos - fpos);
692 hour = lexical_cast<int>(hourStr);
694 catch (const l7vs::bad_lexical_cast& bc) {
695 if (LOG_LV_ERROR >= this->getLogLevel(loggerCategory)) {
696 this->putLogError(loggerCategory,39, "Parse Timing Week Error.", __FILE__, __LINE__);
698 throw std::logic_error("Parse Timing Week Error.");
700 if (0 > hour || hour > 23) {
701 if (LOG_LV_ERROR >= this->getLogLevel(loggerCategory)) {
702 this->putLogError(loggerCategory,40, "Parse Timing Week Error.", __FILE__, __LINE__);
704 throw std::logic_error("Parse Timing Week Error.");
707 std::string minuteStr = ret.substr(rpos + 1);
709 minute = lexical_cast<int>(minuteStr);
711 catch (const l7vs::bad_lexical_cast& bc) {
712 if (LOG_LV_ERROR >= this->getLogLevel(loggerCategory)) {
713 this->putLogError(loggerCategory,41, "Parse Timing Week Error.", __FILE__, __LINE__);
715 throw std::logic_error("Parse Timing Week Error.");
717 if (0 > minute || minute > 59) {
718 if (LOG_LV_ERROR >= this->getLogLevel(loggerCategory)) {
719 this->putLogError(loggerCategory,42, "Parse Timing Week Error.", __FILE__, __LINE__);
721 throw std::logic_error("Parse Timing Week Error.");
725 if (LOG_LV_ERROR >= this->getLogLevel(loggerCategory)) {
726 this->putLogError(loggerCategory,43, "Parse Timing Week Error.", __FILE__, __LINE__);
728 throw std::logic_error("Parse Timing Week Error.");
732 if (LOG_LV_ERROR >= this->getLogLevel(loggerCategory)) {
733 this->putLogError(loggerCategory,44, "Parse Timing Week Error.", __FILE__, __LINE__);
735 throw std::logic_error("Parse Timing Week Error.");
738 // format to internal rotation timing value expresson
739 std::ostringstream oss;
740 oss << std::setfill('0') << std::setw(1) << week
741 << std::setfill('0') << std::setw(2) << hour
742 << std::setfill('0') << std::setw(2) << minute;
744 rotationTimingValue = oss.str();
748 if (LOG_LV_ERROR >= this->getLogLevel(loggerCategory)) {
749 this->putLogError(loggerCategory,45, "Not Exist Log RotaionTiming Week Setting.", __FILE__, __LINE__);
751 throw std::logic_error("Not Exist Log RotaionTiming Week Setting.");
755 if (LOG_TIM_DATE == rotationTiming) {
756 if (l7vs::Parameter::getInstance().isStringExist(PARAM_COMP_LOGGER, rotation_timing_value_key)) {
757 ret = l7vs::Parameter::getInstance().getStringValue(PARAM_COMP_LOGGER, rotation_timing_value_key);
759 std::string::size_type fpos = 0;
760 std::string::size_type rpos = 0;
764 rpos = ret.find_first_of(':', fpos);
765 if (std::string::npos != rpos) {
766 std::string hourStr = ret.substr(fpos, rpos - fpos);
768 hour = lexical_cast<int>(hourStr);
770 catch (const l7vs::bad_lexical_cast& bc) {
771 if (LOG_LV_ERROR >= this->getLogLevel(loggerCategory)) {
772 this->putLogError(loggerCategory,46, "Parse Timing Date Error.", __FILE__, __LINE__);
774 throw std::logic_error("Parse Timing Date Error.");
776 if (0 > hour || hour > 23) {
777 if (LOG_LV_ERROR >= this->getLogLevel(loggerCategory)) {
778 this->putLogError(loggerCategory,47, "Parse Timing Date Error.", __FILE__, __LINE__);
780 throw std::logic_error("Parse Timing Date Error.");
783 std::string minuteStr = ret.substr(rpos + 1);
785 minute = lexical_cast<int>(minuteStr);
787 catch (const l7vs::bad_lexical_cast& bc) {
788 if (LOG_LV_ERROR >= this->getLogLevel(loggerCategory)) {
789 this->putLogError(loggerCategory,48, "Parse Timing Date Error.", __FILE__, __LINE__);
791 throw std::logic_error("Parse Timing Date Error.");
793 if (0 > minute || minute > 59) {
794 if (LOG_LV_ERROR >= this->getLogLevel(loggerCategory)) {
795 this->putLogError(loggerCategory,49, "Parse Timing Date Error.", __FILE__, __LINE__);
797 throw std::logic_error("Parse Timing Date Error.");
801 if (LOG_LV_ERROR >= this->getLogLevel(loggerCategory)) {
802 this->putLogError(loggerCategory,50, "Parse Timing Date Error.", __FILE__, __LINE__);
804 throw std::logic_error("Parse Timing Date Error.");
807 // format to internal rotation timing value expresson
808 std::ostringstream oss;
809 oss << std::setfill('0') << std::setw(2) << hour
810 << std::setfill('0') << std::setw(2) << minute;
812 rotationTimingValue = oss.str();
816 if (LOG_LV_ERROR >= this->getLogLevel(loggerCategory)) {
817 this->putLogError(loggerCategory,51, "Not Exist Log RotaionTiming Date Setting.", __FILE__, __LINE__);
819 throw std::logic_error("Not Exist Log RotaionTiming Date Setting.");
823 if (LOG_TIM_HOUR == rotationTiming) {
824 if (l7vs::Parameter::getInstance().isStringExist(PARAM_COMP_LOGGER, rotation_timing_value_key)) {
825 ret = l7vs::Parameter::getInstance().getStringValue(PARAM_COMP_LOGGER, rotation_timing_value_key);
830 minute = lexical_cast<int>(ret);
832 catch (const l7vs::bad_lexical_cast& bc) {
833 if (LOG_LV_ERROR >= this->getLogLevel(loggerCategory)) {
834 this->putLogError(loggerCategory,52, "Parse Timing Hour Error.", __FILE__, __LINE__);
836 throw std::logic_error("Parse Timing Hour Error.");
838 if (0 > minute || minute > 59) {
839 if (LOG_LV_ERROR >= this->getLogLevel(loggerCategory)) {
840 this->putLogError(loggerCategory,53, "Parse Timing Hour Error.", __FILE__, __LINE__);
842 throw std::logic_error("Parse Timing Hour Error.");
845 // format to internal rotation timing value expresson
846 std::ostringstream oss;
847 oss << std::setfill('0') << std::setw(2) << minute;
849 rotationTimingValue = oss.str();
853 if (LOG_LV_ERROR >= this->getLogLevel(loggerCategory)) {
854 this->putLogError(loggerCategory,54, "Not Exist Log RotaionTiming Hour Setting.", __FILE__, __LINE__);
856 throw std::logic_error("Not Exist Log RotaionTiming Hour Setting.");
862 /*-------- DEBUG LOG for sslproxy --------*/
863 if (LOG_LV_DEBUG >= this->getLogLevel(loggerCategory) &&
864 catnum != LOG_CAT_SSLPROXY_LOGGER) {
865 std::ostringstream oss;
866 oss << "function : void l7vs::LoggerImpl::loadCategoryLoggerConf("
867 << "LOG_CATEGORY_TAG catnum) : "
868 << "Set category Logger appender. "
869 << "catnum = " << catnum;
870 this->putLogDebug(loggerCategory, 4, oss.str(), __FILE__, __LINE__);
872 /*------ DEBUG LOG END for sslproxy ------*/
874 // reset current configuration
875 // when category is LOG_CAT_NONE (first category)
876 log4cxx::helpers::Pool pool;
877 if (catnum == LOG_CAT_NONE) {
878 /*-------- DEBUG LOG for sslproxy --------*/
879 if (LOG_LV_DEBUG >= this->getLogLevel(loggerCategory) &&
880 catnum != LOG_CAT_SSLPROXY_LOGGER) {
881 std::ostringstream oss;
882 oss << "function : l7vs::LoggerImpl::loadCategoryLoggerConf() : "
883 << "Reset all Logger configuration. "
884 << "catnum = " << catnum;
885 this->putLogDebug(loggerCategory, 5, oss.str(), __FILE__, __LINE__);
887 /*------ DEBUG LOG END for sslproxy ------*/
888 log4cxx::LogManager::resetConfiguration();
890 log4cxx::LoggerPtr catlogger = log4cxx::Logger::getLogger(categoryTable[catnum]);
891 if (0 == catlogger) {
892 throw std::logic_error("getLogger Failed.");
894 log4cxx::LayoutPtr layout =
895 new log4cxx::PatternLayout(LOGGER_LAYOUT);
900 // create FixedWindowRollingPolicy
901 log4cxx::rolling::FixedWindowRollingPolicyPtr fixedRollingPolicy =
902 new log4cxx::rolling::FixedWindowRollingPolicy();
905 fixedRollingPolicy->setMinIndex(1);
908 fixedRollingPolicy->setMaxIndex(maxBackupIndex);
910 // setting FileNamePattern
911 std::ostringstream sizeFile;
912 sizeFile << logFilename << "." << LOGGER_FILE_PATTERN;
913 fixedRollingPolicy->setFileNamePattern(sizeFile.str());
915 // create SizeBasedTriggeringPolicy
916 log4cxx::rolling::SizeBasedTriggeringPolicyPtr sizeTriggeringPolicy =
917 new log4cxx::rolling::SizeBasedTriggeringPolicy();
919 // setting maxFileSize
920 sizeTriggeringPolicy->setMaxFileSize(maxFileSize);
922 // create RollingFileAppender
923 log4cxx::rolling::RollingFileAppenderPtr sizeAppender =
924 new log4cxx::rolling::RollingFileAppender();
927 sizeAppender->setLayout(layout);
930 sizeAppender->setRollingPolicy(fixedRollingPolicy);
932 // set TriggeringPolicy
933 sizeAppender->setTriggeringPolicy(sizeTriggeringPolicy);
936 sizeAppender->setFile(logFilename, true, false, LOGGER_DEFAULT_BUFFER_SIZE, pool);
938 // activate appender options
939 sizeAppender->activateOptions(pool);
941 // add size_base_appender to CategoryLogger
942 catlogger->addAppender(sizeAppender);
948 // create StrictTimeBasedRollingPolicy
949 log4cxx::rolling::StrictTimeBasedRollingPolicyPtr strictRollingPolicy =
950 new log4cxx::rolling::StrictTimeBasedRollingPolicy();
953 strictRollingPolicy->setMinIndex(1);
956 strictRollingPolicy->setMaxIndex(maxBackupIndex);
958 // setting FileNamePattern
959 std::ostringstream dateFile;
960 dateFile << logFilename << "." << LOGGER_FILE_PATTERN;
961 strictRollingPolicy->setFileNamePattern(dateFile.str());
963 // setting Rotation Timing
964 strictRollingPolicy->setRotationTiming(rotationTiming);
966 // setting Rotation Timing Value
967 strictRollingPolicy->setRotationTimingValue(rotationTimingValue);
969 //create RollingFileAppender
970 log4cxx::rolling::RollingFileAppenderPtr dateAppender =
971 new log4cxx::rolling::RollingFileAppender();
974 dateAppender->setLayout(layout);
976 // set RollingPolicy (TriggeringPolicy also included RollingPolicy)
977 dateAppender->setRollingPolicy(strictRollingPolicy);
980 dateAppender->setFile(logFilename, true, false, LOGGER_DEFAULT_BUFFER_SIZE, pool);
982 // activate appender options
983 dateAppender->activateOptions(pool);
985 // add date_based_appender to CategoryLogger
986 catlogger->addAppender(dateAppender);
990 default: //LOG_ROT_DATESIZE:
992 // create TimeAndSizeBasedRollingPolicy
993 log4cxx::rolling::TimeAndSizeBasedRollingPolicyPtr timeSizeRollingPolicy =
994 new log4cxx::rolling::TimeAndSizeBasedRollingPolicy();
997 timeSizeRollingPolicy->setMinIndex(1);
1000 timeSizeRollingPolicy->setMaxIndex(maxBackupIndex);
1002 // setting FileNamePattern
1003 std::ostringstream dateSizeFile;
1004 dateSizeFile << logFilename << "." << LOGGER_FILE_PATTERN;
1005 timeSizeRollingPolicy->setFileNamePattern(dateSizeFile.str());
1007 // setting Rotation Timing
1008 timeSizeRollingPolicy->setRotationTiming(rotationTiming);
1010 // setting Rotation Timing Value
1011 timeSizeRollingPolicy->setRotationTimingValue(rotationTimingValue);
1013 // setting MaxFileSize
1014 timeSizeRollingPolicy->setMaxFileSize(maxFileSize);
1016 // create Rolling FileAppender
1017 log4cxx::rolling::RollingFileAppenderPtr dateSizeAppender =
1018 new log4cxx::rolling::RollingFileAppender();
1021 dateSizeAppender->setLayout(layout);
1023 // set RollingPolicy (TriggeringPolicy also included RollingPolicy)
1024 dateSizeAppender->setRollingPolicy(timeSizeRollingPolicy);
1027 dateSizeAppender->setFile(logFilename, true, false, LOGGER_DEFAULT_BUFFER_SIZE, pool);
1029 // activate appender options
1030 dateSizeAppender->activateOptions(pool);
1032 // add time_and_size_based_appender to CategoryLogger
1033 catlogger->addAppender(dateSizeAppender);
1037 //set default log level
1038 for (LOG_CATEGORY_TAG cat = LOG_CAT_NONE; cat < LOG_CAT_END; ++cat) {
1039 log4cxx::Logger::getLogger(categoryTable[cat])->setLevel(categoryLevel[cat]);
1042 //get category level
1043 for (LOG_CATEGORY_TAG cat = LOG_CAT_NONE; cat < LOG_CAT_END; ++cat) {
1044 if (cat == LOG_CAT_NONE) continue;
1045 if (l7vs::Parameter::getInstance().isStringExist(PARAM_COMP_LOGGER, categoryTable[cat])) {
1046 std::string levelStr = l7vs::Parameter::getInstance().getStringValue(PARAM_COMP_LOGGER, categoryTable[cat]);
1047 if ("debug" == levelStr) {
1048 categoryLevel[cat] = log4cxx::Level::getDebug();
1049 log4cxx::Logger::getLogger(categoryTable[cat])->setLevel(categoryLevel[cat]);
1051 else if ("info" == levelStr) {
1052 categoryLevel[cat] = log4cxx::Level::getInfo();
1053 log4cxx::Logger::getLogger(categoryTable[cat])->setLevel(categoryLevel[cat]);
1055 else if ("warn" == levelStr) {
1056 categoryLevel[cat] = log4cxx::Level::getWarn();
1057 log4cxx::Logger::getLogger(categoryTable[cat])->setLevel(categoryLevel[cat]);
1059 else if ("error" == levelStr) {
1060 categoryLevel[cat] = log4cxx::Level::getError();
1061 log4cxx::Logger::getLogger(categoryTable[cat])->setLevel(categoryLevel[cat]);
1063 else if ("fatal" == levelStr) {
1064 categoryLevel[cat] = log4cxx::Level::getFatal();
1065 log4cxx::Logger::getLogger(categoryTable[cat])->setLevel(categoryLevel[cat]);
1068 std::ostringstream oss;
1069 oss << "Invalid Log Category Setting : " << categoryTable[cat];
1070 if (LOG_LV_WARN >= this->getLogLevel(loggerCategory)) {
1071 this->putLogWarn(loggerCategory,2, oss.str(), __FILE__, __LINE__);
1073 categoryLevel[cat] = log4cxx::Level::getInfo();
1074 log4cxx::Logger::getLogger(categoryTable[cat])->setLevel(categoryLevel[cat]);
1078 std::ostringstream oss;
1079 oss << "Not Exist Log Category Setting : " << categoryTable[cat];
1080 if (LOG_LV_WARN >= this->getLogLevel(loggerCategory)) {
1081 this->putLogWarn(loggerCategory,3, oss.str(), __FILE__, __LINE__);
1083 categoryLevel[cat] = log4cxx::Level::getInfo();
1084 log4cxx::Logger::getLogger(categoryTable[cat])->setLevel(categoryLevel[cat]);
1088 catch (const std::exception& e) {
1089 std::ostringstream oss;
1090 oss << "Logger Reload Config Failed : " << e.what();
1091 errorConf(7, oss.str(), __FILE__, __LINE__);
1092 throw std::logic_error(oss.str());
1095 initLogLevelTable();
1099 * initialize LogLevel table.
1100 * initialize the LogLevel table.
1105 void l7vs::LoggerImpl::initLogLevelTable(void)
1109 for (;nCounter < LOGGER_CATEGORY_NUM; nCounter++) {
1110 loglevel[nCounter] = LOG_LV_NONE;