diff --git a/src/Loggers/OwnFilteringChannel.cpp b/src/Loggers/OwnFilteringChannel.cpp new file mode 100644 index 00000000000..6f28341ce2c --- /dev/null +++ b/src/Loggers/OwnFilteringChannel.cpp @@ -0,0 +1,55 @@ +#include "OwnFilteringChannel.h" +#include +// #include // TODO + + +namespace DB +{ + +namespace ErrorCodes +{ + extern const int TYPE_MISMATCH; +} + +void OwnFilteringChannel::log(const Poco::Message & msg) +{ + std::string formatted_text; + + // Apply formatting to the text + if (pFormatter) + { + pFormatter->formatExtended(ExtendedLogMessage::getFrom(msg), formatted_text); + } + else { + formatted_text = msg.getText(); + } + if (!regexpFilteredOut(formatted_text)) + pChannel->log(msg); +} + +bool OwnFilteringChannel::regexpFilteredOut(std::string text) const +{ + if (positive_pattern != "") + { + Poco::RegularExpression positive_regexp(positive_pattern); + if (!positive_regexp.match(text)) + { + // std::cout << "Skipping Message: " << text << "| due to positive regexp: " << positive_pattern << std::endl; + return true; + } + } + + if (negative_pattern != "") + { + Poco::RegularExpression negative_regexp(negative_pattern); + if (negative_regexp.match(text)) + { + // std::cout << "Skipping Message: " << text << "| due to negative regexp: " << negative_pattern << std::endl; + return true; + } + } + // std::cout << "THE FOLLOWING MESSAGE PASSED using positive: " << positive_pattern << " and negative: " << negative_pattern << std::endl; + return false; +} + +} diff --git a/src/Loggers/OwnFilteringChannel.h b/src/Loggers/OwnFilteringChannel.h new file mode 100644 index 00000000000..8c7cc4fd829 --- /dev/null +++ b/src/Loggers/OwnFilteringChannel.h @@ -0,0 +1,61 @@ +#pragma once +#include +#include +#include +#include +#include "OwnPatternFormatter.h" + + +namespace DB +{ + +// Filters the logs based on regular expressions. Should be processed after formatting channel to read entire formatted text +class OwnFilteringChannel : public Poco::Channel +{ +public: + explicit OwnFilteringChannel(Poco::AutoPtr pChannel_, Poco::AutoPtr pf, + std::string positive_pattern_, std::string negative_pattern_) + : positive_pattern(positive_pattern_), negative_pattern(negative_pattern_), pChannel(pChannel_), pFormatter(pf) + { + } + + // Only log if pass both positive and negative regexp checks. + // Checks the regexps on the formatted text (without color), but then passes the raw text + // to the split channel to handle formatting for individual channels (e.g apply color) + void log(const Poco::Message & msg) override; + + // Sets the regex patterns to use for filtering. Specifying an empty string pattern "" indicates no filtering + void setRegexpPatterns(std::string positive_pattern_, std::string negative_pattern_) + { + positive_pattern = positive_pattern_; + negative_pattern = negative_pattern_; + } + + void open() override + { + if (pChannel) + pChannel->open(); + } + + void close() override + { + if (pChannel) + pChannel->close(); + } + + void setProperty(const std::string& name, const std::string& value) override + { + if (pChannel) + pChannel->setProperty(name, value); + } + +private: + bool regexpFilteredOut(std::string text) const; + + std::string positive_pattern; + std::string negative_pattern; + Poco::AutoPtr pChannel; + Poco::AutoPtr pFormatter; +}; + +}