1: <?php
2:
3: namespace GAubry\Logger;
4:
5: use Psr\Log\LogLevel;
6: use Psr\Log\InvalidArgumentException;
7:
8: /**
9: * This is a simple abstract Logger implementation that other Loggers can inherit from.
10: *
11: * Copyright (c) 2013 Geoffroy Aubry <geoffroy.aubry@free.fr>
12: * Licensed under the GNU Lesser General Public License v3 (LGPL version 3).
13: *
14: * @see https://github.com/php-fig/fig-standards/blob/master/accepted/PSR-3-logger-interface.md
15: * @copyright 2013 Geoffroy Aubry <geoffroy.aubry@free.fr>
16: * @license http://www.gnu.org/licenses/lgpl.html
17: */
18: abstract class AbstractLogger extends \Psr\Log\AbstractLogger
19: {
20: /**
21: * Int value of threshold required to log message.
22: * @var int
23: * @see self::$aIntLevels
24: */
25: protected $iMinMsgLevel;
26:
27: /**
28: * Describes log levels with priority.
29: * PSR-3 Log levels are string-based…
30: * @var array
31: * @see \Psr\Log\LogLevel
32: */
33: protected static $aIntLevels = array(
34: LogLevel::DEBUG => 0,
35: LogLevel::INFO => 10,
36: LogLevel::NOTICE => 20,
37: LogLevel::WARNING => 30,
38: LogLevel::ERROR => 40,
39: LogLevel::CRITICAL => 50,
40: LogLevel::ALERT => 60,
41: LogLevel::EMERGENCY => 70
42: );
43:
44: /**
45: * Constructor.
46: *
47: * @param string $iMinMsgLevel threshold required to log message, must be defined in \Psr\Log\LogLevel
48: * @throws \Psr\Log\InvalidArgumentException if calling this method with a level not defined in \Psr\Log\LogLevel
49: */
50: protected function __construct ($sMinMsgLevel = LogLevel::DEBUG)
51: {
52: $this->checkMsgLevel($sMinMsgLevel);
53: $this->iMinMsgLevel = self::$aIntLevels[$sMinMsgLevel];
54: }
55:
56: /**
57: * Check that specified $sMsgLevel is defined into \Psr\Log\LogLevel.
58: *
59: * @param string $sMsgLevel
60: * @throws \Psr\Log\InvalidArgumentException if calling this method with a level not defined in \Psr\Log\LogLevel
61: */
62: protected function checkMsgLevel ($sMsgLevel)
63: {
64: if (! isset(self::$aIntLevels[$sMsgLevel])) {
65: $sErrorMsg = "Unkown level: '$sMsgLevel'! Level MUST be defined in \Psr\Log\LogLevel class.";
66: throw new InvalidArgumentException($sErrorMsg, 1);
67: }
68: }
69:
70: /**
71: * Interpolates context values into the message placeholders.
72: * Taken from PSR-3's example implementation.
73: *
74: * Placeholder names MUST be delimited with a single opening brace { and a single closing brace }.
75: * There MUST NOT be any whitespace between the delimiters and the placeholder name.
76: * Placeholder names SHOULD be composed only of the characters A-Z, a-z, 0-9, underscore _, and period ..
77: * The use of other characters is reserved for future modifications of the placeholders specification.
78: *
79: * Placeholders in the form {foo} will be replaced by the context data in key "foo".
80: *
81: * @param string $sMessage message with placeholders
82: * @param array $aContext context array
83: * @return string message whose placeholders are replaced by context values
84: */
85: protected function interpolateContext ($sMessage, array $aContext)
86: {
87: // build a replacement array with braces around the context keys
88: $aReplace = array();
89: foreach ($aContext as $sKey => $mValue) {
90: $sValue = (string)$mValue;
91: $aReplace['{' . trim($sKey) . '}'] = $sValue;
92: }
93:
94: // interpolate replacement values into the message and return
95: return strtr($sMessage, $aReplace);
96: }
97: }
98: