Logging

Phalcon\Logger is a component whose purpose is to provide logging services for applications. It offers logging to different backends using different adapters. It also offers transaction logging, configuration options, different formats and filters. You can use the Phalcon\Logger for every logging need your application has, from debugging processes to tracing application flow.

Adapters

This component makes use of adapters to store the logged messages. The use of adapters allows for a common logging interface which provides the ability to easily switch backends if necessary. The adapters supported are:

AdapterDescription
Phalcon\Logger\Adapter\FileLogs to a plain text file
Phalcon\Logger\Adapter\StreamLogs to a PHP Streams
Phalcon\Logger\Adapter\SyslogLogs to the system logger
Phalcon\Logger\Adapter\FirePHPLogs to the FirePHP

Factory

Loads Logger Adapter class using adapter option

  1. <?php
  2. use Phalcon\Logger\Factory;
  3. $options = [
  4. 'name' => 'log.txt',
  5. 'adapter' => 'file',
  6. ];
  7. $logger = Factory::load($options);

Creating a Log

The example below shows how to create a log and add messages to it:

  1. <?php
  2. use Phalcon\Logger;
  3. use Phalcon\Logger\Adapter\File as FileAdapter;
  4. $logger = new FileAdapter('app/logs/test.log');
  5. // These are the different log levels available:
  6. $logger->critical(
  7. 'This is a critical message'
  8. );
  9. $logger->emergency(
  10. 'This is an emergency message'
  11. );
  12. $logger->debug(
  13. 'This is a debug message'
  14. );
  15. $logger->error(
  16. 'This is an error message'
  17. );
  18. $logger->info(
  19. 'This is an info message'
  20. );
  21. $logger->notice(
  22. 'This is a notice message'
  23. );
  24. $logger->warning(
  25. 'This is a warning message'
  26. );
  27. $logger->alert(
  28. 'This is an alert message'
  29. );
  30. // You can also use the log() method with a Logger constant:
  31. $logger->log(
  32. 'This is another error message',
  33. Logger::ERROR
  34. );
  35. // If no constant is given, DEBUG is assumed.
  36. $logger->log(
  37. 'This is a message'
  38. );
  39. // You can also pass context parameters like this
  40. $logger->log(
  41. 'This is a {message}',
  42. [
  43. 'message' => 'parameter'
  44. ]
  45. );

The log generated is below:

  1. [Tue, 28 Jul 15 22:09:02 -0500][CRITICAL] This is a critical message
  2. [Tue, 28 Jul 15 22:09:02 -0500][EMERGENCY] This is an emergency message
  3. [Tue, 28 Jul 15 22:09:02 -0500][DEBUG] This is a debug message
  4. [Tue, 28 Jul 15 22:09:02 -0500][ERROR] This is an error message
  5. [Tue, 28 Jul 15 22:09:02 -0500][INFO] This is an info message
  6. [Tue, 28 Jul 15 22:09:02 -0500][NOTICE] This is a notice message
  7. [Tue, 28 Jul 15 22:09:02 -0500][WARNING] This is a warning message
  8. [Tue, 28 Jul 15 22:09:02 -0500][ALERT] This is an alert message
  9. [Tue, 28 Jul 15 22:09:02 -0500][ERROR] This is another error message
  10. [Tue, 28 Jul 15 22:09:02 -0500][DEBUG] This is a message
  11. [Tue, 28 Jul 15 22:09:02 -0500][DEBUG] This is a parameter

You can also set a log level using the setLogLevel() method. This method takes a Logger constant and will only save log messages that are as important or more important than the constant:

  1. <?php
  2. use Phalcon\Logger;
  3. use Phalcon\Logger\Adapter\File as FileAdapter;
  4. $logger = new FileAdapter('app/logs/test.log');
  5. $logger->setLogLevel(
  6. Logger::CRITICAL
  7. );

In the example above, only critical and emergency messages will get saved to the log. By default, everything is saved.

Transactions

Logging data to an adapter i.e. File (file system) is always an expensive operation in terms of performance. To combat that, you can take advantage of logging transactions. Transactions store log data temporarily in memory and later on write the data to the relevant adapter (File in this case) in a single atomic operation.

  1. <?php
  2. use Phalcon\Logger\Adapter\File as FileAdapter;
  3. // Create the logger
  4. $logger = new FileAdapter('app/logs/test.log');
  5. // Start a transaction
  6. $logger->begin();
  7. // Add messages
  8. $logger->alert(
  9. 'This is an alert'
  10. );
  11. $logger->error(
  12. 'This is another error'
  13. );
  14. // Commit messages to file
  15. $logger->commit();

