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",
80 //! for transration between string and LOGGER_CATEGORY_TAG
81 log4cxx::LevelPtr l7vs::LoggerImpl::levelTable[LOGGER_LEVEL_NUM];
84 * returns single instance.
89 l7vs::LoggerImpl& l7vs::LoggerImpl::getInstance()
92 instance = new LoggerImpl;
97 //! static Logger instance pointer initialized by 0.
98 l7vs::LoggerImpl* l7vs::LoggerImpl::instance = 0;
101 * initialize function.
102 * logger initialized to use syslogappender and fileappender(/dev/console)
105 * @retval true succeed
106 * @retval false failed
108 bool l7vs::LoggerImpl::init()
111 if (initialized) return false;
114 log4cxx::LoggerPtr root = log4cxx::Logger::getRootLogger();
118 log4cxx::LayoutPtr layout =
119 new log4cxx::PatternLayout(LOGGER_LAYOUT);
120 log4cxx::net::SyslogAppenderPtr syslogAppender =
121 new log4cxx::net::SyslogAppender(layout, log4cxx::net::SyslogAppender::getFacility(LOGGER_SYSLOG_FACILITY));
122 root->addAppender(syslogAppender);
123 log4cxx::WriterAppender* consoleAppender =
124 new log4cxx::ConsoleAppender( layout, log4cxx::ConsoleAppender::getSystemErr() );
125 root->addAppender(consoleAppender);
127 for (LOG_CATEGORY_TAG cat = LOG_CAT_NONE; cat < LOG_CAT_END; ++cat) {
128 log4cxx::Logger::getLogger(categoryTable[cat])->setLevel(categoryLevel[cat]);
131 catch (const std::exception& e) {
132 std::ostringstream oss;
133 oss << "Logger Initialization Failed : " << e.what();
134 errorConf(6, oss.str(), __FILE__, __LINE__);
138 ret = gethostname(hostname, HOST_NAME_LEN);
140 if (LOG_LV_WARN >= this->getLogLevel(loggerCategory)) {
141 this->putLogWarn(loggerCategory,1, "Fail to get Hostname", __FILE__, __LINE__);
145 if (LOG_LV_INFO >= this->getLogLevel(loggerCategory)) {
146 this->putLogInfo(loggerCategory,1, "Logger Initialized.", __FILE__, __LINE__);
154 * error handling function.
155 * if error occured, switch appenders to syslogappender and fileappender(/dev/console)
156 * message will output to syslog/fileappender appender
158 * @param log message id
160 * @param current file
161 * @param current line
164 void l7vs::LoggerImpl::errorConf(unsigned int message_id, const std::string& errorMessage, const char* file, int line)
167 log4cxx::LogManager::resetConfiguration();
168 log4cxx::LoggerPtr root = log4cxx::Logger::getRootLogger();
171 log4cxx::LayoutPtr layout =
172 new log4cxx::PatternLayout(LOGGER_LAYOUT);
173 log4cxx::net::SyslogAppenderPtr syslogAppender =
174 new log4cxx::net::SyslogAppender(layout, log4cxx::net::SyslogAppender::getFacility(LOGGER_SYSLOG_FACILITY));
175 log4cxx::ConsoleAppender* consoleAppender = new log4cxx::ConsoleAppender(layout, log4cxx::ConsoleAppender::getSystemErr() );
176 root->addAppender(consoleAppender);
177 root->addAppender(syslogAppender);
179 for (LOG_CATEGORY_TAG cat = LOG_CAT_NONE; cat < LOG_CAT_END; ++cat) {
180 log4cxx::Logger::getLogger(categoryTable[cat])->setLevel(categoryLevel[cat]);
184 snprintf(buf, BUF_LEN, "%s%d%03d%04d %s %s", LOGGER_PROCESS_ID, LOG_LV_FATAL, loggerCategory, message_id, errorMessage.c_str(), hostname);
185 log4cxx::Logger::getLogger(categoryTable[loggerCategory])->forcedLog(log4cxx::Level::getFatal(), buf, log4cxx::spi::LocationInfo(file, "", line));
187 catch (const std::exception& e) {
188 std::ostringstream oss;
189 oss << "Logger Error Output Failed : " << e.what() << "\n";
190 fputs(oss.str().c_str(), stderr);
195 * load the logger parameter.
196 * get settings from parameter component, and configure log4cxx property
201 void l7vs::LoggerImpl::loadConf()
203 for (LOG_CATEGORY_TAG cat = LOG_CAT_NONE; cat < LOG_CAT_END; ++cat) {
204 if (cat == LOG_CAT_SSLPROXY_CONNECTION) {
205 // Connection category Logger setting.
206 log_filename_key = LOGGER_CONN_LOG_FILENAME_KEY;
207 rotation_key = LOGGER_CONN_ROTATION_KEY;
208 max_backup_index_key = LOGGER_CONN_MAX_BACKUP_INDEX_KEY;
209 max_file_size_key = LOGGER_CONN_MAX_FILE_SIZE_KEY;
210 rotation_timing_key = LOGGER_CONN_ROTATION_TIMING_KEY;
211 rotation_timing_value_key = LOGGER_CONN_ROTATION_TIMING_VALUE_KEY;
212 /*-------- DEBUG LOG for sslproxy --------*/
213 if (LOG_LV_DEBUG >= this->getLogLevel(loggerCategory)) {
214 std::ostringstream oss;
215 oss << "function : void l7vs::LoggerImpl::loadConf() : "
216 << "Connection category Logger setting. "
218 this->putLogDebug(loggerCategory, 1, oss.str(), __FILE__, __LINE__);
220 /*------ DEBUG LOG END for sslproxy ------*/
222 // Other category Logger setting.
223 log_filename_key = LOGGER_LOG_FILENAME_KEY;
224 rotation_key = LOGGER_ROTATION_KEY;
225 max_backup_index_key = LOGGER_MAX_BACKUP_INDEX_KEY;
226 max_file_size_key = LOGGER_MAX_FILE_SIZE_KEY;
227 rotation_timing_key = LOGGER_ROTATION_TIMING_KEY;
228 rotation_timing_value_key = LOGGER_ROTATION_TIMING_VALUE_KEY;
229 /*-------- DEBUG LOG for sslproxy --------*/
230 if (LOG_LV_DEBUG >= this->getLogLevel(loggerCategory) &&
231 cat != LOG_CAT_SSLPROXY_LOGGER) {
232 std::ostringstream oss;
233 oss << "function : void l7vs::LoggerImpl::loadConf() : "
234 << "Other category Logger setting. "
236 this->putLogDebug(loggerCategory, 2, oss.str(), __FILE__, __LINE__);
238 /*------ DEBUG LOG END for sslproxy ------*/
240 loadCategoryLoggerConf(cat);
242 if (LOG_LV_INFO >= this->getLogLevel(loggerCategory)) {
243 std::ostringstream oss;
244 oss << "Logger Configuration Succeed.";
245 this->putLogInfo(loggerCategory,2, oss.str(), __FILE__, __LINE__);
248 /*-------- DEBUG LOG for sslproxy --------*/
249 if (LOG_LV_DEBUG >= this->getLogLevel(loggerCategory)) {
250 std::ostringstream oss;
251 oss << "out_function : void l7vs::LoggerImpl::loadConf() : "
252 << "loadCategoryLoggerConf() END.";
253 this->putLogDebug(loggerCategory, 3, oss.str(), __FILE__, __LINE__);
255 /*------ DEBUG LOG END for sslproxy ------*/
259 * load the category logger parameter.
260 * get settings from parameter component of each category, and configure log4cxx property
262 * @param[in] catnum LOG_CATEGORY_TAG
265 void l7vs::LoggerImpl::loadCategoryLoggerConf(LOG_CATEGORY_TAG catnum)
270 if (l7vs::Parameter::getInstance().isStringExist(PARAM_COMP_LOGGER, log_filename_key)) {
271 logFilename = l7vs::Parameter::getInstance().getStringValue(PARAM_COMP_LOGGER, log_filename_key);
274 if (LOG_LV_ERROR >= this->getLogLevel(loggerCategory)) {
275 this->putLogError(loggerCategory,1, "Not Exist Log Filename Setting.", __FILE__, __LINE__);
277 throw std::logic_error("Not Exist Log Filename Setting.");
281 if (l7vs::Parameter::getInstance().isStringExist(PARAM_COMP_LOGGER, rotation_key)) {
282 std::string rotationStr = l7vs::Parameter::getInstance().getStringValue(PARAM_COMP_LOGGER, rotation_key);
283 if ("size" == rotationStr) rotation = LOG_ROT_SIZE;
284 else if ("date" == rotationStr) rotation = LOG_ROT_DATE;
285 else if ("datesize" == rotationStr) rotation = LOG_ROT_DATESIZE;
287 if (LOG_LV_ERROR >= this->getLogLevel(loggerCategory)) {
288 this->putLogError(loggerCategory,2, "Invalid Log Rotation Setting.", __FILE__, __LINE__);
290 throw std::logic_error("Invalid Log Rotation Setting.");
294 if (LOG_LV_ERROR >= this->getLogLevel(loggerCategory)) {
295 this->putLogError(loggerCategory,3, "Not Exist Log Rotation Setting.", __FILE__, __LINE__);
297 throw std::logic_error("Not Exist Log Rotation Setting.");
300 //get max backup index
301 if (l7vs::Parameter::getInstance().isStringExist(PARAM_COMP_LOGGER, max_backup_index_key)) {
302 std::string maxBackupIndexStr = l7vs::Parameter::getInstance().getStringValue(PARAM_COMP_LOGGER, max_backup_index_key);
304 maxBackupIndex = lexical_cast<unsigned int>(maxBackupIndexStr);
306 catch (const l7vs::bad_lexical_cast& bc) {
307 if (LOG_LV_ERROR >= this->getLogLevel(loggerCategory)) {
308 this->putLogError(loggerCategory,4, "Invalid MaxBackupIndex Value.", __FILE__, __LINE__);
310 throw std::logic_error("Invalid MaxBackupIndex Value.");
312 if (LOGGER_BACKUP_INDEX_LOWER_LIMIT > maxBackupIndex) {
313 std::ostringstream oss;
314 oss << "Max Backup Index must at least " << LOGGER_BACKUP_INDEX_LOWER_LIMIT << ".";
315 if (LOG_LV_ERROR >= this->getLogLevel(loggerCategory)) {
316 this->putLogError(loggerCategory,5, oss.str(), __FILE__, __LINE__);
318 throw std::logic_error(oss.str());
320 if (LOGGER_BACKUP_INDEX_LIMIT < maxBackupIndex) {
321 std::ostringstream oss;
322 oss << "Max Backup Index must at most " << LOGGER_BACKUP_INDEX_LIMIT << ".";
323 if (LOG_LV_ERROR >= this->getLogLevel(loggerCategory)) {
324 this->putLogError(loggerCategory,6, oss.str(), __FILE__, __LINE__);
326 throw std::logic_error(oss.str());
330 if (LOG_LV_ERROR >= this->getLogLevel(loggerCategory)) {
331 this->putLogError(loggerCategory,7, "Not Exist Log MaxBackupIndex Setting.", __FILE__, __LINE__);
333 throw std::logic_error("Not Exist Log MaxBackupIndex Setting.");
336 if (LOG_ROT_SIZE == rotation || LOG_ROT_DATESIZE == rotation) {
338 std::string maxFileSizeStr;
339 if (l7vs::Parameter::getInstance().isStringExist(PARAM_COMP_LOGGER, max_file_size_key)) {
340 maxFileSizeStr = l7vs::Parameter::getInstance().getStringValue(PARAM_COMP_LOGGER, max_file_size_key);
343 if (LOG_LV_ERROR >= this->getLogLevel(loggerCategory)) {
344 this->putLogError(loggerCategory,8, "Not Exist Log MaxFileSize Setting.", __FILE__, __LINE__);
346 throw std::logic_error("Not Exist Log MaxFileSize Setting.");
349 std::string size_val;
350 std::string last_str = maxFileSizeStr.substr(maxFileSizeStr.length() - 1, 1);
351 // when unit was specified
352 if (("K" == last_str) || ("M" == last_str) || ("G" == last_str)) {
353 size_val = maxFileSizeStr.substr(0, maxFileSizeStr.length() - 1);
356 size_val = maxFileSizeStr;
360 maxFileSize = lexical_cast<size_t>(size_val);
362 catch (const l7vs::bad_lexical_cast& bc) {
363 if (LOG_LV_ERROR >= this->getLogLevel(loggerCategory)) {
364 this->putLogError(loggerCategory,9, "Invalid FileSize Value.", __FILE__, __LINE__);
366 throw std::logic_error("Invalid FileSize Value.");
369 if ("K" == last_str) {
370 if ((ULLONG_MAX / 1024) < maxFileSize) {
371 if (LOG_LV_ERROR >= this->getLogLevel(loggerCategory)) {
372 this->putLogError(loggerCategory,10, "Invalid FileSize Value.", __FILE__, __LINE__);
374 throw std::logic_error("Invalid FileSize Value.");
376 maxFileSize = maxFileSize * 1024;
378 else if ("M" == last_str) {
379 if ((ULLONG_MAX / 1024 / 1024) < maxFileSize) {
380 if (LOG_LV_ERROR >= this->getLogLevel(loggerCategory)) {
381 this->putLogError(loggerCategory,11, "Invalid FileSize Value.", __FILE__, __LINE__);
383 throw std::logic_error("Invalid FileSize Value.");
385 maxFileSize = maxFileSize * 1024 * 1024;
387 else if ("G" == last_str) {
388 if ((ULLONG_MAX / 1024 / 1024 / 1024) < maxFileSize) {
389 if (LOG_LV_ERROR >= this->getLogLevel(loggerCategory)) {
390 this->putLogError(loggerCategory,12, "Invalid FileSize Value.", __FILE__, __LINE__);
392 throw std::logic_error("Invalid FileSize Value.");
394 maxFileSize = maxFileSize * 1024 * 1024 * 1024;
396 if (LOGGER_FILESIZE_LOWER_LIMIT > maxFileSize) {
397 int limit = LOGGER_FILESIZE_LOWER_LIMIT;
398 std::ostringstream oss;
399 oss << "FileSize must at least " << limit << " bytes.";
400 if (LOG_LV_ERROR >= this->getLogLevel(loggerCategory)) {
401 this->putLogError(loggerCategory,13, oss.str(), __FILE__, __LINE__);
403 throw std::logic_error(oss.str());
407 if (LOG_ROT_DATE == rotation || LOG_ROT_DATESIZE == rotation) {
408 // get rotation timing
409 if (l7vs::Parameter::getInstance().isStringExist(PARAM_COMP_LOGGER, rotation_timing_key)) {
410 std::string rotationTimingStr = l7vs::Parameter::getInstance().getStringValue(PARAM_COMP_LOGGER, rotation_timing_key);
411 if ("year" == rotationTimingStr) rotationTiming = LOG_TIM_YEAR;
412 else if ("month" == rotationTimingStr) rotationTiming = LOG_TIM_MONTH;
413 else if ("week" == rotationTimingStr) rotationTiming = LOG_TIM_WEEK;
414 else if ("date" == rotationTimingStr) rotationTiming = LOG_TIM_DATE;
415 else if ("hour" == rotationTimingStr) rotationTiming = LOG_TIM_HOUR;
417 if (LOG_LV_ERROR >= this->getLogLevel(loggerCategory)) {
418 this->putLogError(loggerCategory,14, "Invalid Log RotationTiming Setting.", __FILE__, __LINE__);
420 throw std::logic_error("Invalid Log RotationTiming Setting.");
424 if (LOG_LV_ERROR >= this->getLogLevel(loggerCategory)) {
425 this->putLogError(loggerCategory,15, "Not Exist Log RotaionTiming Setting.", __FILE__, __LINE__);
427 throw std::logic_error("Not Exist Log RotaionTiming Setting.");
430 if (LOG_TIM_YEAR == rotationTiming) {
431 if (l7vs::Parameter::getInstance().isStringExist(PARAM_COMP_LOGGER, rotation_timing_value_key)) {
432 ret = l7vs::Parameter::getInstance().getStringValue(PARAM_COMP_LOGGER, rotation_timing_value_key);
434 std::string::size_type fpos = 0;
435 std::string::size_type rpos = 0;
441 rpos = ret.find_first_of('/', fpos);
442 if (std::string::npos != rpos) {
443 std::string monthStr = ret.substr(fpos, rpos - fpos);
445 month = lexical_cast<int>(monthStr);
447 catch (const l7vs::bad_lexical_cast& bc) {
448 if (LOG_LV_ERROR >= this->getLogLevel(loggerCategory)) {
449 this->putLogError(loggerCategory,16, "Parse Timing Year Error.", __FILE__, __LINE__);
451 throw std::logic_error("Parse Timing Year Error.");
453 if (1 > month || month > 12) {
454 if (LOG_LV_ERROR >= this->getLogLevel(loggerCategory)) {
455 this->putLogError(loggerCategory,17, "Parse Timing Year Error.", __FILE__, __LINE__);
457 throw std::logic_error("Parse Timing Year Error.");
461 rpos = ret.find_first_of(' ', fpos);
462 if (std::string::npos != rpos) {
463 std::string dateStr = ret.substr(fpos, rpos - fpos);
465 date = lexical_cast<int>(dateStr);
467 catch (const l7vs::bad_lexical_cast& bc) {
468 if (LOG_LV_ERROR >= this->getLogLevel(loggerCategory)) {
469 this->putLogError(loggerCategory,18, "Parse Timing Year Error.", __FILE__, __LINE__);
471 throw std::logic_error("Parse Timing Year Error.");
473 if (1 > date || date > 31) {
474 if (LOG_LV_ERROR >= this->getLogLevel(loggerCategory)) {
475 this->putLogError(loggerCategory,19, "Parse Timing Year Error.", __FILE__, __LINE__);
477 throw std::logic_error("Parse Timing Year Error.");
479 int dates[] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
480 if (date > dates[month - 1]) {
481 if (LOG_LV_ERROR >= this->getLogLevel(loggerCategory)) {
482 this->putLogError(loggerCategory,20, "Parse Timing Year Error.", __FILE__, __LINE__);
484 throw std::logic_error("Parse Timing Year Error.");
488 rpos = ret.find_first_of(':', fpos);
489 if (std::string::npos != rpos) {
490 std::string hourStr = ret.substr(fpos, rpos - fpos);
492 hour = lexical_cast<int>(hourStr);
494 catch (const l7vs::bad_lexical_cast& bc) {
495 if (LOG_LV_ERROR >= this->getLogLevel(loggerCategory)) {
496 this->putLogError(loggerCategory,21, "Parse Timing Year Error.", __FILE__, __LINE__);
498 throw std::logic_error("Parse Timing Year Error.");
500 if (0 > hour || hour > 23) {
501 if (LOG_LV_ERROR >= this->getLogLevel(loggerCategory)) {
502 this->putLogError(loggerCategory,22, "Parse Timing Year Error.", __FILE__, __LINE__);
504 throw std::logic_error("Parse Timing Year Error.");
507 std::string minuteStr = ret.substr(rpos + 1);
509 minute = lexical_cast<int>(minuteStr);
511 catch (const l7vs::bad_lexical_cast& bc) {
512 if (LOG_LV_ERROR >= this->getLogLevel(loggerCategory)) {
513 this->putLogError(loggerCategory,23, "Parse Timing Year Error.", __FILE__, __LINE__);
515 throw std::logic_error("Parse Timing Year Error.");
517 if (0 > minute || minute > 59) {
518 if (LOG_LV_ERROR >= this->getLogLevel(loggerCategory)) {
519 this->putLogError(loggerCategory,24, "Parse Timing Year Error.", __FILE__, __LINE__);
521 throw std::logic_error("Parse Timing Year Error.");
525 if (LOG_LV_ERROR >= this->getLogLevel(loggerCategory)) {
526 this->putLogError(loggerCategory,25, "Parse Timing Year Error.", __FILE__, __LINE__);
528 throw std::logic_error("Parse Timing Year Error.");
532 if (LOG_LV_ERROR >= this->getLogLevel(loggerCategory)) {
533 this->putLogError(loggerCategory,26, "Parse Timing Year Error.", __FILE__, __LINE__);
535 throw std::logic_error("Parse Timing Year Error.");
539 if (LOG_LV_ERROR >= this->getLogLevel(loggerCategory)) {
540 this->putLogError(loggerCategory,27, "Parse Timing Year Error.", __FILE__, __LINE__);
542 throw std::logic_error("Parse Timing Year Error.");
545 // format to internal rotation timing value expresson
546 std::ostringstream oss;
547 oss << std::setfill('0') << std::setw(2) << month
548 << std::setfill('0') << std::setw(2) << date
549 << std::setfill('0') << std::setw(2) << hour
550 << std::setfill('0') << std::setw(2) << minute;
552 rotationTimingValue = oss.str();
556 if (LOG_LV_ERROR >= this->getLogLevel(loggerCategory)) {
557 this->putLogError(loggerCategory,28, "Not Exist Log RotaionTiming Year Setting.", __FILE__, __LINE__);
559 throw std::logic_error("Not Exist Log RotaionTiming Year Setting.");
563 if (LOG_TIM_MONTH == rotationTiming) {
564 if (l7vs::Parameter::getInstance().isStringExist(PARAM_COMP_LOGGER, rotation_timing_value_key)) {
565 ret = l7vs::Parameter::getInstance().getStringValue(PARAM_COMP_LOGGER, rotation_timing_value_key);
567 std::string::size_type fpos = 0;
568 std::string::size_type rpos = 0;
573 rpos = ret.find_first_of(' ', fpos);
574 if (std::string::npos != rpos) {
575 std::string dateStr = ret.substr(fpos, rpos - fpos);
577 date = lexical_cast<int>(dateStr);
579 catch (const l7vs::bad_lexical_cast& bc) {
580 if (LOG_LV_ERROR >= this->getLogLevel(loggerCategory)) {
581 this->putLogError(loggerCategory,29, "Parse Timing Month Error.", __FILE__, __LINE__);
583 throw std::logic_error("Parse Timing Month Error.");
585 if (1 > date || date > 31) {
586 if (LOG_LV_ERROR >= this->getLogLevel(loggerCategory)) {
587 this->putLogError(loggerCategory,30, "Parse Timing Month Error.", __FILE__, __LINE__);
589 throw std::logic_error("Parse Timing Month Error.");
593 rpos = ret.find_first_of(':', fpos);
594 if (std::string::npos != rpos) {
595 std::string hourStr = ret.substr(fpos, rpos - fpos);
597 hour = lexical_cast<int>(hourStr);
599 catch (const l7vs::bad_lexical_cast& bc) {
600 if (LOG_LV_ERROR >= this->getLogLevel(loggerCategory)) {
601 this->putLogError(loggerCategory,31, "Parse Timing Month Error.", __FILE__, __LINE__);
603 throw std::logic_error("Parse Timing Month Error.");
605 if (0 > hour || hour > 23) {
606 if (LOG_LV_ERROR >= this->getLogLevel(loggerCategory)) {
607 this->putLogError(loggerCategory,32, "Parse Timing Month Error.", __FILE__, __LINE__);
609 throw std::logic_error("Parse Timing Month Error.");
612 std::string minuteStr = ret.substr(rpos + 1);
614 minute = lexical_cast<int>(minuteStr);
616 catch (const l7vs::bad_lexical_cast& bc) {
617 if (LOG_LV_ERROR >= this->getLogLevel(loggerCategory)) {
618 this->putLogError(loggerCategory,33, "Parse Timing Month Error.", __FILE__, __LINE__);
620 throw std::logic_error("Parse Timing Month Error.");
622 if (0 > minute || minute > 59) {
623 if (LOG_LV_ERROR >= this->getLogLevel(loggerCategory)) {
624 this->putLogError(loggerCategory,34, "Parse Timing Month Error.", __FILE__, __LINE__);
626 throw std::logic_error("Parse Timing Month Error.");
630 if (LOG_LV_ERROR >= this->getLogLevel(loggerCategory)) {
631 this->putLogError(loggerCategory,35, "Parse Timing Month Error.", __FILE__, __LINE__);
633 throw std::logic_error("Parse Timing Month Error.");
637 if (LOG_LV_ERROR >= this->getLogLevel(loggerCategory)) {
638 this->putLogError(loggerCategory,36, "Parse Timing Month Error.", __FILE__, __LINE__);
640 throw std::logic_error("Parse Timing Month Error.");
643 // format to internal rotation timing value expresson
644 std::ostringstream oss;
645 oss << std::setfill('0') << std::setw(2) << date
646 << std::setfill('0') << std::setw(2) << hour
647 << std::setfill('0') << std::setw(2) << minute;
649 rotationTimingValue = oss.str();
653 if (LOG_LV_ERROR >= this->getLogLevel(loggerCategory)) {
654 this->putLogError(loggerCategory,37, "Not Exist Log RotaionTiming Month Setting.", __FILE__, __LINE__);
656 throw std::logic_error("Not Exist Log RotaionTiming Month Setting.");
660 if (LOG_TIM_WEEK == rotationTiming) {
661 if (l7vs::Parameter::getInstance().isStringExist(PARAM_COMP_LOGGER, rotation_timing_value_key)) {
662 ret = l7vs::Parameter::getInstance().getStringValue(PARAM_COMP_LOGGER, rotation_timing_value_key);
664 std::string::size_type fpos = 0;
665 std::string::size_type rpos = 0;
669 rpos = ret.find_first_of(' ', fpos);
671 if (std::string::npos != rpos) {
672 std::string weekStr = ret.substr(fpos, rpos - fpos);
674 if ("sun" == weekStr) week = 0;
675 else if ("mon" == weekStr) week = 1;
676 else if ("tue" == weekStr) week = 2;
677 else if ("wed" == weekStr) week = 3;
678 else if ("thu" == weekStr) week = 4;
679 else if ("fri" == weekStr) week = 5;
680 else if ("sat" == weekStr) week = 6;
682 if (LOG_LV_ERROR >= this->getLogLevel(loggerCategory)) {
683 this->putLogError(loggerCategory,38, "Parse Timing Week Error.", __FILE__, __LINE__);
685 throw std::logic_error("Parse Timing Week Error.");
689 rpos = ret.find_first_of(':', fpos);
690 if (std::string::npos != rpos) {
691 std::string hourStr = ret.substr(fpos, rpos - fpos);
693 hour = lexical_cast<int>(hourStr);
695 catch (const l7vs::bad_lexical_cast& bc) {
696 if (LOG_LV_ERROR >= this->getLogLevel(loggerCategory)) {
697 this->putLogError(loggerCategory,39, "Parse Timing Week Error.", __FILE__, __LINE__);
699 throw std::logic_error("Parse Timing Week Error.");
701 if (0 > hour || hour > 23) {
702 if (LOG_LV_ERROR >= this->getLogLevel(loggerCategory)) {
703 this->putLogError(loggerCategory,40, "Parse Timing Week Error.", __FILE__, __LINE__);
705 throw std::logic_error("Parse Timing Week Error.");
708 std::string minuteStr = ret.substr(rpos + 1);
710 minute = lexical_cast<int>(minuteStr);
712 catch (const l7vs::bad_lexical_cast& bc) {
713 if (LOG_LV_ERROR >= this->getLogLevel(loggerCategory)) {
714 this->putLogError(loggerCategory,41, "Parse Timing Week Error.", __FILE__, __LINE__);
716 throw std::logic_error("Parse Timing Week Error.");
718 if (0 > minute || minute > 59) {
719 if (LOG_LV_ERROR >= this->getLogLevel(loggerCategory)) {
720 this->putLogError(loggerCategory,42, "Parse Timing Week Error.", __FILE__, __LINE__);
722 throw std::logic_error("Parse Timing Week Error.");
726 if (LOG_LV_ERROR >= this->getLogLevel(loggerCategory)) {
727 this->putLogError(loggerCategory,43, "Parse Timing Week Error.", __FILE__, __LINE__);
729 throw std::logic_error("Parse Timing Week Error.");
733 if (LOG_LV_ERROR >= this->getLogLevel(loggerCategory)) {
734 this->putLogError(loggerCategory,44, "Parse Timing Week Error.", __FILE__, __LINE__);
736 throw std::logic_error("Parse Timing Week Error.");
739 // format to internal rotation timing value expresson
740 std::ostringstream oss;
741 oss << std::setfill('0') << std::setw(1) << week
742 << std::setfill('0') << std::setw(2) << hour
743 << std::setfill('0') << std::setw(2) << minute;
745 rotationTimingValue = oss.str();
749 if (LOG_LV_ERROR >= this->getLogLevel(loggerCategory)) {
750 this->putLogError(loggerCategory,45, "Not Exist Log RotaionTiming Week Setting.", __FILE__, __LINE__);
752 throw std::logic_error("Not Exist Log RotaionTiming Week Setting.");
756 if (LOG_TIM_DATE == rotationTiming) {
757 if (l7vs::Parameter::getInstance().isStringExist(PARAM_COMP_LOGGER, rotation_timing_value_key)) {
758 ret = l7vs::Parameter::getInstance().getStringValue(PARAM_COMP_LOGGER, rotation_timing_value_key);
760 std::string::size_type fpos = 0;
761 std::string::size_type rpos = 0;
765 rpos = ret.find_first_of(':', fpos);
766 if (std::string::npos != rpos) {
767 std::string hourStr = ret.substr(fpos, rpos - fpos);
769 hour = lexical_cast<int>(hourStr);
771 catch (const l7vs::bad_lexical_cast& bc) {
772 if (LOG_LV_ERROR >= this->getLogLevel(loggerCategory)) {
773 this->putLogError(loggerCategory,46, "Parse Timing Date Error.", __FILE__, __LINE__);
775 throw std::logic_error("Parse Timing Date Error.");
777 if (0 > hour || hour > 23) {
778 if (LOG_LV_ERROR >= this->getLogLevel(loggerCategory)) {
779 this->putLogError(loggerCategory,47, "Parse Timing Date Error.", __FILE__, __LINE__);
781 throw std::logic_error("Parse Timing Date Error.");
784 std::string minuteStr = ret.substr(rpos + 1);
786 minute = lexical_cast<int>(minuteStr);
788 catch (const l7vs::bad_lexical_cast& bc) {
789 if (LOG_LV_ERROR >= this->getLogLevel(loggerCategory)) {
790 this->putLogError(loggerCategory,48, "Parse Timing Date Error.", __FILE__, __LINE__);
792 throw std::logic_error("Parse Timing Date Error.");
794 if (0 > minute || minute > 59) {
795 if (LOG_LV_ERROR >= this->getLogLevel(loggerCategory)) {
796 this->putLogError(loggerCategory,49, "Parse Timing Date Error.", __FILE__, __LINE__);
798 throw std::logic_error("Parse Timing Date Error.");
802 if (LOG_LV_ERROR >= this->getLogLevel(loggerCategory)) {
803 this->putLogError(loggerCategory,50, "Parse Timing Date Error.", __FILE__, __LINE__);
805 throw std::logic_error("Parse Timing Date Error.");
808 // format to internal rotation timing value expresson
809 std::ostringstream oss;
810 oss << std::setfill('0') << std::setw(2) << hour
811 << std::setfill('0') << std::setw(2) << minute;
813 rotationTimingValue = oss.str();
817 if (LOG_LV_ERROR >= this->getLogLevel(loggerCategory)) {
818 this->putLogError(loggerCategory,51, "Not Exist Log RotaionTiming Date Setting.", __FILE__, __LINE__);
820 throw std::logic_error("Not Exist Log RotaionTiming Date Setting.");
824 if (LOG_TIM_HOUR == rotationTiming) {
825 if (l7vs::Parameter::getInstance().isStringExist(PARAM_COMP_LOGGER, rotation_timing_value_key)) {
826 ret = l7vs::Parameter::getInstance().getStringValue(PARAM_COMP_LOGGER, rotation_timing_value_key);
831 minute = lexical_cast<int>(ret);
833 catch (const l7vs::bad_lexical_cast& bc) {
834 if (LOG_LV_ERROR >= this->getLogLevel(loggerCategory)) {
835 this->putLogError(loggerCategory,52, "Parse Timing Hour Error.", __FILE__, __LINE__);
837 throw std::logic_error("Parse Timing Hour Error.");
839 if (0 > minute || minute > 59) {
840 if (LOG_LV_ERROR >= this->getLogLevel(loggerCategory)) {
841 this->putLogError(loggerCategory,53, "Parse Timing Hour Error.", __FILE__, __LINE__);
843 throw std::logic_error("Parse Timing Hour Error.");
846 // format to internal rotation timing value expresson
847 std::ostringstream oss;
848 oss << std::setfill('0') << std::setw(2) << minute;
850 rotationTimingValue = oss.str();
854 if (LOG_LV_ERROR >= this->getLogLevel(loggerCategory)) {
855 this->putLogError(loggerCategory,54, "Not Exist Log RotaionTiming Hour Setting.", __FILE__, __LINE__);
857 throw std::logic_error("Not Exist Log RotaionTiming Hour Setting.");
863 /*-------- DEBUG LOG for sslproxy --------*/
864 if (LOG_LV_DEBUG >= this->getLogLevel(loggerCategory) &&
865 catnum != LOG_CAT_SSLPROXY_LOGGER) {
866 std::ostringstream oss;
867 oss << "function : void l7vs::LoggerImpl::loadCategoryLoggerConf("
868 << "LOG_CATEGORY_TAG catnum) : "
869 << "Set category Logger appender. "
870 << "catnum = " << catnum;
871 this->putLogDebug(loggerCategory, 4, oss.str(), __FILE__, __LINE__);
873 /*------ DEBUG LOG END for sslproxy ------*/
875 // reset current configuration
876 // when category is LOG_CAT_NONE (first category)
877 log4cxx::helpers::Pool pool;
878 if (catnum == LOG_CAT_NONE) {
879 /*-------- DEBUG LOG for sslproxy --------*/
880 if (LOG_LV_DEBUG >= this->getLogLevel(loggerCategory) &&
881 catnum != LOG_CAT_SSLPROXY_LOGGER) {
882 std::ostringstream oss;
883 oss << "function : l7vs::LoggerImpl::loadCategoryLoggerConf() : "
884 << "Reset all Logger configuration. "
885 << "catnum = " << catnum;
886 this->putLogDebug(loggerCategory, 5, oss.str(), __FILE__, __LINE__);
888 /*------ DEBUG LOG END for sslproxy ------*/
889 log4cxx::LogManager::resetConfiguration();
891 log4cxx::LoggerPtr catlogger = log4cxx::Logger::getLogger(categoryTable[catnum]);
892 if (0 == catlogger) {
893 throw std::logic_error("getLogger Failed.");
895 log4cxx::LayoutPtr layout =
896 new log4cxx::PatternLayout(LOGGER_LAYOUT);
901 // create FixedWindowRollingPolicy
902 log4cxx::rolling::FixedWindowRollingPolicyPtr fixedRollingPolicy =
903 new log4cxx::rolling::FixedWindowRollingPolicy();
906 fixedRollingPolicy->setMinIndex(1);
909 fixedRollingPolicy->setMaxIndex(maxBackupIndex);
911 // setting FileNamePattern
912 std::ostringstream sizeFile;
913 sizeFile << logFilename << "." << LOGGER_FILE_PATTERN;
914 fixedRollingPolicy->setFileNamePattern(sizeFile.str());
916 // create SizeBasedTriggeringPolicy
917 log4cxx::rolling::SizeBasedTriggeringPolicyPtr sizeTriggeringPolicy =
918 new log4cxx::rolling::SizeBasedTriggeringPolicy();
920 // setting maxFileSize
921 sizeTriggeringPolicy->setMaxFileSize(maxFileSize);
923 // create RollingFileAppender
924 log4cxx::rolling::RollingFileAppenderPtr sizeAppender =
925 new log4cxx::rolling::RollingFileAppender();
928 sizeAppender->setLayout(layout);
931 sizeAppender->setRollingPolicy(fixedRollingPolicy);
933 // set TriggeringPolicy
934 sizeAppender->setTriggeringPolicy(sizeTriggeringPolicy);
937 sizeAppender->setFile(logFilename, true, false, LOGGER_DEFAULT_BUFFER_SIZE, pool);
939 // activate appender options
940 sizeAppender->activateOptions(pool);
942 // add size_base_appender to CategoryLogger
943 catlogger->addAppender(sizeAppender);
949 // create StrictTimeBasedRollingPolicy
950 log4cxx::rolling::StrictTimeBasedRollingPolicyPtr strictRollingPolicy =
951 new log4cxx::rolling::StrictTimeBasedRollingPolicy();
954 strictRollingPolicy->setMinIndex(1);
957 strictRollingPolicy->setMaxIndex(maxBackupIndex);
959 // setting FileNamePattern
960 std::ostringstream dateFile;
961 dateFile << logFilename << "." << LOGGER_FILE_PATTERN;
962 strictRollingPolicy->setFileNamePattern(dateFile.str());
964 // setting Rotation Timing
965 strictRollingPolicy->setRotationTiming(rotationTiming);
967 // setting Rotation Timing Value
968 strictRollingPolicy->setRotationTimingValue(rotationTimingValue);
970 //create RollingFileAppender
971 log4cxx::rolling::RollingFileAppenderPtr dateAppender =
972 new log4cxx::rolling::RollingFileAppender();
975 dateAppender->setLayout(layout);
977 // set RollingPolicy (TriggeringPolicy also included RollingPolicy)
978 dateAppender->setRollingPolicy(strictRollingPolicy);
981 dateAppender->setFile(logFilename, true, false, LOGGER_DEFAULT_BUFFER_SIZE, pool);
983 // activate appender options
984 dateAppender->activateOptions(pool);
986 // add date_based_appender to CategoryLogger
987 catlogger->addAppender(dateAppender);
991 default: //LOG_ROT_DATESIZE:
993 // create TimeAndSizeBasedRollingPolicy
994 log4cxx::rolling::TimeAndSizeBasedRollingPolicyPtr timeSizeRollingPolicy =
995 new log4cxx::rolling::TimeAndSizeBasedRollingPolicy();
998 timeSizeRollingPolicy->setMinIndex(1);
1001 timeSizeRollingPolicy->setMaxIndex(maxBackupIndex);
1003 // setting FileNamePattern
1004 std::ostringstream dateSizeFile;
1005 dateSizeFile << logFilename << "." << LOGGER_FILE_PATTERN;
1006 timeSizeRollingPolicy->setFileNamePattern(dateSizeFile.str());
1008 // setting Rotation Timing
1009 timeSizeRollingPolicy->setRotationTiming(rotationTiming);
1011 // setting Rotation Timing Value
1012 timeSizeRollingPolicy->setRotationTimingValue(rotationTimingValue);
1014 // setting MaxFileSize
1015 timeSizeRollingPolicy->setMaxFileSize(maxFileSize);
1017 // create Rolling FileAppender
1018 log4cxx::rolling::RollingFileAppenderPtr dateSizeAppender =
1019 new log4cxx::rolling::RollingFileAppender();
1022 dateSizeAppender->setLayout(layout);
1024 // set RollingPolicy (TriggeringPolicy also included RollingPolicy)
1025 dateSizeAppender->setRollingPolicy(timeSizeRollingPolicy);
1028 dateSizeAppender->setFile(logFilename, true, false, LOGGER_DEFAULT_BUFFER_SIZE, pool);
1030 // activate appender options
1031 dateSizeAppender->activateOptions(pool);
1033 // add time_and_size_based_appender to CategoryLogger
1034 catlogger->addAppender(dateSizeAppender);
1038 //set default log level
1039 for (LOG_CATEGORY_TAG cat = LOG_CAT_NONE; cat < LOG_CAT_END; ++cat) {
1040 log4cxx::Logger::getLogger(categoryTable[cat])->setLevel(categoryLevel[cat]);
1043 //get category level
1044 for (LOG_CATEGORY_TAG cat = LOG_CAT_NONE; cat < LOG_CAT_END; ++cat) {
1045 if (cat == LOG_CAT_NONE) continue;
1046 if (l7vs::Parameter::getInstance().isStringExist(PARAM_COMP_LOGGER, categoryTable[cat])) {
1047 std::string levelStr = l7vs::Parameter::getInstance().getStringValue(PARAM_COMP_LOGGER, categoryTable[cat]);
1048 if ("debug" == levelStr) {
1049 categoryLevel[cat] = log4cxx::Level::getDebug();
1050 log4cxx::Logger::getLogger(categoryTable[cat])->setLevel(categoryLevel[cat]);
1052 else if ("info" == levelStr) {
1053 categoryLevel[cat] = log4cxx::Level::getInfo();
1054 log4cxx::Logger::getLogger(categoryTable[cat])->setLevel(categoryLevel[cat]);
1056 else if ("warn" == levelStr) {
1057 categoryLevel[cat] = log4cxx::Level::getWarn();
1058 log4cxx::Logger::getLogger(categoryTable[cat])->setLevel(categoryLevel[cat]);
1060 else if ("error" == levelStr) {
1061 categoryLevel[cat] = log4cxx::Level::getError();
1062 log4cxx::Logger::getLogger(categoryTable[cat])->setLevel(categoryLevel[cat]);
1064 else if ("fatal" == levelStr) {
1065 categoryLevel[cat] = log4cxx::Level::getFatal();
1066 log4cxx::Logger::getLogger(categoryTable[cat])->setLevel(categoryLevel[cat]);
1069 std::ostringstream oss;
1070 oss << "Invalid Log Category Setting : " << categoryTable[cat];
1071 if (LOG_LV_WARN >= this->getLogLevel(loggerCategory)) {
1072 this->putLogWarn(loggerCategory,2, oss.str(), __FILE__, __LINE__);
1074 categoryLevel[cat] = log4cxx::Level::getInfo();
1075 log4cxx::Logger::getLogger(categoryTable[cat])->setLevel(categoryLevel[cat]);
1079 std::ostringstream oss;
1080 oss << "Not Exist Log Category Setting : " << categoryTable[cat];
1081 if (LOG_LV_WARN >= this->getLogLevel(loggerCategory)) {
1082 this->putLogWarn(loggerCategory,3, oss.str(), __FILE__, __LINE__);
1084 categoryLevel[cat] = log4cxx::Level::getInfo();
1085 log4cxx::Logger::getLogger(categoryTable[cat])->setLevel(categoryLevel[cat]);
1089 catch (const std::exception& e) {
1090 std::ostringstream oss;
1091 oss << "Logger Reload Config Failed : " << e.what();
1092 errorConf(7, oss.str(), __FILE__, __LINE__);
1093 throw std::logic_error(oss.str());
1096 initLogLevelTable();
1100 * initialize LogLevel table.
1101 * initialize the LogLevel table.
1106 void l7vs::LoggerImpl::initLogLevelTable(void)
1110 for (;nCounter < LOGGER_CATEGORY_NUM; nCounter++) {
1111 loglevel[nCounter] = LOG_LV_NONE;