ClickHouse

Форк
0
/
OwnJSONPatternFormatter.cpp 
197 строк · 6.0 Кб
1
#include "OwnJSONPatternFormatter.h"
2

3
#include <functional>
4
#include <IO/WriteBufferFromString.h>
5
#include <IO/WriteHelpers.h>
6
#include <Interpreters/InternalTextLogsQueue.h>
7
#include <base/terminalColors.h>
8
#include <Common/CurrentThread.h>
9
#include <Common/HashTable/Hash.h>
10

11
OwnJSONPatternFormatter::OwnJSONPatternFormatter(Poco::Util::AbstractConfiguration & config)
12
{
13
    if (config.has("logger.formatting.names.date_time"))
14
        date_time = config.getString("logger.formatting.names.date_time", "");
15

16
    if (config.has("logger.formatting.names.thread_name"))
17
        thread_name = config.getString("logger.formatting.names.thread_name", "");
18

19
    if (config.has("logger.formatting.names.thread_id"))
20
        thread_id = config.getString("logger.formatting.names.thread_id", "");
21

22
    if (config.has("logger.formatting.names.level"))
23
        level = config.getString("logger.formatting.names.level", "");
24

25
    if (config.has("logger.formatting.names.query_id"))
26
        query_id = config.getString("logger.formatting.names.query_id", "");
27

28
    if (config.has("logger.formatting.names.logger_name"))
29
        logger_name = config.getString("logger.formatting.names.logger_name", "");
30

31
    if (config.has("logger.formatting.names.message"))
32
        message = config.getString("logger.formatting.names.message", "");
33

34
    if (config.has("logger.formatting.names.source_file"))
35
        source_file = config.getString("logger.formatting.names.source_file", "");
36

37
    if (config.has("logger.formatting.names.source_line"))
38
        source_line = config.getString("logger.formatting.names.source_line", "");
39

40
    if (date_time.empty() && thread_name.empty() && thread_id.empty() && level.empty() && query_id.empty()
41
        && logger_name.empty() && message.empty() && source_file.empty() && source_line.empty())
42
    {
43
        date_time = "date_time";
44
        thread_name = "thread_name";
45
        thread_id = "thread_id";
46
        level = "level";
47
        query_id = "query_id";
48
        logger_name = "logger_name";
49
        message = "message";
50
        source_file = "source_file";
51
        source_line = "source_line";
52
    }
53
}
54

55
void OwnJSONPatternFormatter::formatExtended(const DB::ExtendedLogMessage & msg_ext, std::string & text) const
56
{
57
    DB::WriteBufferFromString wb(text);
58

59
    DB::FormatSettings settings;
60
    bool print_comma = false;
61

62
    const Poco::Message & msg = msg_ext.base;
63
    DB::writeChar('{', wb);
64

65
    if (!date_time.empty())
66
    {
67
        writeJSONString(date_time, wb, settings);
68
        DB::writeChar(':', wb);
69

70
        DB::writeChar('\"', wb);
71
        /// Change delimiters in date for compatibility with old logs.
72
        writeDateTimeUnixTimestamp(msg_ext.time_seconds, 0, wb);
73
        DB::writeChar('.', wb);
74
        DB::writeChar('0' + ((msg_ext.time_microseconds / 100000) % 10), wb);
75
        DB::writeChar('0' + ((msg_ext.time_microseconds / 10000) % 10), wb);
76
        DB::writeChar('0' + ((msg_ext.time_microseconds / 1000) % 10), wb);
77
        DB::writeChar('0' + ((msg_ext.time_microseconds / 100) % 10), wb);
78
        DB::writeChar('0' + ((msg_ext.time_microseconds / 10) % 10), wb);
79
        DB::writeChar('0' + ((msg_ext.time_microseconds / 1) % 10), wb);
80
        DB::writeChar('\"', wb);
81
        print_comma = true;
82
    }
83

84
    if (!thread_name.empty())
85
    {
86
        if (print_comma)
87
            DB::writeChar(',', wb);
88
        else
89
            print_comma = true;
90

91
        writeJSONString(thread_name, wb, settings);
92
        DB::writeChar(':', wb);
93

94
        writeJSONString(msg.getThread(), wb, settings);
95
    }
96

97
    if (!thread_id.empty())
98
    {
99
        if (print_comma)
100
            DB::writeChar(',', wb);
101
        else
102
            print_comma = true;
103

104
        writeJSONString(thread_id, wb, settings);
105
        DB::writeChar(':', wb);
106
        DB::writeChar('\"', wb);
107
        DB::writeIntText(msg_ext.thread_id, wb);
108
        DB::writeChar('\"', wb);
109
    }
110

111
    if (!level.empty())
112
    {
113
        if (print_comma)
114
            DB::writeChar(',', wb);
115
        else
116
            print_comma = true;
117

118
        writeJSONString(level, wb, settings);
119
        DB::writeChar(':', wb);
120
        int priority = static_cast<int>(msg.getPriority());
121
        writeJSONString(getPriorityName(priority), wb, settings);
122
    }
123

124
    if (!query_id.empty())
125
    {
126
        if (print_comma)
127
            DB::writeChar(',', wb);
128
        else
129
            print_comma = true;
130

131
        /// We write query_id even in case when it is empty (no query context)
132
        /// just to be convenient for various log parsers.
133

134
        writeJSONString(query_id, wb, settings);
135
        DB::writeChar(':', wb);
136
        writeJSONString(msg_ext.query_id, wb, settings);
137
    }
138

139
    if (!logger_name.empty())
140
    {
141
        if (print_comma)
142
            DB::writeChar(',', wb);
143
        else
144
            print_comma = true;
145

146
        writeJSONString(logger_name, wb, settings);
147
        DB::writeChar(':', wb);
148

149
        writeJSONString(msg.getSource(), wb, settings);
150
    }
151

152
    if (!message.empty())
153
    {
154
        if (print_comma)
155
            DB::writeChar(',', wb);
156
        else
157
            print_comma = true;
158

159
        writeJSONString(message, wb, settings);
160
        DB::writeChar(':', wb);
161
        writeJSONString(msg.getText(), wb, settings);
162
    }
163

164
    if (!source_file.empty())
165
    {
166
        if (print_comma)
167
            DB::writeChar(',', wb);
168
        else
169
            print_comma = true;
170

171
        writeJSONString(source_file, wb, settings);
172
        DB::writeChar(':', wb);
173
        const char * source_file_name = msg.getSourceFile();
174
        if (source_file_name != nullptr)
175
            writeJSONString(source_file_name, wb, settings);
176
        else
177
            writeJSONString("", wb, settings);
178
    }
179

180
    if (!source_line.empty())
181
    {
182
        if (print_comma)
183
            DB::writeChar(',', wb);
184

185
        writeJSONString(source_line, wb, settings);
186
        DB::writeChar(':', wb);
187
        DB::writeChar('\"', wb);
188
        DB::writeIntText(msg.getSourceLine(), wb);
189
        DB::writeChar('\"', wb);
190
    }
191
    DB::writeChar('}', wb);
192
}
193

194
void OwnJSONPatternFormatter::format(const Poco::Message & msg, std::string & text)
195
{
196
    formatExtended(DB::ExtendedLogMessage::getFrom(msg), text);
197
}
198

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.