Logging to Multiple Handlers

Phalcon\Logger can send messages to multiple handlers with a just single call:

  1. <?php
  2. use Phalcon\Logger;
  3. use Phalcon\Logger\Multiple as MultipleStream;
  4. use Phalcon\Logger\Adapter\File as FileAdapter;
  5. use Phalcon\Logger\Adapter\Stream as StreamAdapter;
  6. $logger = new MultipleStream();
  7. $logger->push(
  8. new FileAdapter('test.log')
  9. );
  10. $logger->push(
  11. new StreamAdapter('php://stdout')
  12. );
  13. $logger->log(
  14. 'This is a message'
  15. );
  16. $logger->log(
  17. 'This is an error',
  18. Logger::ERROR
  19. );
  20. $logger->error(
  21. 'This is another error'
  22. );

The messages are sent to the handlers in the order they were registered.

Message Formatting

This component makes use of formatters to format messages before sending them to the backend. The formatters available are:

AdapterDescription
Phalcon\Logger\Formatter\LineFormats the messages using a one-line string
Phalcon\Logger\Formatter\FirephpFormats the messages so that they can be sent to FirePHP
Phalcon\Logger\Formatter\JsonPrepares a message to be encoded with JSON
Phalcon\Logger\Formatter\SyslogPrepares a message to be sent to syslog

Line Formatter

Formats the messages using a one-line string. The default logging format is:

  1. [%date%][%type%] %message%

You can change the default format using setFormat(), this allows you to change the format of the logged messages by defining your own. The log format variables allowed are:

VariableDescription
%message%The message itself expected to be logged
%date%Date the message was added
%type%Uppercase string with message type

The example below shows how to change the log format:

  1. <?php
  2. use Phalcon\Logger\Formatter\Line as LineFormatter;
  3. $formatter = new LineFormatter('%date% - %message%');
  4. // Changing the logger format
  5. $logger->setFormatter($formatter);

Implementing your own formatters

The Phalcon\Logger\FormatterInterface interface must be implemented in order to create your own logger formatter or extend the existing ones.

Adapters

The following examples show the basic use of each adapter:

Stream Logger

The stream logger writes messages to a valid registered stream in PHP. A list of streams is available here:

  1. <?php
  2. use Phalcon\Logger\Adapter\Stream as StreamAdapter;
  3. // Opens a stream using zlib compression
  4. $logger = new StreamAdapter('compress.zlib://week.log.gz');
  5. // Writes the logs to stderr
  6. $logger = new StreamAdapter('php://stderr');

File Logger

This logger uses plain files to log any kind of data. By default all logger files are opened using append mode which opens the files for writing only; placing the file pointer at the end of the file. If the file does not exist, an attempt will be made to create it. You can change this mode by passing additional options to the constructor:

  1. <?php
  2. use Phalcon\Logger\Adapter\File as FileAdapter;
  3. // Create the file logger in 'w' mode
  4. $logger = new FileAdapter(
  5. 'app/logs/test.log',
  6. [
  7. 'mode' => 'w',
  8. ]
  9. );

Syslog Logger

This logger sends messages to the system logger. The syslog behavior may vary from one operating system to another.

  1. <?php
  2. use Phalcon\Logger\Adapter\Syslog as SyslogAdapter;
  3. // Basic Usage
  4. $logger = new SyslogAdapter(null);
  5. // Setting ident/mode/facility
  6. $logger = new SyslogAdapter(
  7. 'ident-name',
  8. [
  9. 'option' => LOG_NDELAY,
  10. 'facility' => LOG_MAIL,
  11. ]
  12. );

FirePHP Logger

This logger sends messages in HTTP response headers that are displayed by FirePHP, a Firebug extension for Firefox.

  1. <?php
  2. use Phalcon\Logger;
  3. use Phalcon\Logger\Adapter\Firephp as Firephp;
  4. $logger = new Firephp('');
  5. $logger->log(
  6. 'This is a message'
  7. );
  8. $logger->log(
  9. 'This is an error',
  10. Logger::ERROR
  11. );
  12. $logger->error(
  13. 'This is another error'
  14. );

Implementing your own adapters

The Phalcon\Logger\AdapterInterface interface must be implemented in order to create your own logger adapters or extend the existing ones.