From 31bd0c87ae68335a1d1b101d8036194740ee9541 Mon Sep 17 00:00:00 2001 From: acidumirae Date: Sun, 11 Dec 2022 08:50:45 +0800 Subject: [PATCH] Logger configuration & control output level --- wrenchboard/configs/php.ini | 2 +- wrenchboard/etc/wrenchboard_api.conf | 18 +++++ wrenchboard/src/core/clog.cc | 104 +++++++++++++++++--------- wrenchboard/src/core/pgsql.cc | 2 +- wrenchboard/src/core/wrenchboard.cc | 56 ++++++++------ wrenchboard/src/include/clog.h | 25 +++++-- wrenchboard/src/include/wrenchboard.h | 2 + 7 files changed, 145 insertions(+), 64 deletions(-) diff --git a/wrenchboard/configs/php.ini b/wrenchboard/configs/php.ini index b8556812..130121ec 100644 --- a/wrenchboard/configs/php.ini +++ b/wrenchboard/configs/php.ini @@ -313,7 +313,7 @@ serialize_precision = -1 ; This directive allows you to disable certain functions for security reasons. ; It receives a comma-delimited list of function names. ; http://php.net/disable-functions -disable_functions = dl,exec,passthru,shell_exec,system,proc_open,popen,parse_ini_file,show_source,phpinfo +;disable_functions = dl,exec,passthru,shell_exec,system,proc_open,popen,parse_ini_file,show_source,phpinfo ; This directive allows you to disable certain classes for security reasons. ; It receives a comma-delimited list of class names. diff --git a/wrenchboard/etc/wrenchboard_api.conf b/wrenchboard/etc/wrenchboard_api.conf index ec92e831..01844a15 100644 --- a/wrenchboard/etc/wrenchboard_api.conf +++ b/wrenchboard/etc/wrenchboard_api.conf @@ -143,3 +143,21 @@ onesignal: app_id = "e80afcf4-6fad-493c-b5cc-452d37196dea"; }; +logger: +{ + host = "10.0.0.112"; + port = 12201L; + file = 0L; + level = "DEBUG"; # 3L; + # 0 - ERROR + # 1 - WARNING + # 2 - INFO + # 3 - DEBUG + # 4 - DEBUG1 + # 5 - DEBUG2 + # 6 - DEBUG3 + # 7 - DEBUG4 + # 8 - SQL + # 9 - FLOG_MAX +}; + diff --git a/wrenchboard/src/core/clog.cc b/wrenchboard/src/core/clog.cc index a5efb863..20228656 100644 --- a/wrenchboard/src/core/clog.cc +++ b/wrenchboard/src/core/clog.cc @@ -1,9 +1,18 @@ #include "clog.h" +#include "cfg.h" #include "php_wrenchboard_log.h" +#include -//gelfcpp::output::GelfUDPOutput graylog("10.0.0.112", 12201); +using namespace gelfcpp::output; + +TLogLevel global_log_level = FLOG_MAX; // By default log everything + +GelfUDPOutput graylog(CfgReadChar("logger.host"), CfgReadLong("logger.port")); void logfmt( TLogLevel level, const char * format, ... ) { + if (level > global_log_level) { + return; // We do not log messages greater than global log level! + } size_t n = 32678; char buffer[n]; bzero(buffer, n); @@ -26,13 +35,29 @@ void logfmt( TLogLevel level, const char * format, ... ) { } va_end (args); // Graylog - /*if (strlen(buffer) > 0) { - GELF_MESSAGE(graylog) - (CurrentTimeStamp()) - ("level", FILELog::ToString(level).c_str()) - ("pid", getpid()) - (buffer); - } // */ + if (strlen(buffer) > 0) { + if (false) { + FILE_LOG(logERROR) << "graylog is null!" ; + } else { + gelfcpp::GelfMessageStream stream(graylog); + if (stream) { + stream.Send(graylog) = gelfcpp::GelfMessageBuilder() + (CurrentTimeStamp()) + ("level", FILELog::ToString(level).c_str()) + ("pid", getpid()) + (buffer); + } else { + FILE_LOG(logERROR) << "GelfMessageStream failed!" ; + } + GELF_MESSAGE(graylog) + (CurrentTimeStamp()) + ("level", FILELog::ToString(level).c_str()) + ("pid", getpid()) + (buffer); + } + } else { + FILE_LOG(logERROR) << "buffer length is " << strlen(buffer) ; + }// */ } catch(const std::exception& e) { FILE_LOG(logERROR) << e.what(); } @@ -54,36 +79,45 @@ void logfmt( TLogLevel level, const char * format, ... ) { } */ -/* void GraylogStream(std::string raw) { +void GraylogStream(std::string raw) { +/* + if (!raw.empty()) { + GELF_MESSAGE(graylog) + (CurrentTimeStamp()) + ("pid", getpid()) + ("level", "FLOG_MAX") + (raw.c_str()); + } +*/ + std::string marker1 ("]: "); + std::string marker2 (" ["); + std::string marker3 (" "); - std::string marker1 ("]: "); - std::string marker2 (" ["); - std::string marker3 (" "); + std::size_t found = raw.find(marker1); + if (found != std::string::npos) { + std::string msg = raw.substr (found + 3); + std::string level ("FLOG_MAX"); - std::size_t found = raw.find(marker1); - if (found != std::string::npos) { - std::string msg = raw.substr (found + 3); - std::string level ("FLOG_MAX"); + found = raw.find(marker2); + if (found != std::string::npos) { + std::string str = raw.substr(0, found - 1); + boost::trim_right(str); + found = str.find(marker3); + /* while (found != std::string::npos) { + level = str.substr(found); + found = str.find(marker3); + } // */ + } - found = raw.find(marker2); - if (found != std::string::npos) { - std::string str = raw.substr(0, found - 1); - boost::trim_right(str); - found = str.find(marker3); - while (found != std::string::npos) { - level = str.substr(found); - found = str.find(marker3); - } - } - - if (!msg.empty()) { - GELF_MESSAGE(graylog) - (CurrentTimeStamp()) - ("pid", getpid()) - ("level", level.c_str()) - (msg.c_str()); - } - } + if (!msg.empty()) { + GELF_MESSAGE(graylog) + (CurrentTimeStamp()) + ("pid", getpid()) + ("level", level.c_str()) + (msg.c_str()); + } + } + // */ } // */ diff --git a/wrenchboard/src/core/pgsql.cc b/wrenchboard/src/core/pgsql.cc index 4fabbadb..6fce9941 100644 --- a/wrenchboard/src/core/pgsql.cc +++ b/wrenchboard/src/core/pgsql.cc @@ -65,7 +65,7 @@ int pgsql_exec(const char * format, ... ) //perror (buffer); va_end (args); - FILE_LOG(logSQL) << "About to run query: "; + FILE_LOG(logSQL) << "About to exec query: "; FILE_LOG(logSQL) << query; /* Escape any PostgrsSQL-unsafe characters */ // user_size = PQescapeStringConn (conn, user_data, username, strlen(username), &pqesc_error); diff --git a/wrenchboard/src/core/wrenchboard.cc b/wrenchboard/src/core/wrenchboard.cc index a9c6b06e..e989018e 100644 --- a/wrenchboard/src/core/wrenchboard.cc +++ b/wrenchboard/src/core/wrenchboard.cc @@ -13,33 +13,43 @@ #include WrenchBoard::WrenchBoard() { - // Open log - this->pFile = fopen(WRENCHBOARD_LOG, "a"); - Output2FILE::Stream() = pFile; - FILE_LOG(logINFO) << "WRENCHBOARD is starting..."; - + // Read config CfgReadConfig(); + + this->logFile = CfgReadLong("logger.file"); + // global_log_level = static_cast(CfgReadLong("logger.level")); + global_log_level = FILELog().FromString(CfgReadChar("logger.level")); + FILELog().SetReportingLevel(global_log_level); + + // Open log + if (this->logFile == 1) { + this->pFile = fopen(WRENCHBOARD_LOG, "a"); + Output2FILE::Stream() = pFile; + } + FILE_LOG(logINFO) << "WRENCHBOARD is starting..."; + logfmt(logINFO, "Version from config: %s", CfgReadChar("version")); - + // Open database FILE_LOG(logDEBUG) << "Connecting to database..."; FILE_LOG(logDEBUG) << "host=" << CfgReadChar("database.host") << ", name=" << CfgReadChar("database.name") << ", user=" << CfgReadChar("database.user") << ", pass=***hidden***, port=" << CfgReadLong("database.port"); this->db = 0; try { - this->db = pgsql_db_connect(CfgReadChar("database.host"), - CfgReadChar("database.name"), - CfgReadChar("database.user"), - CfgReadChar("database.pass"), - CfgReadLong("database.port") ); + this->db = pgsql_db_connect( + CfgReadChar("database.host"), + CfgReadChar("database.name"), + CfgReadChar("database.user"), + CfgReadChar("database.pass"), + CfgReadLong("database.port") ); FILE_LOG(logDEBUG) << "pgsql_db_connect() done!"; } catch (const std::exception &e) { - FILE_LOG(logDEBUG) << "Exception: " << e.what(); + FILE_LOG(logDEBUG) << "Exception: " << e.what(); } catch (const std::string &e) { - FILE_LOG(logDEBUG) << "Exception: " << e; + FILE_LOG(logDEBUG) << "Exception: " << e; } catch (const char *e) { - FILE_LOG(logDEBUG) << "Exception: " << e; + FILE_LOG(logDEBUG) << "Exception: " << e; } catch (...) { FILE_LOG(logDEBUG) << "Unknown Exception!"; } @@ -49,11 +59,11 @@ WrenchBoard::WrenchBoard() { long WrenchBoard::wrenchboard_api(CVars in, CVars &out) { long retval = PHP_API_BAD_PARAM; try { - retval = wrenchboard_api_main(in, out); + retval = wrenchboard_api_main(in, out); } catch (bad_parameter) { - out["status"] = "Incorrect input parameter"; + out["status"] = "Incorrect input parameter"; } catch (...) { - out["status"] = "Unhandled exception"; + out["status"] = "Unhandled exception"; } return retval; } @@ -73,12 +83,14 @@ void WrenchBoard::logMessage(const char *message) { WrenchBoard::~WrenchBoard() { FILE_LOG(logINFO) << "WRENCHBOARD is stopping..."; if (db>0) { - FILE_LOG(logDEBUG) << "Closing database connection"; - pgsql_close(); + FILE_LOG(logDEBUG) << "Closing database connection"; + pgsql_close(); } // Do we need it? - if (this->pFile) { - fclose(this->pFile); - } // */ + if (this->logFile == 1) { + if (this->pFile) { + fclose(this->pFile); + } // */ + } } diff --git a/wrenchboard/src/include/clog.h b/wrenchboard/src/include/clog.h index 96746c34..34028bc9 100644 --- a/wrenchboard/src/include/clog.h +++ b/wrenchboard/src/include/clog.h @@ -23,6 +23,8 @@ void GraylogStream(std::string raw); enum TLogLevel {logERROR, logWARNING, logINFO, logDEBUG, logDEBUG1, logDEBUG2, logDEBUG3, logDEBUG4, logSQL, FLOG_MAX}; +extern TLogLevel global_log_level; + void logfmt( TLogLevel level, const char * format, ... ); template @@ -36,8 +38,10 @@ public: static TLogLevel& ReportingLevel(); static std::string ToString(TLogLevel level); static TLogLevel FromString(const std::string& level); + void SetReportingLevel(TLogLevel level = FLOG_MAX); protected: std::ostringstream os; + TLogLevel currentLevel = FLOG_MAX; private: Log(const Log&); Log& operator =(const Log&); @@ -51,6 +55,7 @@ Log::Log() template std::ostringstream& Log::Get(TLogLevel level) { + currentLevel = level; os << "- " << NowTime(); os << " " << ToString(level); os << " [" << getpid() << "]: "; @@ -61,18 +66,22 @@ std::ostringstream& Log::Get(TLogLevel level) template Log::~Log() { + if (currentLevel > global_log_level) { + return; // We do not log messages greater than global log level! + } + // Graylog - //GraylogStream(os.str()); + GraylogStream(os.str()); os << std::endl; + T::Output(os.str()); } template TLogLevel& Log::ReportingLevel() { - static TLogLevel reportingLevel = FLOG_MAX; - return reportingLevel; + return global_log_level; } template @@ -87,8 +96,8 @@ TLogLevel Log::FromString(const std::string& level) { if (level == "FLOG_MAX") return FLOG_MAX; - if (level == "SQL") - return logSQL; + if (level == "SQL") + return logSQL; if (level == "DEBUG4") return logDEBUG4; if (level == "DEBUG3") @@ -109,6 +118,12 @@ TLogLevel Log::FromString(const std::string& level) return logINFO; } +template +void Log::SetReportingLevel(TLogLevel level) +{ + global_log_level = level; +} + class Output2FILE { public: diff --git a/wrenchboard/src/include/wrenchboard.h b/wrenchboard/src/include/wrenchboard.h index 9d496a9e..bc292892 100644 --- a/wrenchboard/src/include/wrenchboard.h +++ b/wrenchboard/src/include/wrenchboard.h @@ -2,6 +2,7 @@ #define WRENCHBOARD_API_WRENCHBOARD_H #include "vars.h" +#include "clog.h" // A very simple wrenchboard class class WrenchBoard { @@ -15,6 +16,7 @@ public: private: FILE* pFile; int db; + int logFile; }; #endif /* WRENCHBOARD_API_WRENCHBOARD_H */