2015-07-24 00:53:41 +02:00
|
|
|
<?php
|
|
|
|
|
2016-08-19 00:51:35 +02:00
|
|
|
namespace SilverStripe\Logging;
|
|
|
|
|
|
|
|
use Psr\Log\LoggerInterface;
|
|
|
|
use SilverStripe\Core\Injector\Injector;
|
|
|
|
use SilverStripe\Dev\Deprecation;
|
|
|
|
|
2015-07-24 00:53:41 +02:00
|
|
|
/**
|
|
|
|
* Wrapper class for a logging handler like {@link Zend_Log}
|
|
|
|
* which takes a message (or a map of context variables) and
|
|
|
|
* sends it to one or more {@link Zend_Log_Writer_Abstract}
|
|
|
|
* subclasses for output.
|
|
|
|
*
|
|
|
|
* These priorities are currently supported:
|
2016-09-09 08:43:05 +02:00
|
|
|
* - Log::ERR
|
|
|
|
* - Log::WARN
|
|
|
|
* - Log::NOTICE
|
|
|
|
* - Log::INFO
|
|
|
|
* - Log::DEBUG
|
2015-07-24 00:53:41 +02:00
|
|
|
*
|
2016-09-09 08:43:05 +02:00
|
|
|
* You can add an error writer by calling {@link Log::add_writer()}
|
2015-07-24 00:53:41 +02:00
|
|
|
*
|
|
|
|
* Example usage of logging errors by email notification:
|
|
|
|
* <code>
|
2016-09-09 08:43:05 +02:00
|
|
|
* Log::add_writer(new LogEmailWriter('my@email.com'), Log::ERR);
|
2015-07-24 00:53:41 +02:00
|
|
|
* </code>
|
|
|
|
*
|
|
|
|
* Example usage of logging errors by file:
|
|
|
|
* <code>
|
2016-09-09 08:43:05 +02:00
|
|
|
* Log::add_writer(new LogFileWriter('/var/log/silverstripe/errors.log'), Log::ERR);
|
2015-07-24 00:53:41 +02:00
|
|
|
* </code>
|
|
|
|
*
|
|
|
|
* Example usage of logging at warnings and errors by setting the priority to '<=':
|
|
|
|
* <code>
|
2016-09-09 08:43:05 +02:00
|
|
|
* Log::add_writer(new LogEmailWriter('my@email.com'), Log::WARN, '<=');
|
2015-07-24 00:53:41 +02:00
|
|
|
* </code>
|
|
|
|
*
|
|
|
|
* Each writer object can be assigned a formatter. The formatter is
|
|
|
|
* responsible for formatting the message before giving it to the writer.
|
2016-09-09 08:43:05 +02:00
|
|
|
* {@link LogErrorEmailFormatter} is such an example that formats errors
|
2015-07-24 00:53:41 +02:00
|
|
|
* into HTML for human readability in an email client.
|
|
|
|
*
|
|
|
|
* Formatters are added to writers like this:
|
|
|
|
* <code>
|
2016-09-09 08:43:05 +02:00
|
|
|
* $logEmailWriter = new LogEmailWriter('my@email.com');
|
2015-07-24 00:53:41 +02:00
|
|
|
* $myEmailFormatter = new MyLogEmailFormatter();
|
|
|
|
* $logEmailWriter->setFormatter($myEmailFormatter);
|
|
|
|
* </code>
|
|
|
|
*/
|
2016-09-09 08:43:05 +02:00
|
|
|
class Log
|
2015-07-24 00:53:41 +02:00
|
|
|
{
|
|
|
|
|
|
|
|
const ERR = 'error';
|
|
|
|
const WARN = 'warning';
|
|
|
|
const NOTICE = 'notice';
|
|
|
|
const INFO = 'info';
|
|
|
|
const DEBUG = 'debug';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the logger currently in use, or create a new one if it doesn't exist.
|
|
|
|
*
|
2016-08-19 00:51:35 +02:00
|
|
|
* @deprecated 4.0..5.0
|
|
|
|
* @return LoggerInterface
|
2015-07-24 00:53:41 +02:00
|
|
|
*/
|
|
|
|
public static function get_logger() {
|
2016-08-19 00:51:35 +02:00
|
|
|
Deprecation::notice('5.0', 'Use Injector::inst()->get(\'Logger\') instead');
|
2015-07-24 00:53:41 +02:00
|
|
|
return Injector::inst()->get('Logger');
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Dispatch a message by priority level.
|
|
|
|
*
|
|
|
|
* The message parameter can be either a string (a simple error
|
|
|
|
* message), or an array of variables. The latter is useful for passing
|
|
|
|
* along a list of debug information for the writer to handle, such as
|
|
|
|
* error code, error line, error context (backtrace).
|
|
|
|
*
|
|
|
|
* @param mixed $message Exception object or array of error context variables
|
2016-09-09 08:43:05 +02:00
|
|
|
* @param string $priority Priority. Possible values: Log::ERR, Log::WARN, Log::NOTICE, Log::INFO or Log::DEBUG
|
2015-07-24 00:53:41 +02:00
|
|
|
*
|
|
|
|
* @deprecated 4.0.0:5.0.0 Use Injector::inst()->get('Logger')->log($priority, $message) instead
|
|
|
|
*/
|
2015-09-14 17:00:19 +02:00
|
|
|
public static function log($message, $priority) {
|
2016-08-19 00:51:35 +02:00
|
|
|
Deprecation::notice('5.0', 'Use Injector::inst()->get(\'Logger\')->log($priority, $message) instead');
|
2015-07-24 00:53:41 +02:00
|
|
|
Injector::inst()->get('Logger')->log($priority, $message);
|
|
|
|
}
|
|
|
|
}
|