1#include "OwnPatternFormatter.h"
2
3#include <functional>
4#include <optional>
5#include <IO/WriteBufferFromString.h>
6#include <IO/WriteHelpers.h>
7#include <Interpreters/InternalTextLogsQueue.h>
8#include <sys/time.h>
9#include <Common/CurrentThread.h>
10#include <common/getThreadNumber.h>
11#include "Loggers.h"
12
13
14OwnPatternFormatter::OwnPatternFormatter(const Loggers * loggers_, OwnPatternFormatter::Options options_)
15 : Poco::PatternFormatter(""), loggers(loggers_), options(options_)
16{
17}
18
19
20void OwnPatternFormatter::formatExtended(const DB::ExtendedLogMessage & msg_ext, std::string & text)
21{
22 DB::WriteBufferFromString wb(text);
23
24 const Poco::Message & msg = msg_ext.base;
25
26 /// For syslog: tag must be before message and first whitespace.
27 /// This code is only used in Yandex.Metrika and unneeded in ClickHouse.
28 if ((options & ADD_LAYER_TAG) && loggers)
29 {
30 auto layer = loggers->getLayer();
31 if (layer)
32 {
33 writeCString("layer[", wb);
34 DB::writeIntText(*layer, wb);
35 writeCString("]: ", wb);
36 }
37 }
38
39 /// Change delimiters in date for compatibility with old logs.
40 DB::writeDateTimeText<'.', ':'>(msg_ext.time_seconds, wb);
41
42 DB::writeChar('.', wb);
43 DB::writeChar('0' + ((msg_ext.time_microseconds / 100000) % 10), wb);
44 DB::writeChar('0' + ((msg_ext.time_microseconds / 10000) % 10), wb);
45 DB::writeChar('0' + ((msg_ext.time_microseconds / 1000) % 10), wb);
46 DB::writeChar('0' + ((msg_ext.time_microseconds / 100) % 10), wb);
47 DB::writeChar('0' + ((msg_ext.time_microseconds / 10) % 10), wb);
48 DB::writeChar('0' + ((msg_ext.time_microseconds / 1) % 10), wb);
49
50 writeCString(" [ ", wb);
51 DB::writeIntText(msg_ext.thread_number, wb);
52 writeCString(" ] ", wb);
53
54 /// We write query_id even in case when it is empty (no query context)
55 /// just to be convenient for various log parsers.
56 writeCString("{", wb);
57 DB::writeString(msg_ext.query_id, wb);
58 writeCString("} ", wb);
59
60 writeCString("<", wb);
61 DB::writeString(getPriorityName(static_cast<int>(msg.getPriority())), wb);
62 writeCString("> ", wb);
63 DB::writeString(msg.getSource(), wb);
64 writeCString(": ", wb);
65 DB::writeString(msg.getText(), wb);
66}
67
68void OwnPatternFormatter::format(const Poco::Message & msg, std::string & text)
69{
70 formatExtended(DB::ExtendedLogMessage::getFrom(msg), text);
71}
72