Add support for logging JSON to stdout and stderr.

This commit is contained in:
Sadie Powell 2022-08-24 00:13:14 +01:00
parent a5c2a94bd8
commit cf47e0f3f8
2 changed files with 45 additions and 9 deletions

View File

@ -1367,7 +1367,7 @@
# not loaded the oper accounts are still protected by a password. #
#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#
# JSON logging module: Allows writing messages to the system log. #
# JSON logging module: Allows writing messages to a JSON file. #
# This module is in extras. Re-run configure with: #
# ./configure --enable-extras log_json
#<module name="log_json">
@ -1376,6 +1376,14 @@
# target="inspircd.json"
# level="normal"
# type="* -USERINPUT -USEROUTPUT">
#
#<log method="json-stderr"
# level="normal"
# type="* -USERINPUT -USEROUTPUT">
#
#<log method="json-stdout"
# level="normal"
# type="* -USERINPUT -USEROUTPUT">
#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#
# SQL logging module: Allows writing messages to an SQL database.. #

View File

@ -29,6 +29,9 @@ class JSONMethod final
, public Timer
{
private:
// Whether to autoclose the file on exit.
bool autoclose;
// The file to which the log is written.
FILE* file;
@ -45,8 +48,9 @@ public:
// RapidJSON API: The type of character that this writer accepts.
typedef char Ch;
JSONMethod(const std::string& n, FILE* fh, unsigned long fl) ATTR_NOT_NULL(3)
JSONMethod(const std::string& n, FILE* fh, unsigned long fl, bool ac) ATTR_NOT_NULL(3)
: Timer(15*60, true)
, autoclose(ac)
, file(fh)
, flush(fl)
, name(n)
@ -57,7 +61,8 @@ public:
~JSONMethod() override
{
fclose(file);
if (autoclose)
fclose(file);
}
// RapidJSON API: We implement our own flushing in OnLog.
@ -117,11 +122,11 @@ public:
}
};
class JSONEngine final
class JSONFileEngine final
: public Log::Engine
{
public:
JSONEngine(Module* Creator) ATTR_NOT_NULL(2)
JSONFileEngine(Module* Creator) ATTR_NOT_NULL(2)
: Log::Engine(Creator, "json")
{
}
@ -141,7 +146,26 @@ public:
}
const unsigned long flush = tag->getUInt("flush", 20, 1);
return std::make_shared<JSONMethod>(fulltarget, fh, flush);
return std::make_shared<JSONMethod>(fulltarget, fh, flush, true);
}
};
class JSONStreamEngine final
: public Log::Engine
{
private:
FILE* file;
public:
JSONStreamEngine(Module* Creator, const std::string& Name, FILE* fh) ATTR_NOT_NULL(2, 4)
: Log::Engine(Creator, Name)
, file(fh)
{
}
Log::MethodPtr Create(std::shared_ptr<ConfigTag> tag) override
{
return std::make_shared<JSONMethod>(name, file, 1, false);
}
};
@ -149,12 +173,16 @@ class ModuleLogJSON final
: public Module
{
private:
JSONEngine engine;
JSONFileEngine log;
JSONStreamEngine stderrlog;
JSONStreamEngine stdoutlog;
public:
ModuleLogJSON()
: Module(VF_VENDOR, "Provides the ability to write logs to syslog.")
, engine(this)
: Module(VF_VENDOR, "Provides the ability to write logs to a JSON file.")
, log(this)
, stderrlog(this, "json-stderr", stderr)
, stdoutlog(this, "json-stdout", stdout)
{
}
};