Completed
Push — master ( f5d71d...bfd9cb )
by Sam
12:52
created

Log::get_logger()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 3
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 4
rs 10
1
<?php
2
3
namespace SilverStripe\Logging;
4
5
use Psr\Log\LoggerInterface;
6
use SilverStripe\Core\Injector\Injector;
7
use SilverStripe\Dev\Deprecation;
8
9
/**
10
 * Wrapper class for a logging handler like {@link Zend_Log}
11
 * which takes a message (or a map of context variables) and
12
 * sends it to one or more {@link Zend_Log_Writer_Abstract}
13
 * subclasses for output.
14
 *
15
 * These priorities are currently supported:
16
 *  - Log::ERR
17
 *  - Log::WARN
18
 *  - Log::NOTICE
19
 *  - Log::INFO
20
 *  - Log::DEBUG
21
 *
22
 * You can add an error writer by calling {@link Log::add_writer()}
23
 *
24
 * Example usage of logging errors by email notification:
25
 * <code>
26
 * Log::add_writer(new LogEmailWriter('[email protected]'), Log::ERR);
27
 * </code>
28
 *
29
 * Example usage of logging errors by file:
30
 * <code>
31
 *	Log::add_writer(new LogFileWriter('/var/log/silverstripe/errors.log'), Log::ERR);
32
 * </code>
33
 *
34
 * Example usage of logging at warnings and errors by setting the priority to '<=':
35
 * <code>
36
 * Log::add_writer(new LogEmailWriter('[email protected]'), Log::WARN, '<=');
37
 * </code>
38
 *
39
 * Each writer object can be assigned a formatter. The formatter is
40
 * responsible for formatting the message before giving it to the writer.
41
 * {@link LogErrorEmailFormatter} is such an example that formats errors
42
 * into HTML for human readability in an email client.
43
 *
44
 * Formatters are added to writers like this:
45
 * <code>
46
 * $logEmailWriter = new LogEmailWriter('[email protected]');
47
 * $myEmailFormatter = new MyLogEmailFormatter();
48
 * $logEmailWriter->setFormatter($myEmailFormatter);
49
 * </code>
50
 */
51
class Log
52
{
53
54
	const ERR = 'error';
55
	const WARN = 'warning';
56
	const NOTICE = 'notice';
57
	const INFO = 'info';
58
	const DEBUG = 'debug';
59
60
	/**
61
	 * Get the logger currently in use, or create a new one if it doesn't exist.
62
	 *
63
	 * @deprecated 4.0..5.0
64
	 * @return LoggerInterface
65
	 */
66
	public static function get_logger() {
67
		Deprecation::notice('5.0', 'Use Injector::inst()->get(\'Logger\') instead');
68
		return Injector::inst()->get('Logger');
69
	}
70
71
	/**
72
	 * Dispatch a message by priority level.
73
	 *
74
	 * The message parameter can be either a string (a simple error
75
	 * message), or an array of variables. The latter is useful for passing
76
	 * along a list of debug information for the writer to handle, such as
77
	 * error code, error line, error context (backtrace).
78
	 *
79
	 * @param mixed $message Exception object or array of error context variables
80
	 * @param string $priority Priority. Possible values: Log::ERR, Log::WARN, Log::NOTICE, Log::INFO or Log::DEBUG
81
	 *
82
	 * @deprecated 4.0.0:5.0.0 Use Injector::inst()->get('Logger')->log($priority, $message) instead
83
	 */
84
	public static function log($message, $priority) {
0 ignored issues
show
Coding Style Best Practice introduced by
Please use __construct() instead of a PHP4-style constructor that is named after the class.
Loading history...
85
		Deprecation::notice('5.0', 'Use Injector::inst()->get(\'Logger\')->log($priority, $message) instead');
86
		Injector::inst()->get('Logger')->log($priority, $message);
87
	}
88
}
89