Skip to content

Commit 031d6e0

Browse files
author
Stefan Fritsch
committed
Add support for conditional logging depending on an expression.
The syntax is a bit unwieldy, the quotes have to start before the 'expr=': CustomLog "logs/cond_log" combined "expr=req('User-Agent') == 'x'" git-svn-id: https://svn.apache.org/repos/asf/httpd/httpd/trunk@1033157 13f79535-47bb-0310-9956-ffa450edef68
1 parent ab4c6b3 commit 031d6e0

1 file changed

Lines changed: 29 additions & 7 deletions

File tree

modules/loggers/mod_log_config.c

Lines changed: 29 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -265,6 +265,7 @@ typedef struct {
265265
apr_array_header_t *format;
266266
void *log_writer;
267267
char *condition_var;
268+
ap_expr_info_t *condition_expr;
268269
} config_log_state;
269270

270271
/*
@@ -1081,6 +1082,15 @@ static int config_log_transaction(request_rec *r, config_log_state *cls,
10811082
}
10821083
}
10831084
}
1085+
else if (cls->condition_expr != NULL) {
1086+
const char *err;
1087+
int rc = ap_expr_exec(r, cls->condition_expr, &err);
1088+
if (rc < 0)
1089+
ap_log_rerror(APLOG_MARK, APLOG_WARNING, 0, r,
1090+
"Error evaluating log condition: %s", err);
1091+
if (rc <= 0)
1092+
return DECLINED;
1093+
}
10841094

10851095
format = cls->format ? cls->format : default_format;
10861096

@@ -1229,15 +1239,27 @@ static const char *add_custom_log(cmd_parms *cmd, void *dummy, const char *fn,
12291239

12301240
cls = (config_log_state *) apr_array_push(mls->config_logs);
12311241
cls->condition_var = NULL;
1242+
cls->condition_expr = NULL;
12321243
if (envclause != NULL) {
1233-
if (strncasecmp(envclause, "env=", 4) != 0) {
1234-
return "error in condition clause";
1244+
if (strncasecmp(envclause, "env=", 4) == 0) {
1245+
if ((envclause[4] == '\0')
1246+
|| ((envclause[4] == '!') && (envclause[5] == '\0'))) {
1247+
return "missing environment variable name";
1248+
}
1249+
cls->condition_var = apr_pstrdup(cmd->pool, &envclause[4]);
12351250
}
1236-
if ((envclause[4] == '\0')
1237-
|| ((envclause[4] == '!') && (envclause[5] == '\0'))) {
1238-
return "missing environment variable name";
1251+
else if (strncasecmp(envclause, "expr=", 5) == 0) {
1252+
const char *err;
1253+
if ((envclause[5] == '\0'))
1254+
return "missing condition";
1255+
cls->condition_expr = ap_expr_parse_cmd(cmd, &envclause[5], &err,
1256+
NULL);
1257+
if (err)
1258+
return err;
1259+
}
1260+
else {
1261+
return "error in condition clause";
12391262
}
1240-
cls->condition_var = apr_pstrdup(cmd->pool, &envclause[4]);
12411263
}
12421264

12431265
cls->fname = fn;
@@ -1277,7 +1299,7 @@ static const command_rec config_log_cmds[] =
12771299
{
12781300
AP_INIT_TAKE23("CustomLog", add_custom_log, NULL, RSRC_CONF,
12791301
"a file name, a custom log format string or format name, "
1280-
"and an optional \"env=\" clause (see docs)"),
1302+
"and an optional \"env=\" or \"expr=\" clause (see docs)"),
12811303
AP_INIT_TAKE1("TransferLog", set_transfer_log, NULL, RSRC_CONF,
12821304
"the filename of the access log"),
12831305
AP_INIT_TAKE12("LogFormat", log_format, NULL, RSRC_CONF,

0 commit comments

Comments
 (0)