Logging in PHP

Here’s the translation of the Go logging example to PHP, formatted in Markdown suitable for Hugo:

Our example demonstrates various logging techniques in PHP. We’ll use the built-in error_log() function and create custom logging functions to showcase different logging capabilities.

<?php

// Simply using error_log() writes to the default error log
error_log("standard logger");

// We can set the error_log configuration to specify a custom log file
ini_set("error_log", "app.log");
error_log("logged to app.log");

// Custom function to log with microsecond precision
function log_with_micro($message) {
    $date = new DateTime();
    error_log($date->format('Y-m-d H:i:s.u') . ' ' . $message);
}

log_with_micro("with micro");

// Custom function to log with file and line information
function log_with_file_line($message) {
    $bt = debug_backtrace();
    $caller = array_shift($bt);
    error_log(basename($caller['file']) . ':' . $caller['line'] . ' ' . $message);
}

log_with_file_line("with file/line");

// Custom logger class with prefix
class MyLogger {
    private $prefix;

    public function __construct($prefix) {
        $this->prefix = $prefix;
    }

    public function log($message) {
        error_log($this->prefix . ' ' . $message);
    }

    public function setPrefix($prefix) {
        $this->prefix = $prefix;
    }
}

$mylog = new MyLogger("my:");
$mylog->log("from mylog");

$mylog->setPrefix("ohmy:");
$mylog->log("from mylog");

// Logging to a custom output (in this case, a string)
$buffer = '';
function buffer_log($message) {
    global $buffer;
    $buffer .= date('Y-m-d H:i:s') . ' ' . $message . "\n";
}

buffer_log("hello");
echo "from buflog:" . $buffer;

// Structured logging in JSON format
function json_log($message, $data = []) {
    $log_entry = [
        'time' => (new DateTime())->format('c'),
        'level' => 'INFO',
        'msg' => $message
    ];
    $log_entry = array_merge($log_entry, $data);
    error_log(json_encode($log_entry));
}

json_log("hi there");
json_log("hello again", ['key' => 'val', 'age' => 25]);

This PHP code demonstrates various logging techniques:

  1. Using the built-in error_log() function for basic logging.
  2. Configuring a custom log file using ini_set().
  3. Creating a custom function to log with microsecond precision.
  4. Implementing a function to log with file and line information.
  5. Defining a custom logger class with a configurable prefix.
  6. Logging to a custom output (in this case, a string buffer).
  7. Implementing structured logging in JSON format.

To run this script, save it as logging.php and execute it using the PHP CLI:

$ php logging.php

Sample output (the actual date and time will depend on when you run the script):

from buflog:2023-08-22 10:45:16 hello

{"time":"2023-08-22T10:45:16-07:00","level":"INFO","msg":"hi there"}
{"time":"2023-08-22T10:45:16-07:00","level":"INFO","msg":"hello again","key":"val","age":25}

Note that the other log messages will be written to the configured log files or the default error log, depending on your PHP configuration.

This example showcases various logging techniques in PHP, allowing you to choose the most appropriate method for your application’s needs.