diff --git a/libs/libloggers/loggers/Loggers.cpp b/libs/libloggers/loggers/Loggers.cpp index 0fdaa766838..e51df91de19 100644 --- a/libs/libloggers/loggers/Loggers.cpp +++ b/libs/libloggers/loggers/Loggers.cpp @@ -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 log = new DB::OwnFormattingChannel(new OwnPatternFormatter(this), new Poco::ConsoleChannel); + Poco::AutoPtr pf = new OwnPatternFormatter(this, OwnPatternFormatter::ADD_NOTHING, is_tty); + Poco::AutoPtr log = new DB::OwnFormattingChannel(pf, new Poco::ConsoleChannel); logger.warning("Logging " + log_level + " to console"); split->addChannel(log); } diff --git a/libs/libloggers/loggers/OwnPatternFormatter.cpp b/libs/libloggers/loggers/OwnPatternFormatter.cpp index c049fa90485..2b6847648b7 100644 --- a/libs/libloggers/loggers/OwnPatternFormatter.cpp +++ b/libs/libloggers/loggers/OwnPatternFormatter.cpp @@ -4,6 +4,7 @@ #include #include #include +#include #include #include #include @@ -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()(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(msg.getPriority())), wb); + int priority = static_cast(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); diff --git a/libs/libloggers/loggers/OwnPatternFormatter.h b/libs/libloggers/loggers/OwnPatternFormatter.h index dc1254cba29..4aedcc04637 100644 --- a/libs/libloggers/loggers/OwnPatternFormatter.h +++ b/libs/libloggers/loggers/OwnPatternFormatter.h @@ -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; };