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 **********************************************************************/
25 #ifndef __LOGGER_IMPL_H__
26 #define __LOGGER_IMPL_H__
30 #include <log4cxx/logger.h>
31 #include <log4cxx/level.h>
32 #include <log4cxx/net/syslogappender.h>
33 #include <log4cxx/fileappender.h>
34 #include <log4cxx/rollingfileappender.h>
35 #include <log4cxx/patternlayout.h>
36 #include "logger_enum.h"
38 #define BUF_LEN (4096)
39 #define LOGGER_LEVEL_NUM (6)
40 #define LOGGER_CATEGORY_NUM (40)
42 #if !defined(LOGGER_PROCESS_VSD) && !defined(LOGGER_PROCESS_ADM) && !defined(LOGGER_PROCESS_SNM)
43 #define LOGGER_PROCESS_VSD
46 #if defined(LOGGER_PROCESS_VSD)
47 #define LOGGER_PROCESS_ID "VSD"
48 #elif defined(LOGGER_PROCESS_ADM)
49 #define LOGGER_PROCESS_ID "ADM"
51 #define LOGGER_PROCESS_ID "SNM"
54 #if defined(LOGGER_PROCESS_VSD)
55 #include "l7vs_snmpbridge.h"
58 #define LOGGER_NULL "/dev/null"
60 #define HOST_NAME_LEN (256)
64 typedef helpers::ObjectPtrT<RollingFileAppender> RollingFileAppenderPtr;
70 * Logger implement class.
71 * operate log4cxx library.
72 * this is singleton class.
77 //! returns current instance.
78 static LoggerImpl& getInstance();
80 //! default constructor initialize member variables.
81 LoggerImpl() : initialized(false), logFilename(""), rotation(LOG_ROT_SIZE), maxBackupIndex(0), maxFileSize(0), rotationTiming(LOG_TIM_YEAR), rotationTimingValue("")
83 levelTable[LOG_LV_NONE] = log4cxx::Level::getDebug();
84 levelTable[LOG_LV_DEBUG] = log4cxx::Level::getDebug();
85 levelTable[LOG_LV_INFO] = log4cxx::Level::getInfo();
86 levelTable[LOG_LV_WARN] = log4cxx::Level::getWarn();
87 levelTable[LOG_LV_ERROR] = log4cxx::Level::getError();
88 levelTable[LOG_LV_FATAL] = log4cxx::Level::getFatal();
89 #if defined(LOGGER_PROCESS_VSD)
90 loggerCategory = LOG_CAT_L7VSD_LOGGER;
91 loggerProcess = LOG_MOD_L7VSD;
92 //set default log level
93 for (LOG_CATEGORY_TAG cat = LOG_CAT_NONE; cat <= getCategoryRangeEnd(LOG_MOD_SNMPAGENT); ++cat) {
94 categoryLevel[cat] = log4cxx::Level::getError();
96 #elif defined(LOGGER_PROCESS_ADM)
97 loggerCategory = LOG_CAT_L7VSADM_LOGGER;
98 loggerProcess = LOG_MOD_L7VSADM;
99 //set default log level
100 for (LOG_CATEGORY_TAG cat = LOG_CAT_NONE; cat <= getCategoryRangeEnd(LOG_MOD_SNMPAGENT); ++cat) {
101 categoryLevel[cat] = log4cxx::Level::getError();
103 #else //LOGGER_PROCESS_SNM
104 loggerCategory = LOG_CAT_SNMPAGENT_LOGGER;
105 loggerProcess = LOG_MOD_SNMPAGENT;
106 //set default log level
107 for (LOG_CATEGORY_TAG cat = LOG_CAT_NONE; cat <= getCategoryRangeEnd(LOG_MOD_SNMPAGENT); ++cat) {
108 categoryLevel[cat] = log4cxx::Level::getError();
112 //! cpoy constructor disable
113 LoggerImpl( const LoggerImpl& );
114 //! operator= disable
115 LoggerImpl& operator=( const LoggerImpl& );
117 virtual ~LoggerImpl() {}
118 //! static Logger instance
119 static LoggerImpl* instance;
123 LOG_CATEGORY_TAG loggerCategory;
125 LOG_MODULE_TAG loggerProcess;
127 char hostname[HOST_NAME_LEN];
129 //! if error occured, switch appenders to syslogappender and fileappender(/dev/console)
130 virtual void errorConf(unsigned int messageId, const std::string& errorMessage, const char* file, int line);
133 std::string logFilename;
134 //! rotation way (size base, date base, or both size and date base)
135 LOG_ROTATION_TAG rotation;
136 //! number of backup log file
137 unsigned int maxBackupIndex;
138 //! max size of log file
139 unsigned long long maxFileSize;
148 LOG_ROTATION_TIMING_TAG rotationTiming;
150 * rotation timing value
152 * rotation timing value
153 * -------------------------------------------------
154 * year "03051500" (3/5 15:00)
155 * month "051100" (5 11:00)
156 * week "12000" (mon 20:00) sun = 0, sat = 6
157 * date "1500" (15:00)
160 std::string rotationTimingValue;
162 //! for transration between log4cxx::LevelPtr and LOGER_LEVEL_TAG
163 static log4cxx::LevelPtr levelTable[LOGGER_LEVEL_NUM];
164 //! for transration between string and LOGGER_CATEGORY_TAG
165 static char categoryTable[][LOGGER_CATEGORY_NUM];
166 //! holds category-loglevel
167 log4cxx::LevelPtr categoryLevel[LOGGER_CATEGORY_NUM];
169 //! LOG_LEVEL_TAG to log4cxx::LevelPtr transrator
170 virtual inline const log4cxx::LevelPtr toLevel(LOG_LEVEL_TAG level)
172 return levelTable[level];
174 //! log4cxx::LevelPtr to LOG_LEVEL_TAG transrator
175 virtual inline LOG_LEVEL_TAG toLevelTag(const log4cxx::LevelPtr level)
177 int levelInt = level->toInt();
179 case log4cxx::Level::DEBUG_INT:
181 case log4cxx::Level::INFO_INT:
183 case log4cxx::Level::WARN_INT:
185 case log4cxx::Level::ERROR_INT:
187 case log4cxx::Level::FATAL_INT:
195 LOG_LEVEL_TAG loglevel[LOGGER_CATEGORY_NUM];
198 //! initialze function
200 //! Configuration function
201 virtual void loadConf();
202 //1 initialize loglevel table
203 void initLogLevelTable();
206 * retrieve category's log level.
208 * @param category that want to know
211 virtual inline LOG_LEVEL_TAG getLogLevel(LOG_CATEGORY_TAG cat)
213 #if defined(LOGGER_PROCESS_VSD)
214 if (cat >= getCategoryRangeStart(LOG_MOD_SNMPAGENT) && cat <= getCategoryRangeEnd(LOG_MOD_SNMPAGENT)) {
215 return l7vs_snmpbridge_get_loglevel(cat);
218 if (LOG_LV_NONE == loglevel[cat]) {
219 this->loglevel[cat] = toLevelTag(log4cxx::Logger::getLogger(categoryTable[cat])->getLevel());
222 return loglevel[cat];
223 //return toLevelTag(log4cxx::Logger::getLogger(categoryTable[cat])->getLevel());
227 * set category's log level.
229 * @param category to set log level
231 * @retval true succeed
232 * @retval false failed
234 virtual inline bool setLogLevel(LOG_CATEGORY_TAG cat, LOG_LEVEL_TAG level)
237 #if defined(LOGGER_PROCESS_VSD)
238 if (cat >= getCategoryRangeStart(LOG_MOD_SNMPAGENT) && cat <= getCategoryRangeEnd(LOG_MOD_SNMPAGENT)) {
239 l7vs_snmpbridge_change_loglevel(cat, level);
242 log4cxx::Logger::getLogger(categoryTable[cat])->setLevel(toLevel(level));
244 catch (const std::exception& ex) {
248 loglevel[cat] = level;
255 * @param category that logging matter occured
256 * @param log message id
258 * @param current file
259 * @param current line
262 virtual inline void putLogFatal(LOG_CATEGORY_TAG cat, const unsigned int message_id, const std::string& message, const char *file, int line)
265 snprintf(buf, BUF_LEN, "%s%d%03d%04d %s %s", LOGGER_PROCESS_ID, LOG_LV_FATAL, cat, message_id, message.c_str(), hostname);
267 log4cxx::Logger::getLogger(categoryTable[cat])->forcedLog(log4cxx::Level::getFatal(), buf, log4cxx::spi::LocationInfo(file, "", line));
269 catch (const std::exception& ex) {
270 std::ostringstream oss;
271 oss << "Logging Error (Fatal Log) : " << ex.what();
272 errorConf(1, oss.str(), __FILE__, __LINE__);
278 * @param category that logging matter occured
279 * @param log message id
281 * @param current file
282 * @param current line
285 //! output fatal log.
286 virtual inline void putLogError(LOG_CATEGORY_TAG cat, const unsigned int message_id, const std::string& message, const char *file, int line)
289 snprintf(buf, BUF_LEN, "%s%d%03d%04d %s %s", LOGGER_PROCESS_ID, LOG_LV_ERROR, cat, message_id, message.c_str(), hostname);
291 log4cxx::Logger::getLogger(categoryTable[cat])->forcedLog(log4cxx::Level::getError(), buf, log4cxx::spi::LocationInfo(file, "", line));
293 catch (const std::exception& ex) {
294 std::ostringstream oss;
295 oss << "Logging Error (Error Log) : " << ex.what();
296 errorConf(2, oss.str(), __FILE__, __LINE__);
302 * @param category that logging matter occured
303 * @param log message id
305 * @param current file
306 * @param current line
309 //! output fatal log.
310 virtual inline void putLogWarn(LOG_CATEGORY_TAG cat, const unsigned int message_id, const std::string& message, const char *file, int line)
313 snprintf(buf, BUF_LEN, "%s%d%03d%04d %s %s", LOGGER_PROCESS_ID, LOG_LV_WARN, cat, message_id, message.c_str(), hostname);
315 log4cxx::Logger::getLogger(categoryTable[cat])->forcedLog(log4cxx::Level::getWarn(), buf, log4cxx::spi::LocationInfo(file, "", line));
317 catch (const std::exception& ex) {
318 std::ostringstream oss;
319 oss << "Logging Error (Warn Log) : " << ex.what();
320 errorConf(3, oss.str(), __FILE__, __LINE__);
326 * @param category that logging matter occured
327 * @param log message id
329 * @param current file
330 * @param current line
333 //! output fatal log.
334 virtual inline void putLogInfo(LOG_CATEGORY_TAG cat, const unsigned int message_id, const std::string& message, const char *file, int line)
337 snprintf(buf, BUF_LEN, "%s%d%03d%04d %s %s", LOGGER_PROCESS_ID, LOG_LV_INFO, cat, message_id, message.c_str(), hostname);
339 log4cxx::Logger::getLogger(categoryTable[cat])->forcedLog(log4cxx::Level::getInfo(), buf, log4cxx::spi::LocationInfo(file, "", line));
341 catch (const std::exception& ex) {
342 std::ostringstream oss;
343 oss << "Logging Error (Info Log) : " << ex.what();
344 errorConf(4, oss.str(), __FILE__, __LINE__);
350 * @param category that logging matter occured
351 * @param log message id
353 * @param current file
354 * @param current line
357 virtual inline void putLogDebug(LOG_CATEGORY_TAG cat, const unsigned int message_id, const std::string& message, const char *file, int line)
360 snprintf(buf, BUF_LEN, "%s%d%03d%04d %s %s", LOGGER_PROCESS_ID, LOG_LV_DEBUG, cat, message_id, message.c_str(), hostname);
362 log4cxx::Logger::getLogger(categoryTable[cat])->forcedLog(log4cxx::Level::getDebug(), buf, log4cxx::spi::LocationInfo(file, "", line));
364 catch (const std::exception& ex) {
365 std::ostringstream oss;
366 oss << "Logging Error (Debug Log) : " << ex.what();
367 errorConf(5, oss.str(), __FILE__, __LINE__);
372 * return start category by using module.
375 * @retrun start category
377 virtual inline LOG_CATEGORY_TAG getCategoryRangeStart(LOG_MODULE_TAG mod)
380 case LOG_MOD_L7VSADM: return LOG_CAT_L7VSADM_PARSE;
381 case LOG_MOD_SNMPAGENT: return LOG_CAT_SNMPAGENT_START_STOP;
382 default: return LOG_CAT_L7VSD_NETWORK;
386 * return end category by using module.
389 * @retrun end category
391 virtual inline LOG_CATEGORY_TAG getCategoryRangeEnd(LOG_MODULE_TAG mod)
394 case LOG_MOD_L7VSADM: return LOG_CAT_L7VSADM_MODULE;
395 case LOG_MOD_SNMPAGENT: return LOG_CAT_SNMPAGENT_PARAMETER;
396 default: return LOG_CAT_L7VSD_MODULE;
401 #endif //__LOGGER_IMPL_H__