Is it just me or is do others have trouble understanding how logging works in PHP?
I come from a Java/Python logging world where you usually have access to an inbuilt utility for logging that allows you to either specify a log level or call a log level function (like logger.info('foo');
). The core library would then organise from configuration values how the log appears and in what file it ends up in.
However out of the many documents I've read and google searches done on the subject, I seem to be missing some sort of fundamental understanding of the way PHP logging works.
what I've found so far is that PHP has:
- the
log_errors=[0|1]
ini config directive, which enables logging of errors (if you would believe it), - the
error_log=/path/to/file
, not to be confused with theerror_log()
function, which defines the file which errors are logged toan additional note on the error_log value, I have found that in apache2, when you set this value as an absolute path, Apache will honor the value, however any other relative path will default to the ErrorLog value of the Apache server (usually
/var/log/[httpd|apache2|apache]/error.log
) - a
syslog()
anderror_log()
function which defers messages to either the PHP system logger (/var/log/syslog
) or the SAPI logger respectively (see this post and notice onerror_log
).
I have no control from the method signature what level message I have logged. (I know there are different levels as I can see when I receive an Illegal string offset
Warning or Undefined variable
notice).
# php internal log examples
[10-Mar-2017 02:45:17 UTC] PHP Warning: Illegal string offset 'username' in /var/www/html/foobar.php on line 123
[10-Mar-2017 02:45:17 UTC] PHP Notice: Undefined variable: col_info in /var/www/html/barfoo.php on line 89
[10-Mar-2017 02:45:17 UTC] my message using error_log() function without any level indication.
Now I realise that I may output the level myself, but am I to believe I cannot integrate with PHP's own level system for the sake of easy config? I choose to believe otherwise.
All my searches thus far have only informed me how to disable or enable these levels and not how to output a message with a given level.
I am hoping to be directed to some fundamental documentation on the reasoning behind logging in PHP... Or I may eat my shoe out of frustration from this simple endeavour.
P.S. I am using Apache2 as my web server.
Update:
I can see that you may log to syslog with syslog($ERRNO, $ERRMSG)
and this sets the log_level (LOG_DEBUG
, LOG_INFO
, etc...). It would be nice to hijack the log_level configuration without using the implicit syslog file. i.e. my_error.log
as opposed to syslog
. I'm beginning to believe that log levels are internal ideas in the PHP core.