mirror of
https://github.com/ClickHouse/ClickHouse.git
synced 2024-11-09 17:14:47 +00:00
Add OwnFilteringChannel[.h, .cpp]
This commit is contained in:
parent
56b39c37a1
commit
25475ceda2
55
src/Loggers/OwnFilteringChannel.cpp
Normal file
55
src/Loggers/OwnFilteringChannel.cpp
Normal file
@ -0,0 +1,55 @@
|
||||
#include "OwnFilteringChannel.h"
|
||||
#include <Poco/RegularExpression.h>
|
||||
// #include <iostream> // 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;
|
||||
}
|
||||
|
||||
}
|
61
src/Loggers/OwnFilteringChannel.h
Normal file
61
src/Loggers/OwnFilteringChannel.h
Normal file
@ -0,0 +1,61 @@
|
||||
#pragma once
|
||||
#include <Poco/AutoPtr.h>
|
||||
#include <Poco/Channel.h>
|
||||
#include <Poco/Message.h>
|
||||
#include <Poco/Util/AbstractConfiguration.h>
|
||||
#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<Poco::Channel> pChannel_, Poco::AutoPtr<OwnPatternFormatter> 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<Poco::Channel> pChannel;
|
||||
Poco::AutoPtr<OwnPatternFormatter> pFormatter;
|
||||
};
|
||||
|
||||
}
|
Loading…
Reference in New Issue
Block a user