Logger configuration & control output level

This commit is contained in:
2022-12-11 08:50:45 +08:00
parent 37310d4f28
commit 31bd0c87ae
7 changed files with 145 additions and 64 deletions
+1 -1
View File
@@ -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.
+18
View File
@@ -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
};
+69 -35
View File
@@ -1,9 +1,18 @@
#include "clog.h"
#include "cfg.h"
#include "php_wrenchboard_log.h"
#include <gelfcpp/output/GelfUDPOutput.hpp>
//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());
}
}
// */
}
// */
+1 -1
View File
@@ -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);
+34 -22
View File
@@ -13,33 +13,43 @@
#include <libpq-fe.h>
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<TLogLevel>(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);
} // */
}
}
+20 -5
View File
@@ -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 <typename T>
@@ -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<T>::Log()
template <typename T>
std::ostringstream& Log<T>::Get(TLogLevel level)
{
currentLevel = level;
os << "- " << NowTime();
os << " " << ToString(level);
os << " [" << getpid() << "]: ";
@@ -61,18 +66,22 @@ std::ostringstream& Log<T>::Get(TLogLevel level)
template <typename T>
Log<T>::~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 <typename T>
TLogLevel& Log<T>::ReportingLevel()
{
static TLogLevel reportingLevel = FLOG_MAX;
return reportingLevel;
return global_log_level;
}
template <typename T>
@@ -87,8 +96,8 @@ TLogLevel Log<T>::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<T>::FromString(const std::string& level)
return logINFO;
}
template <typename T>
void Log<T>::SetReportingLevel(TLogLevel level)
{
global_log_level = level;
}
class Output2FILE
{
public:
+2
View File
@@ -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 */