ClickHouse/base/loggers/OwnPatternFormatter.cpp

87 lines
2.9 KiB
C++
Raw Normal View History

#include "OwnPatternFormatter.h"
#include <functional>
#include <optional>
2020-02-16 08:44:34 +00:00
#include <sys/time.h>
#include <IO/WriteBufferFromString.h>
#include <IO/WriteHelpers.h>
2020-02-03 02:33:31 +00:00
#include <Common/HashTable/Hash.h>
#include <Interpreters/InternalTextLogsQueue.h>
#include <Common/CurrentThread.h>
2020-02-02 02:35:47 +00:00
#include <common/getThreadId.h>
#include <common/terminalColors.h>
#include "Loggers.h"
2020-02-03 02:33:31 +00:00
OwnPatternFormatter::OwnPatternFormatter(const Loggers * loggers_, OwnPatternFormatter::Options options_, bool color_)
: Poco::PatternFormatter(""), loggers(loggers_), options(options_), color(color_)
{
}
void OwnPatternFormatter::formatExtended(const DB::ExtendedLogMessage & msg_ext, std::string & text)
{
DB::WriteBufferFromString wb(text);
const Poco::Message & msg = msg_ext.base;
/// For syslog: tag must be before message and first whitespace.
2019-06-14 14:05:59 +00:00
/// This code is only used in Yandex.Metrika and unneeded in ClickHouse.
2019-06-14 15:49:38 +00:00
if ((options & ADD_LAYER_TAG) && loggers)
{
2019-06-14 15:49:38 +00:00
auto layer = loggers->getLayer();
if (layer)
{
writeCString("layer[", wb);
DB::writeIntText(*layer, wb);
writeCString("]: ", wb);
}
}
/// Change delimiters in date for compatibility with old logs.
DB::writeDateTimeText<'.', ':'>(msg_ext.time_seconds, wb);
DB::writeChar('.', wb);
DB::writeChar('0' + ((msg_ext.time_microseconds / 100000) % 10), wb);
DB::writeChar('0' + ((msg_ext.time_microseconds / 10000) % 10), wb);
DB::writeChar('0' + ((msg_ext.time_microseconds / 1000) % 10), wb);
DB::writeChar('0' + ((msg_ext.time_microseconds / 100) % 10), wb);
DB::writeChar('0' + ((msg_ext.time_microseconds / 10) % 10), wb);
DB::writeChar('0' + ((msg_ext.time_microseconds / 1) % 10), wb);
writeCString(" [ ", wb);
2020-02-03 02:33:31 +00:00
if (color)
2020-02-16 08:44:34 +00:00
writeString(setColor(intHash64(msg_ext.thread_id)), wb);
2020-02-02 02:27:15 +00:00
DB::writeIntText(msg_ext.thread_id, wb);
2020-02-03 02:33:31 +00:00
if (color)
writeCString(resetColor(), wb);
writeCString(" ] ", wb);
/// We write query_id even in case when it is empty (no query context)
/// just to be convenient for various log parsers.
writeCString("{", wb);
2020-02-03 02:33:31 +00:00
if (color)
2020-02-16 08:44:34 +00:00
writeString(setColor(std::hash<std::string>()(msg_ext.query_id)), wb);
DB::writeString(msg_ext.query_id, wb);
2020-02-03 02:33:31 +00:00
if (color)
writeCString(resetColor(), wb);
writeCString("} ", wb);
2018-07-04 13:29:31 +00:00
writeCString("<", wb);
2020-02-03 02:48:10 +00:00
int priority = static_cast<int>(msg.getPriority());
2020-02-03 02:47:28 +00:00
if (color)
writeCString(setColorForLogPriority(priority), wb);
2020-02-03 02:48:10 +00:00
DB::writeString(getPriorityName(priority), wb);
2020-02-03 02:47:28 +00:00
if (color)
writeCString(resetColor(), wb);
writeCString("> ", wb);
DB::writeString(msg.getSource(), wb);
writeCString(": ", wb);
DB::writeString(msg.getText(), wb);
}
void OwnPatternFormatter::format(const Poco::Message & msg, std::string & text)
{
formatExtended(DB::ExtendedLogMessage::getFrom(msg), text);
}