I have a custom C module for Apache 2.4 (on Windows, though I hope that is not relevant). within the module we write out errors and the like with ap_log_perror. all is well, we get the error messages to the log file. recently, I tried to create a custom format to the error log file using the ErrorLogFormat directive, e.g.:
# timestamp (pid:tid) [level] [module] {client} (errorcode) message
ErrorLogFormat "%{u}t (%P:%T) [%l] [%m] {%a} (%E) %M"
for all standard Apache modules, the format change works nicely, e.g.:
Thu Jul 16 17:45:28.521107 2015 (1912:1504) [info] [ssl] {127.0.0.1:52505} ((OS 10060)A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond. ) AH01991: SSL input filter read failed.
Thu Jul 16 17:45:28.521107 2015 (1912:1504) [debug] [ssl] {127.0.0.1:52505} AH02001: Connection closed to child 98 with standard shutdown (server myhost.meditech.com:443)
for my custom module, however, the format is the default:
[Thu Jul 16 17:45:28.821350 2015] [my_mod:notice] [pid 1912:tid 1488] onMessage:begin
I've followed the path through Apache's source and found that the main logging facility will kick in, if we can find the log format (and it's not a startup message):
if (log_format && !info.startup) {
len += do_errorlog_format(log_format, &info, errstr + len,
MAX_STRING_LEN - len,
&errstr_start, &errstr_end, fmt, args);
}
and furthermore, log_format is set as such:
if (s->module_config) {
sconf = ap_get_core_module_config(s->module_config);
...
log_format = sconf ? sconf->error_log_format : NULL;
...
I cannot compile Apache (having trouble doing so), but I did throw some logic into my module to pull in the s->module_cong->error_log_format and verified that it's the custom list of tokens, etc.
I took a look at a couple standard modules and played around with defining merged configs and such (before I learned what they're really used for) but no dice.
any thoughts? I realize there are other workarounds like piping the log into to a program, etc. but I should be able to have my module behave like standard ones in this regard.
thanks
p.s. in case it's interesting, a typical call to log from my mod looks like:
request_rec *r = server->request(server);
...
ap_log_perror(APLOG_MARK,APLOG_NOTICE,0,r->pool,"onMessage:begin");
Don't use ap_log_perror(), it has almost no context to apply any per request or server config. Use ap_log_rerror when you have a request_rec* in your context (which you are dereferencing anyway).
Generally, use ap_log_rerror when you have a request_rec, and ap_log_cerror when you have a conn_rec. Then ap_log_error with a server_rec or "ap_server_conf".