mirror of
https://github.com/ClickHouse/ClickHouse.git
synced 2024-11-27 10:02:01 +00:00
Merge pull request #8961 from ClickHouse/color-log-in-console
If server is run from terminal, highlight thread number, query id and log priority by colors.
This commit is contained in:
commit
b3b40232ce
@ -133,10 +133,13 @@ void Loggers::buildLoggers(Poco::Util::AbstractConfiguration & config, Poco::Log
|
||||
split->addChannel(log);
|
||||
}
|
||||
|
||||
bool is_tty = isatty(STDIN_FILENO) || isatty(STDERR_FILENO);
|
||||
|
||||
if (config.getBool("logger.console", false)
|
||||
|| (!config.hasProperty("logger.console") && !is_daemon && (isatty(STDIN_FILENO) || isatty(STDERR_FILENO))))
|
||||
|| (!config.hasProperty("logger.console") && !is_daemon && is_tty))
|
||||
{
|
||||
Poco::AutoPtr<DB::OwnFormattingChannel> log = new DB::OwnFormattingChannel(new OwnPatternFormatter(this), new Poco::ConsoleChannel);
|
||||
Poco::AutoPtr<OwnPatternFormatter> pf = new OwnPatternFormatter(this, OwnPatternFormatter::ADD_NOTHING, is_tty);
|
||||
Poco::AutoPtr<DB::OwnFormattingChannel> log = new DB::OwnFormattingChannel(pf, new Poco::ConsoleChannel);
|
||||
logger.warning("Logging " + log_level + " to console");
|
||||
split->addChannel(log);
|
||||
}
|
||||
|
@ -4,6 +4,7 @@
|
||||
#include <optional>
|
||||
#include <IO/WriteBufferFromString.h>
|
||||
#include <IO/WriteHelpers.h>
|
||||
#include <Common/HashTable/Hash.h>
|
||||
#include <Interpreters/InternalTextLogsQueue.h>
|
||||
#include <sys/time.h>
|
||||
#include <Common/CurrentThread.h>
|
||||
@ -11,8 +12,64 @@
|
||||
#include "Loggers.h"
|
||||
|
||||
|
||||
OwnPatternFormatter::OwnPatternFormatter(const Loggers * loggers_, OwnPatternFormatter::Options options_)
|
||||
: Poco::PatternFormatter(""), loggers(loggers_), options(options_)
|
||||
static const char * setColor(UInt64 num)
|
||||
{
|
||||
/// ANSI escape sequences to set foreground font color in terminal.
|
||||
|
||||
static constexpr auto num_colors = 14;
|
||||
static const char * colors[num_colors] =
|
||||
{
|
||||
/// Black on black is meaningless
|
||||
"\033[0;31m",
|
||||
"\033[0;32m",
|
||||
"\033[0;33m",
|
||||
/// Low intense blue on black is too dark.
|
||||
"\033[0;35m",
|
||||
"\033[0;36m",
|
||||
"\033[0;37m",
|
||||
"\033[1;30m",
|
||||
"\033[1;31m",
|
||||
"\033[1;32m",
|
||||
"\033[1;33m",
|
||||
"\033[1;34m",
|
||||
"\033[1;35m",
|
||||
"\033[1;36m",
|
||||
"\033[1m", /// Not as white but just as high intense - for readability on white background.
|
||||
};
|
||||
|
||||
return colors[num % num_colors];
|
||||
}
|
||||
|
||||
static const char * setColorForLogPriority(int priority)
|
||||
{
|
||||
if (priority < 1 || priority > 8)
|
||||
return "";
|
||||
|
||||
static const char * colors[] =
|
||||
{
|
||||
"",
|
||||
"\033[1;41m", /// Fatal
|
||||
"\033[0;41m", /// Critical
|
||||
"\033[1;31m", /// Error
|
||||
"\033[0;31m", /// Warning
|
||||
"\033[0;33m", /// Notice
|
||||
"\033[1m", /// Information
|
||||
"", /// Debug
|
||||
"\033[1;30m", /// Trace
|
||||
};
|
||||
|
||||
return colors[priority];
|
||||
}
|
||||
|
||||
static const char * resetColor()
|
||||
{
|
||||
return "\033[0m";
|
||||
}
|
||||
|
||||
|
||||
|
||||
OwnPatternFormatter::OwnPatternFormatter(const Loggers * loggers_, OwnPatternFormatter::Options options_, bool color_)
|
||||
: Poco::PatternFormatter(""), loggers(loggers_), options(options_), color(color_)
|
||||
{
|
||||
}
|
||||
|
||||
@ -48,17 +105,30 @@ void OwnPatternFormatter::formatExtended(const DB::ExtendedLogMessage & msg_ext,
|
||||
DB::writeChar('0' + ((msg_ext.time_microseconds / 1) % 10), wb);
|
||||
|
||||
writeCString(" [ ", wb);
|
||||
if (color)
|
||||
writeCString(setColor(intHash64(msg_ext.thread_id)), wb);
|
||||
DB::writeIntText(msg_ext.thread_id, wb);
|
||||
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);
|
||||
if (color)
|
||||
writeCString(setColor(std::hash<std::string>()(msg_ext.query_id)), wb);
|
||||
DB::writeString(msg_ext.query_id, wb);
|
||||
if (color)
|
||||
writeCString(resetColor(), wb);
|
||||
writeCString("} ", wb);
|
||||
|
||||
writeCString("<", wb);
|
||||
DB::writeString(getPriorityName(static_cast<int>(msg.getPriority())), wb);
|
||||
int priority = static_cast<int>(msg.getPriority());
|
||||
if (color)
|
||||
writeCString(setColorForLogPriority(priority), wb);
|
||||
DB::writeString(getPriorityName(priority), wb);
|
||||
if (color)
|
||||
writeCString(resetColor(), wb);
|
||||
writeCString("> ", wb);
|
||||
DB::writeString(msg.getSource(), wb);
|
||||
writeCString(": ", wb);
|
||||
|
@ -31,7 +31,7 @@ public:
|
||||
ADD_LAYER_TAG = 1 << 0
|
||||
};
|
||||
|
||||
OwnPatternFormatter(const Loggers * loggers_, Options options_ = ADD_NOTHING);
|
||||
OwnPatternFormatter(const Loggers * loggers_, Options options_ = ADD_NOTHING, bool color_ = false);
|
||||
|
||||
void format(const Poco::Message & msg, std::string & text) override;
|
||||
void formatExtended(const DB::ExtendedLogMessage & msg_ext, std::string & text);
|
||||
@ -39,4 +39,5 @@ public:
|
||||
private:
|
||||
const Loggers * loggers;
|
||||
Options options;
|
||||
bool color;
|
||||
};
|
||||
|
Loading…
Reference in New Issue
Block a user