FastSitePHP\Data\Log\FileLogger

Simple File Logger that uses the [Psr\Log\LoggerInterface]

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
file_path string null Path of the log file. The file will be created on first used if it does not exist.
log_format string "{date} {level} - {message}{line_break}" Default Log file format
line_break string PHP_EOL Line breaks default to the line breaks for the OS:
    "\r\n" - Windows
    "\n"   - Other OS's
date_format string \DateTime::ISO8601 Format to use when converting dates to a string

Methods

__construct($file_path)

Class Constructor. The file path to write to is set when this the object is created.

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.