2019-06-14 14:00:37 +00:00
|
|
|
#include "Loggers.h"
|
|
|
|
|
|
|
|
#include <iostream>
|
|
|
|
#include <Poco/SyslogChannel.h>
|
|
|
|
#include <Poco/Util/AbstractConfiguration.h>
|
|
|
|
#include "OwnFormattingChannel.h"
|
|
|
|
#include "OwnPatternFormatter.h"
|
|
|
|
#include <Poco/ConsoleChannel.h>
|
|
|
|
#include <Poco/File.h>
|
|
|
|
#include <Poco/Logger.h>
|
|
|
|
#include <Poco/Net/RemoteSyslogChannel.h>
|
|
|
|
#include <Poco/Path.h>
|
|
|
|
|
2019-09-03 19:47:48 +00:00
|
|
|
namespace DB
|
|
|
|
{
|
|
|
|
class SensitiveDataMasker;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-06-14 14:00:37 +00:00
|
|
|
// TODO: move to libcommon
|
|
|
|
static std::string createDirectory(const std::string & file)
|
|
|
|
{
|
|
|
|
auto path = Poco::Path(file).makeParent();
|
|
|
|
if (path.toString().empty())
|
|
|
|
return "";
|
|
|
|
Poco::File(path).createDirectories();
|
|
|
|
return path.toString();
|
|
|
|
};
|
|
|
|
|
2020-01-23 20:19:51 +00:00
|
|
|
void Loggers::setTextLog(std::shared_ptr<DB::TextLog> log, int max_priority)
|
2019-08-01 10:31:29 +00:00
|
|
|
{
|
2019-07-30 14:04:18 +00:00
|
|
|
text_log = log;
|
2020-01-23 20:19:51 +00:00
|
|
|
text_log_max_priority = max_priority;
|
2019-07-30 14:04:18 +00:00
|
|
|
}
|
|
|
|
|
2019-06-14 14:00:37 +00:00
|
|
|
void Loggers::buildLoggers(Poco::Util::AbstractConfiguration & config, Poco::Logger & logger /*_root*/, const std::string & cmd_name)
|
|
|
|
{
|
2019-07-31 21:59:26 +00:00
|
|
|
if (split)
|
|
|
|
if (auto log = text_log.lock())
|
2020-01-23 20:19:51 +00:00
|
|
|
split->addTextLog(log, text_log_max_priority);
|
2019-07-31 14:03:23 +00:00
|
|
|
|
2019-06-14 14:00:37 +00:00
|
|
|
auto current_logger = config.getString("logger", "");
|
2021-05-08 16:09:17 +00:00
|
|
|
if (config_logger == current_logger) //-V1051
|
2019-06-14 14:00:37 +00:00
|
|
|
return;
|
2019-07-31 21:59:26 +00:00
|
|
|
|
2019-06-14 14:00:37 +00:00
|
|
|
config_logger = current_logger;
|
|
|
|
|
|
|
|
bool is_daemon = config.getBool("application.runAsDaemon", false);
|
|
|
|
|
|
|
|
/// Split logs to ordinary log, error log, syslog and console.
|
|
|
|
/// Use extended interface of Channel for more comprehensive logging.
|
2019-07-30 14:04:18 +00:00
|
|
|
split = new DB::OwnSplitChannel();
|
2019-06-14 14:00:37 +00:00
|
|
|
|
2021-05-03 16:29:50 +00:00
|
|
|
auto log_level_string = config.getString("logger.level", "trace");
|
2021-05-03 08:56:32 +00:00
|
|
|
|
2021-05-03 16:29:50 +00:00
|
|
|
/// different channels (log, console, syslog) may have different loglevels configured
|
|
|
|
/// The maximum (the most verbose) of those will be used as default for Poco loggers
|
|
|
|
int max_log_level = 0;
|
2021-05-03 08:56:32 +00:00
|
|
|
|
2019-06-14 14:00:37 +00:00
|
|
|
const auto log_path = config.getString("logger.log", "");
|
|
|
|
if (!log_path.empty())
|
|
|
|
{
|
|
|
|
createDirectory(log_path);
|
2021-05-03 16:29:50 +00:00
|
|
|
std::cerr << "Logging " << log_level_string << " to " << log_path << std::endl;
|
|
|
|
auto log_level = Poco::Logger::parseLevel(log_level_string);
|
|
|
|
if (log_level > max_log_level)
|
|
|
|
{
|
|
|
|
max_log_level = log_level;
|
|
|
|
}
|
2019-06-14 14:00:37 +00:00
|
|
|
|
|
|
|
// Set up two channel chains.
|
|
|
|
log_file = new Poco::FileChannel;
|
|
|
|
log_file->setProperty(Poco::FileChannel::PROP_PATH, Poco::Path(log_path).absolute().toString());
|
|
|
|
log_file->setProperty(Poco::FileChannel::PROP_ROTATION, config.getRawString("logger.size", "100M"));
|
|
|
|
log_file->setProperty(Poco::FileChannel::PROP_ARCHIVE, "number");
|
|
|
|
log_file->setProperty(Poco::FileChannel::PROP_COMPRESS, config.getRawString("logger.compress", "true"));
|
|
|
|
log_file->setProperty(Poco::FileChannel::PROP_PURGECOUNT, config.getRawString("logger.count", "1"));
|
|
|
|
log_file->setProperty(Poco::FileChannel::PROP_FLUSH, config.getRawString("logger.flush", "true"));
|
|
|
|
log_file->setProperty(Poco::FileChannel::PROP_ROTATEONOPEN, config.getRawString("logger.rotateOnOpen", "false"));
|
|
|
|
log_file->open();
|
|
|
|
|
2021-05-04 19:58:34 +00:00
|
|
|
Poco::AutoPtr<OwnPatternFormatter> pf = new OwnPatternFormatter;
|
2019-06-14 14:00:37 +00:00
|
|
|
|
|
|
|
Poco::AutoPtr<DB::OwnFormattingChannel> log = new DB::OwnFormattingChannel(pf, log_file);
|
2021-05-03 16:29:50 +00:00
|
|
|
log->setLevel(log_level);
|
2019-06-14 14:00:37 +00:00
|
|
|
split->addChannel(log);
|
|
|
|
}
|
|
|
|
|
|
|
|
const auto errorlog_path = config.getString("logger.errorlog", "");
|
|
|
|
if (!errorlog_path.empty())
|
|
|
|
{
|
|
|
|
createDirectory(errorlog_path);
|
2021-05-03 08:56:32 +00:00
|
|
|
|
|
|
|
// NOTE: we don't use notice & critical in the code, so in practice error log collects fatal & error & warning.
|
|
|
|
// (!) Warnings are important, they require attention and should never be silenced / ignored.
|
|
|
|
auto errorlog_level = Poco::Logger::parseLevel(config.getString("logger.errorlog_level", "notice"));
|
|
|
|
if (errorlog_level > max_log_level)
|
|
|
|
{
|
|
|
|
max_log_level = errorlog_level;
|
|
|
|
}
|
|
|
|
|
2019-06-14 14:00:37 +00:00
|
|
|
std::cerr << "Logging errors to " << errorlog_path << std::endl;
|
|
|
|
|
|
|
|
error_log_file = new Poco::FileChannel;
|
|
|
|
error_log_file->setProperty(Poco::FileChannel::PROP_PATH, Poco::Path(errorlog_path).absolute().toString());
|
|
|
|
error_log_file->setProperty(Poco::FileChannel::PROP_ROTATION, config.getRawString("logger.size", "100M"));
|
|
|
|
error_log_file->setProperty(Poco::FileChannel::PROP_ARCHIVE, "number");
|
|
|
|
error_log_file->setProperty(Poco::FileChannel::PROP_COMPRESS, config.getRawString("logger.compress", "true"));
|
|
|
|
error_log_file->setProperty(Poco::FileChannel::PROP_PURGECOUNT, config.getRawString("logger.count", "1"));
|
|
|
|
error_log_file->setProperty(Poco::FileChannel::PROP_FLUSH, config.getRawString("logger.flush", "true"));
|
|
|
|
error_log_file->setProperty(Poco::FileChannel::PROP_ROTATEONOPEN, config.getRawString("logger.rotateOnOpen", "false"));
|
|
|
|
|
2021-05-04 19:58:34 +00:00
|
|
|
Poco::AutoPtr<OwnPatternFormatter> pf = new OwnPatternFormatter;
|
2019-06-14 14:00:37 +00:00
|
|
|
|
|
|
|
Poco::AutoPtr<DB::OwnFormattingChannel> errorlog = new DB::OwnFormattingChannel(pf, error_log_file);
|
2021-05-03 08:56:32 +00:00
|
|
|
errorlog->setLevel(errorlog_level);
|
2019-06-14 14:00:37 +00:00
|
|
|
errorlog->open();
|
|
|
|
split->addChannel(errorlog);
|
|
|
|
}
|
|
|
|
|
2021-05-04 17:43:23 +00:00
|
|
|
if (config.getBool("logger.use_syslog", false))
|
2019-06-14 14:00:37 +00:00
|
|
|
{
|
|
|
|
//const std::string & cmd_name = commandName();
|
2021-05-03 16:29:50 +00:00
|
|
|
auto syslog_level = Poco::Logger::parseLevel(config.getString("logger.syslog_level", log_level_string));
|
2021-05-03 08:56:32 +00:00
|
|
|
if (syslog_level > max_log_level)
|
|
|
|
{
|
|
|
|
max_log_level = syslog_level;
|
|
|
|
}
|
2019-06-14 14:00:37 +00:00
|
|
|
|
|
|
|
if (config.has("logger.syslog.address"))
|
|
|
|
{
|
|
|
|
syslog_channel = new Poco::Net::RemoteSyslogChannel();
|
|
|
|
// syslog address
|
|
|
|
syslog_channel->setProperty(Poco::Net::RemoteSyslogChannel::PROP_LOGHOST, config.getString("logger.syslog.address"));
|
|
|
|
if (config.has("logger.syslog.hostname"))
|
|
|
|
{
|
|
|
|
syslog_channel->setProperty(Poco::Net::RemoteSyslogChannel::PROP_HOST, config.getString("logger.syslog.hostname"));
|
|
|
|
}
|
|
|
|
syslog_channel->setProperty(Poco::Net::RemoteSyslogChannel::PROP_FORMAT, config.getString("logger.syslog.format", "syslog"));
|
|
|
|
syslog_channel->setProperty(
|
|
|
|
Poco::Net::RemoteSyslogChannel::PROP_FACILITY, config.getString("logger.syslog.facility", "LOG_USER"));
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
syslog_channel = new Poco::SyslogChannel();
|
|
|
|
syslog_channel->setProperty(Poco::SyslogChannel::PROP_NAME, cmd_name);
|
|
|
|
syslog_channel->setProperty(Poco::SyslogChannel::PROP_OPTIONS, config.getString("logger.syslog.options", "LOG_CONS|LOG_PID"));
|
|
|
|
syslog_channel->setProperty(Poco::SyslogChannel::PROP_FACILITY, config.getString("logger.syslog.facility", "LOG_DAEMON"));
|
|
|
|
}
|
|
|
|
syslog_channel->open();
|
|
|
|
|
2021-05-04 17:43:23 +00:00
|
|
|
Poco::AutoPtr<OwnPatternFormatter> pf = new OwnPatternFormatter;
|
2019-06-14 14:00:37 +00:00
|
|
|
|
|
|
|
Poco::AutoPtr<DB::OwnFormattingChannel> log = new DB::OwnFormattingChannel(pf, syslog_channel);
|
2021-05-03 08:56:32 +00:00
|
|
|
log->setLevel(syslog_level);
|
|
|
|
|
2019-06-14 14:00:37 +00:00
|
|
|
split->addChannel(log);
|
|
|
|
}
|
|
|
|
|
2020-02-21 19:17:03 +00:00
|
|
|
bool should_log_to_console = isatty(STDIN_FILENO) || isatty(STDERR_FILENO);
|
|
|
|
bool color_logs_by_default = isatty(STDERR_FILENO);
|
2020-02-03 02:33:31 +00:00
|
|
|
|
2019-06-14 14:00:37 +00:00
|
|
|
if (config.getBool("logger.console", false)
|
2020-02-21 19:17:03 +00:00
|
|
|
|| (!config.hasProperty("logger.console") && !is_daemon && should_log_to_console))
|
2019-06-14 14:00:37 +00:00
|
|
|
{
|
2020-02-21 19:17:03 +00:00
|
|
|
bool color_enabled = config.getBool("logger.color_terminal", color_logs_by_default);
|
2020-02-06 09:11:49 +00:00
|
|
|
|
2021-05-13 13:37:44 +00:00
|
|
|
auto console_log_level_string = config.getString("logger.console_log_level", log_level_string);
|
|
|
|
auto console_log_level = Poco::Logger::parseLevel(console_log_level_string);
|
|
|
|
if (console_log_level > max_log_level)
|
2021-05-03 08:56:32 +00:00
|
|
|
{
|
2021-05-13 13:37:44 +00:00
|
|
|
max_log_level = console_log_level;
|
2021-05-03 08:56:32 +00:00
|
|
|
}
|
|
|
|
|
2021-05-04 17:43:23 +00:00
|
|
|
Poco::AutoPtr<OwnPatternFormatter> pf = new OwnPatternFormatter(color_enabled);
|
2020-02-03 02:33:31 +00:00
|
|
|
Poco::AutoPtr<DB::OwnFormattingChannel> log = new DB::OwnFormattingChannel(pf, new Poco::ConsoleChannel);
|
2021-05-13 13:37:44 +00:00
|
|
|
logger.warning("Logging " + console_log_level_string + " to console");
|
|
|
|
log->setLevel(console_log_level);
|
2019-06-14 14:00:37 +00:00
|
|
|
split->addChannel(log);
|
|
|
|
}
|
|
|
|
|
|
|
|
split->open();
|
|
|
|
logger.close();
|
|
|
|
logger.setChannel(split);
|
|
|
|
|
|
|
|
// Global logging level (it can be overridden for specific loggers).
|
2021-05-03 08:56:32 +00:00
|
|
|
logger.setLevel(max_log_level);
|
2019-06-14 14:00:37 +00:00
|
|
|
|
|
|
|
// Set level to all already created loggers
|
|
|
|
std::vector<std::string> names;
|
|
|
|
//logger_root = Logger::root();
|
|
|
|
logger.root().names(names);
|
|
|
|
for (const auto & name : names)
|
2021-05-03 08:56:32 +00:00
|
|
|
logger.root().get(name).setLevel(max_log_level);
|
2019-06-14 14:00:37 +00:00
|
|
|
|
|
|
|
// Attach to the root logger.
|
2021-05-03 08:56:32 +00:00
|
|
|
logger.root().setLevel(max_log_level);
|
2019-06-14 14:00:37 +00:00
|
|
|
logger.root().setChannel(logger.getChannel());
|
|
|
|
|
|
|
|
// Explicitly specified log levels for specific loggers.
|
2020-04-27 18:21:01 +00:00
|
|
|
{
|
|
|
|
Poco::Util::AbstractConfiguration::Keys loggers_level;
|
|
|
|
config.keys("logger.levels", loggers_level);
|
2019-06-14 14:00:37 +00:00
|
|
|
|
2020-04-27 18:21:01 +00:00
|
|
|
if (!loggers_level.empty())
|
|
|
|
{
|
|
|
|
for (const auto & key : loggers_level)
|
|
|
|
{
|
|
|
|
if (key == "logger" || key.starts_with("logger["))
|
|
|
|
{
|
|
|
|
const std::string name(config.getString("logger.levels." + key + ".name"));
|
|
|
|
const std::string level(config.getString("logger.levels." + key + ".level"));
|
|
|
|
logger.root().get(name).setLevel(level);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// Legacy syntax
|
|
|
|
const std::string level(config.getString("logger.levels." + key, "trace"));
|
|
|
|
logger.root().get(key).setLevel(level);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-06-14 14:00:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void Loggers::closeLogs(Poco::Logger & logger)
|
|
|
|
{
|
|
|
|
if (log_file)
|
|
|
|
log_file->close();
|
|
|
|
if (error_log_file)
|
|
|
|
error_log_file->close();
|
2019-06-20 07:17:21 +00:00
|
|
|
// Shouldn't syslog_channel be closed here too?
|
2019-06-14 14:00:37 +00:00
|
|
|
|
|
|
|
if (!log_file)
|
|
|
|
logger.warning("Logging to console but received signal to close log file (ignoring).");
|
|
|
|
}
|