FastSitePHP\Data\Log\HtmlLogger

HTML Logger that uses the [Psr\Log\LoggerInterface].

This class can be used for temporary development logs because it outputs an HTML table of logged messages after the response is sent or depending on options can be used to replace the original response.

Source Code

GitHub

Example Code

Logging

// FastSitePHP includes two logging classes that implement the widely used
// [Psr\Log] Interface.

// Create a file logger. Log messages are appended and the file is created
// when the first message is added.
$file = __DIR__ . '/log.txt';
$file_logger = new \FastSitePHP\Data\Log\FileLogger($file);

// Create an HTML Logger
// This class can be used for temporary development logs because it outputs an
// HTML table of logged messages after the response is sent or depending on
// options can be used to replace the original response. The parameter
// [$replace_response] is optional.
$replace_response = false;
$html_logger = new \FastSitePHP\Data\Log\HtmlLogger($app, $replace_response);

// Log messages using one of the following functions:
//     emergency(), alert(), critical(), error(),
//     warning(), notice(), info(), debug()
$file_logger->info('This is a Test.');
$html_logger->error('Application Test');

// Additionally data can be passed to the message with placeholders
$html_logger->info('User {name} created', [
    'name' => 'Admin'
]);

// The date format can be any valid value for the PHP function [date()].
// Default is [\DateTime::ISO8601].
$file_logger->date_format = 'Y-m-d H:i:s';

// For the file logger the output format can be controlled by properties.
//
// Default Format:
//     '{date} {level} - {message}{line_break}';
//
// Line Breaks default based on the OS:
//     "\r\n" - Windows
//     "\n"   - Other OS's
$file_logger->log_format = '[{level}] {message}{line_break}';
$file_logger->line_break = '^^';

// You can also customize the HTML Logger with your own template:
// $html_logger->template_file = 'YOUR_TEMPLATE.php';

Properties

Name Data Type Default Description
temlate_file string __DIR__ . '/../../Templates/html-template.php' File path for the PHP Template used to show the logs
date_format string \DateTime::ISO8601 Format to use when converting dates to a string

Methods

__construct(Application $app, $replace_response = false)

Class Constructor

The FastSitePHP Application must be passed when this class is created. Once called it adds either a [beforeSend()] or [after()] event based on the optional parameter [$replace_response].

getHtml()

Return HTML that will be used to show the logged messages. This function gets called to replace the current route or after the response is sent.

Returns: string

log($level, $message, array $context = array())

Logs with an arbitrary level.

emergency($message, array $context = array())

System is unusable.

alert($message, array $context = array())

Action must be taken immediately.

Example: Entire website down, database unavailable, etc. This should trigger the SMS alerts and wake you up.

critical($message, array $context = array())

Critical conditions.

Example: Application component unavailable, unexpected exception.

error($message, array $context = array())

Runtime errors that do not require immediate action but should typically be logged and monitored.

warning($message, array $context = array())

Exceptional occurrences that are not errors.

Example: Use of deprecated APIs, poor use of an API, undesirable things that are not necessarily wrong.

notice($message, array $context = array())

Normal but significant events.

info($message, array $context = array())

Interesting events.

Example: User logs in, SQL logs.

debug($message, array $context = array())

Detailed debug information.