@@ -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