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 #define LOGGER_PROCESS_ID "SLP"
44 #define LOGGER_NULL "/dev/null"
46 #define HOST_NAME_LEN (256)
50 typedef helpers::ObjectPtrT<RollingFileAppender> RollingFileAppenderPtr;
56 * Logger implement class.
57 * operate log4cxx library.
58 * this is singleton class.
63 //! returns current instance.
64 static LoggerImpl& getInstance();
66 //! default constructor initialize member variables.
67 LoggerImpl() : initialized(false), logFilename(""), rotation(LOG_ROT_SIZE), maxBackupIndex(0), maxFileSize(0), rotationTiming(LOG_TIM_YEAR), rotationTimingValue("")
69 levelTable[LOG_LV_NONE] = log4cxx::Level::getDebug();
70 levelTable[LOG_LV_DEBUG] = log4cxx::Level::getDebug();
71 levelTable[LOG_LV_INFO] = log4cxx::Level::getInfo();
72 levelTable[LOG_LV_WARN] = log4cxx::Level::getWarn();
73 levelTable[LOG_LV_ERROR] = log4cxx::Level::getError();
74 levelTable[LOG_LV_FATAL] = log4cxx::Level::getFatal();
76 loggerCategory = LOG_CAT_SSLPROXY_LOGGER;
77 //set default log level
78 for (LOG_CATEGORY_TAG cat = LOG_CAT_NONE; cat < LOG_CAT_END; ++cat) {
79 categoryLevel[cat] = log4cxx::Level::getError();
82 //! cpoy constructor disable
83 LoggerImpl( const LoggerImpl& );
85 LoggerImpl& operator=( const LoggerImpl& );
87 virtual ~LoggerImpl() {}
88 //! static Logger instance
89 static LoggerImpl* instance;
93 LOG_CATEGORY_TAG loggerCategory;
95 char hostname[HOST_NAME_LEN];
97 //! if error occured, switch appenders to syslogappender and fileappender(/dev/console)
98 virtual void errorConf(unsigned int messageId, const std::string& errorMessage, const char* file, int line);
101 std::string logFilename;
102 //! rotation way (size base, date base, or both size and date base)
103 LOG_ROTATION_TAG rotation;
104 //! number of backup log file
105 unsigned int maxBackupIndex;
106 //! max size of log file
107 unsigned long long maxFileSize;
116 LOG_ROTATION_TIMING_TAG rotationTiming;
118 * rotation timing value
120 * rotation timing value
121 * -------------------------------------------------
122 * year "03051500" (3/5 15:00)
123 * month "051100" (5 11:00)
124 * week "12000" (mon 20:00) sun = 0, sat = 6
125 * date "1500" (15:00)
128 std::string rotationTimingValue;
130 //! key strings for logger
131 std::string log_filename_key;
132 std::string rotation_key;
133 std::string max_backup_index_key;
134 std::string max_file_size_key;
135 std::string rotation_timing_key;
136 std::string rotation_timing_value_key;
139 //! for transration between log4cxx::LevelPtr and LOGER_LEVEL_TAG
140 static log4cxx::LevelPtr levelTable[LOGGER_LEVEL_NUM];
141 //! for transration between string and LOGGER_CATEGORY_TAG
142 static char categoryTable[][LOGGER_CATEGORY_NUM];
143 //! holds category-loglevel
144 log4cxx::LevelPtr categoryLevel[LOGGER_CATEGORY_NUM];
146 //! LOG_LEVEL_TAG to log4cxx::LevelPtr transrator
147 virtual inline const log4cxx::LevelPtr toLevel(LOG_LEVEL_TAG level)
149 return levelTable[level];
151 //! log4cxx::LevelPtr to LOG_LEVEL_TAG transrator
152 virtual inline LOG_LEVEL_TAG toLevelTag(const log4cxx::LevelPtr level)
154 int levelInt = level->toInt();
156 case log4cxx::Level::DEBUG_INT:
158 case log4cxx::Level::INFO_INT:
160 case log4cxx::Level::WARN_INT:
162 case log4cxx::Level::ERROR_INT:
164 case log4cxx::Level::FATAL_INT:
172 LOG_LEVEL_TAG loglevel[LOGGER_CATEGORY_NUM];
175 //! initialze function
177 //! Configuration function
178 virtual void loadConf();
179 //! Category logger configuration function
180 virtual void loadCategoryLoggerConf(LOG_CATEGORY_TAG cat);
181 //1 initialize loglevel table
182 void initLogLevelTable();
185 * retrieve category's log level.
187 * @param category that want to know
190 virtual inline LOG_LEVEL_TAG getLogLevel(LOG_CATEGORY_TAG cat)
192 if (LOG_LV_NONE == loglevel[cat]) {
193 this->loglevel[cat] = toLevelTag(log4cxx::Logger::getLogger(categoryTable[cat])->getLevel());
196 return loglevel[cat];
200 * set category's log level.
202 * @param category to set log level
204 * @retval true succeed
205 * @retval false failed
207 virtual inline bool setLogLevel(LOG_CATEGORY_TAG cat, LOG_LEVEL_TAG level)
210 log4cxx::Logger::getLogger(categoryTable[cat])->setLevel(toLevel(level));
212 catch (const std::exception& ex) {
216 loglevel[cat] = level;
223 * @param category that logging matter occured
224 * @param log message id
226 * @param current file
227 * @param current line
230 virtual inline void putLogFatal(LOG_CATEGORY_TAG cat, const unsigned int message_id, const std::string& message, const char *file, int line)
233 snprintf(buf, BUF_LEN, "%s%d%03d%04d %s %s", LOGGER_PROCESS_ID, LOG_LV_FATAL, cat, message_id, message.c_str(), hostname);
235 log4cxx::Logger::getLogger(categoryTable[cat])->forcedLog(log4cxx::Level::getFatal(), buf, log4cxx::spi::LocationInfo(file, "", line));
237 catch (const std::exception& ex) {
238 std::ostringstream oss;
239 oss << "Logging Error (Fatal Log) : " << ex.what();
240 errorConf(1, oss.str(), __FILE__, __LINE__);
246 * @param category that logging matter occured
247 * @param log message id
249 * @param current file
250 * @param current line
253 //! output fatal log.
254 virtual inline void putLogError(LOG_CATEGORY_TAG cat, const unsigned int message_id, const std::string& message, const char *file, int line)
257 snprintf(buf, BUF_LEN, "%s%d%03d%04d %s %s", LOGGER_PROCESS_ID, LOG_LV_ERROR, cat, message_id, message.c_str(), hostname);
259 log4cxx::Logger::getLogger(categoryTable[cat])->forcedLog(log4cxx::Level::getError(), buf, log4cxx::spi::LocationInfo(file, "", line));
261 catch (const std::exception& ex) {
262 std::ostringstream oss;
263 oss << "Logging Error (Error Log) : " << ex.what();
264 errorConf(2, oss.str(), __FILE__, __LINE__);
270 * @param category that logging matter occured
271 * @param log message id
273 * @param current file
274 * @param current line
277 //! output fatal log.
278 virtual inline void putLogWarn(LOG_CATEGORY_TAG cat, const unsigned int message_id, const std::string& message, const char *file, int line)
281 snprintf(buf, BUF_LEN, "%s%d%03d%04d %s %s", LOGGER_PROCESS_ID, LOG_LV_WARN, cat, message_id, message.c_str(), hostname);
283 log4cxx::Logger::getLogger(categoryTable[cat])->forcedLog(log4cxx::Level::getWarn(), buf, log4cxx::spi::LocationInfo(file, "", line));
285 catch (const std::exception& ex) {
286 std::ostringstream oss;
287 oss << "Logging Error (Warn Log) : " << ex.what();
288 errorConf(3, oss.str(), __FILE__, __LINE__);
294 * @param category that logging matter occured
295 * @param log message id
297 * @param current file
298 * @param current line
301 //! output fatal log.
302 virtual inline void putLogInfo(LOG_CATEGORY_TAG cat, const unsigned int message_id, const std::string& message, const char *file, int line)
305 snprintf(buf, BUF_LEN, "%s%d%03d%04d %s %s", LOGGER_PROCESS_ID, LOG_LV_INFO, cat, message_id, message.c_str(), hostname);
307 log4cxx::Logger::getLogger(categoryTable[cat])->forcedLog(log4cxx::Level::getInfo(), buf, log4cxx::spi::LocationInfo(file, "", line));
309 catch (const std::exception& ex) {
310 std::ostringstream oss;
311 oss << "Logging Error (Info Log) : " << ex.what();
312 errorConf(4, oss.str(), __FILE__, __LINE__);
318 * @param category that logging matter occured
319 * @param log message id
321 * @param current file
322 * @param current line
325 virtual inline void putLogDebug(LOG_CATEGORY_TAG cat, const unsigned int message_id, const std::string& message, const char *file, int line)
328 snprintf(buf, BUF_LEN, "%s%d%03d%04d %s %s", LOGGER_PROCESS_ID, LOG_LV_DEBUG, cat, message_id, message.c_str(), hostname);
330 log4cxx::Logger::getLogger(categoryTable[cat])->forcedLog(log4cxx::Level::getDebug(), buf, log4cxx::spi::LocationInfo(file, "", line));
332 catch (const std::exception& ex) {
333 std::ostringstream oss;
334 oss << "Logging Error (Debug Log) : " << ex.what();
335 errorConf(5, oss.str(), __FILE__, __LINE__);
340 #endif //__LOGGER_IMPL_H__