Flexible Logger

Logging is an extremely important tool when you are developing an application, or just monitoring an existing one. I wrote this logger tool to handle a variety of logging needs, from web based applications to daemons that run in PHP’s CLI mode.

The big advantage of using this logger is that if you need to change your logging in an application from, say, logging to a text file to logging into a MySQL DB all you need to do is change the logger configuration.

Here’s an example of typical output in text form, with levels appropriately indented.

Output can be formatted in many ways, as plain text, HTML, XML, and written to STDOUT, a file or database. Output can be spawned in realtime or buffered and output at the end of execution, depending on the type of output. Some output methods (e.g. syslog) are not suitable for buffered output while others (e.g. XML) are not suitable for realtime output. The following table shows available outputs and their restrictions:

Text buffered or non-buffered
zTemplate buffered or non-buffered
HTML buffered only
XML buffered only
SysLog non-buffered only
DB non-buffered only

Usage

Configuration

The constructor of the logger can be passed either a string specifying the output method or an array of configuration items like so:

Logging

To log you call the log() method with the logging level and message. You can also use methods fatal(), warning(), info(), and debug() which are defined as levels 1 to 4. Level 0 is used for messages from the logger itself (start and exit).

Defining your own levels

You can define your own labels for the levels by defining constants, like so:

Breaking on Error

In the normal course of debugging you probably want PHP execution to halt if something goes wrong with the logger, for example if logging to a filesystem which becomes full. However this behaviour is not desirable if the logger is used on a live application. To turn off break on error set the breakOnError property of the logger to false, either by passing it in the config or setting the property manually.

Creating your own plugin

Creating a plugin is easy:

1) Create a new class that extends the \opensourceame\loggerPlugin class.
2) Create the method that generates the log entry (called log())
3) Create methods to run when the log is started and stopped (these are optional)

Here is an example, which generates HTML logs as an unordered list. This is not meant as a practical example (since unordered lists aren’t suited to logs), but as an illustration of how the plugin works:

Options

Options for each plugin are given in the tables below:

Common

These options are available to all plugins:

logStartStop add log entries for the start and stop of the logger
showElapsedTime Log the time elapsed since the previous log entry
useLabels show a label such as “warning” in the log entry
elapsedTimeFormat The sprintf() format to use when printing the elapsed time

Text

logFile The name of a file to log to
indentText The text to use for indenting levels. Defaults to two spaces.
prefix A prefix to show on each log line (see the example at the top of the page)
dateFormat The format for the date, in standard PHP date() form. Defaults to “Y-m-d H:i:s”

zTemplate

templateFile The template file to use (e.g. ‘/path/mytemplate.tpl’)
mainBlock The main block in the template (defaults to “logger”)
levelBlocks An array mapping levels to blocks to parse in the template (defaults to each level mapped to “log”)

SysLog

indentText The text to use for indenting levels. Defaults to two spaces.
facility The syslog facility (auth, mail, user, etc.) Defaults to LOG_USER
priority The syslog priority for messages. Defaults to LOG_INFO
logPID Whether to log the process ID with every entry. Defaults to false.
prefix The prefix to use in log lines)

HTML

dateFormat The format for the date, in standard PHP date() form. Defaults to “Y-m-d H:i:s”
rowsStyled Whether to apply style attributes to row tags
rowsStyles An array containing the levels as keys, and style data as values

XML

dateFormat The format for the date, in standard PHP date() form. Defaults to “Y-m-d H:i:s”
useAttributes Embed date and level information in attributes rather than tags. Defaults to false.

Database

dsn Database Service Name
username Username to connect to the db
password Password to connect to the db
table The table into which logs are written
fields An array of fields that maps data to the db fields

Download

Download the class and example files here

Full API documentation for the logger is available in the API Docs

Note: the logger is written for PHP 5.3 as it incorporates namespaces and some 5.3-only functions.

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes:

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code class="" title="" data-url=""> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong> <pre class="" title="" data-url=""> <span class="" title="" data-url="">