ErrorLog Adapter

This adapter uses the PHP error_log() function internally. This provides flexibility in the log target with a single adapter. For example, you can send the log to email, a text file, or PHP's system logger. See the PHP manual for more details.

The current message types are:

  • ErrorLog::TYPE_PHP_LOG; // equivalent to message type 0 in error_log()
  • ErrorLog::TYPE_EMAIL; // equivalent to message type 1 in error_log()
  • ErrorLog::TYPE_FILE; // equivalent to message type 3 in error_log()
  • ErrorLog::TYPE_SAPI_LOG; // equivalent to message type 4 in error_log()
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
<?php
use Vespula\Log\Log;
use Vespula\Log\Adapter\ErrorLog;

// Send to PHP's system logger
ini_set('error_log', '/myapp/logs/mylog.txt');
$adapter = new ErrorLog(); // ErrorLog::TYPE_PHP_LOG is the default (php system logger)

// Note, you will probably want to modify the message format to omit the timestamp
// because PHP's system logger automatically adds a timestamp.

$adapter->setMessageFormat("[{level}]\t{message}");
$log = new Log($adapter);
$log->info('Some message');

// Send to a specific file

$adapter = ErrorLog(ErrorLog::TYPE_FILE, '/myapp/logs/mylog.txt');

// Send to email
$headers = [
    'From: donotreply@myapp.com',
    'Subject: MyApp LogEntry'
];
$header_string = implode("\r\n", $headers);
$adapter = new ErrorLog(ErrorLog::TYPE_EMAIL, 'juser@happy.com', $header_string);

$log = new Log($adapter);
$log->info('Some message');

// You might not want to use this unless you are logging serious events.
// Your inbox might get filled up fast.