Skip to content

Commit a32dc67

Browse files
authored
Added path_exempt in AccessLogger plugin config to exclude desired path from logging (#2258)
1 parent e155df9 commit a32dc67

File tree

2 files changed

+58
-1
lines changed

2 files changed

+58
-1
lines changed

lib/inc/drogon/plugins/AccessLogger.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ namespace plugin
3636
// "show_microseconds": true,
3737
// "custom_time_format": "",
3838
// "use_real_ip": false
39+
// "path_exempt": ""
3940
}
4041
}
4142
@endcode
@@ -98,6 +99,10 @@ namespace plugin
9899
* Enable the plugin by adding the configuration to the list of plugins in the
99100
* configuration file.
100101
*
102+
* path_exempt: must be a string or a string array, present a regular expression
103+
* (for matching the path of a request) or a regular expression list for URLs
104+
* that don't have to be logged.
105+
*
101106
*/
102107
class DROGON_EXPORT AccessLogger : public drogon::Plugin<AccessLogger>
103108
{
@@ -117,6 +122,8 @@ class DROGON_EXPORT AccessLogger : public drogon::Plugin<AccessLogger>
117122
bool useCustomTimeFormat_{false};
118123
std::string timeFormat_;
119124
static bool useRealIp_;
125+
std::regex exemptRegex_;
126+
bool regexFlag_{false};
120127

121128
using LogFunction = std::function<void(trantor::LogStream &,
122129
const drogon::HttpRequestPtr &,

lib/src/AccessLogger.cc

Lines changed: 51 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,46 @@ void AccessLogger::initAndStart(const Json::Value &config)
113113
}
114114
createLogFunctions(format);
115115
auto logPath = config.get("log_path", "").asString();
116+
117+
if (config.isMember("path_exempt"))
118+
{
119+
if (config["path_exempt"].isArray())
120+
{
121+
const auto &exempts = config["path_exempt"];
122+
size_t exemptsCount = exempts.size();
123+
if (exemptsCount)
124+
{
125+
std::string regexString;
126+
size_t len = 0;
127+
for (const auto &exempt : exempts)
128+
{
129+
assert(exempt.isString());
130+
len += exempt.size();
131+
}
132+
regexString.reserve((exemptsCount * (1 + 2)) - 1 + len);
133+
134+
const auto last = --exempts.end();
135+
for (auto exempt = exempts.begin(); exempt != last; ++exempt)
136+
regexString.append("(")
137+
.append(exempt->asString())
138+
.append(")|");
139+
regexString.append("(").append(last->asString()).append(")");
140+
141+
exemptRegex_ = std::regex(regexString);
142+
regexFlag_ = true;
143+
}
144+
}
145+
else if (config["path_exempt"].isString())
146+
{
147+
exemptRegex_ = std::regex(config["path_exempt"].asString());
148+
regexFlag_ = true;
149+
}
150+
else
151+
{
152+
LOG_ERROR << "path_exempt must be a string or string array!";
153+
}
154+
}
155+
116156
#ifdef DROGON_SPDLOG_SUPPORT
117157
auto logWithSpdlog = trantor::Logger::hasSpdLogSupport() &&
118158
config.get("use_spdlog", false).asBool();
@@ -228,7 +268,17 @@ void AccessLogger::initAndStart(const Json::Value &config)
228268
drogon::app().registerPreSendingAdvice(
229269
[this](const drogon::HttpRequestPtr &req,
230270
const drogon::HttpResponsePtr &resp) {
231-
logging(LOG_RAW_TO(logIndex_), req, resp);
271+
if (regexFlag_)
272+
{
273+
if (!std::regex_match(req->path(), exemptRegex_))
274+
{
275+
logging(LOG_RAW_TO(logIndex_), req, resp);
276+
}
277+
}
278+
else
279+
{
280+
logging(LOG_RAW_TO(logIndex_), req, resp);
281+
}
232282
});
233283
}
234284

0 commit comments

Comments
 (0)