0

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 the error_log() function, which defines the file which errors are logged to

    an 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() and error_log() function which defers messages to either the PHP system logger (/var/log/syslog) or the SAPI logger respectively (see this post and notice on error_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.

Community
  • 1
  • 1
coderatchet
  • 8,120
  • 17
  • 69
  • 125
  • Look at this for error_log http://php.net/manual/en/function.set-error-handler.php and this for exceptions http://php.net/manual/en/function.set-exception-handler.php – Yolo Mar 10 '17 at 03:06
  • I prefer to keep my applicative logs away from the standard ones. For this, i normally compose-in apache/log4php. There are plenty examples in the distribution for you to adjust 'where' to log, log what, at what level. ps. Autoloader is your friend. – YvesLeBorg Mar 10 '17 at 03:18
  • 1
    @Yolo, from this it seems as though I'm being told I have to define an error handler which detects the log_level and trigger my own errors, This seems like a fairly strange and cumbersome procedure. besides, It doesn't seem to have a DEBUG option in the defined constants. – coderatchet Mar 10 '17 at 03:21
  • @YvesLeBorg I was hoping to have some functionality in the core library, I'm not opposed to Composer or Autoloader, however my knowledge of these are little and we are currently running solely off core PHP. I suppose I'm asking if this is a function that everyone has either had to hand carve or include as a third-party library. – coderatchet Mar 10 '17 at 03:23
  • @thenaglecode : well, your call ! I generally work close to php-core myself, bloated frameworks abound. However, there are some staple components i dont mind composing into my projects, log4php is one of them, same as i npm-in log4js in my node projects, and yes, log4j in my cobol projects. – YvesLeBorg Mar 10 '17 at 03:27
  • 1
    Welp, looks like I'm minusing a point in my PHP tally for logging functionality/practicality. better start learning how to use composer. Hope it's not as verbose as Maven. :P – coderatchet Mar 10 '17 at 03:32
  • @thenaglecode hahaha , nothing could. Dead simple, easy to integrate to your deployment pipeline. – YvesLeBorg Mar 10 '17 at 03:39

0 Answers0