[TASK] Convert log levels from string to integer
[Packages/TYPO3.CMS.git] / typo3 / sysext / core / Classes / Log / LogLevel.php
1 <?php
2 namespace TYPO3\CMS\Core\Log;
3
4 /***************************************************************
5 * Copyright notice
6 *
7 * (c) 2011-2013 Ingo Renner (ingo@typo3.org)
8 * All rights reserved
9 *
10 * This script is part of the TYPO3 project. The TYPO3 project is
11 * free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
15 *
16 * The GNU General Public License can be found at
17 * http://www.gnu.org/copyleft/gpl.html.
18 *
19 * This script is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * GNU General Public License for more details.
23 *
24 * This copyright notice MUST APPEAR in all copies of the script!
25 ***************************************************************/
26 /**
27 * Log levels according to RFC 3164
28 *
29 * @author Ingo Renner <ingo@typo3.org>
30 */
31 class LogLevel {
32
33 /**
34 * Emergency: system is unusable
35 *
36 * You'd likely not be able to reach the system. You better have an SLA in
37 * place when this happens.
38 *
39 * @var integer
40 */
41 const EMERGENCY = 0;
42 /**
43 * Alert: action must be taken immediately
44 *
45 * Example: Entire website down, database unavailable, etc.
46 *
47 * @var integer
48 */
49 const ALERT = 1;
50 /**
51 * Critical: critical conditions
52 *
53 * Example: unexpected exception.
54 *
55 * @var integer
56 */
57 const CRITICAL = 2;
58 /**
59 * Error: error conditions
60 *
61 * Example: Runtime error.
62 *
63 * @var integer
64 */
65 const ERROR = 3;
66 /**
67 * Warning: warning conditions
68 *
69 * Examples: Use of deprecated APIs, undesirable things that are not
70 * necessarily wrong.
71 *
72 * @var integer
73 */
74 const WARNING = 4;
75 /**
76 * Notice: normal but significant condition
77 *
78 * Example: things you should have a look at, nothing to worry about though.
79 *
80 * @var integer
81 */
82 const NOTICE = 5;
83 /**
84 * Informational: informational messages
85 *
86 * Examples: User logs in, SQL logs.
87 *
88 * @var integer
89 */
90 const INFO = 6;
91 /**
92 * Debug: debug-level messages
93 *
94 * Example: Detailed status information.
95 *
96 * @var integer
97 */
98 const DEBUG = 7;
99 /**
100 * Reverse look up of log level to level name.
101 *
102 * @var array
103 */
104 static protected $levels = array(
105 self::EMERGENCY => 'EMERGENCY',
106 self::ALERT => 'ALERT',
107 self::CRITICAL => 'CRITICAL',
108 self::ERROR => 'ERROR',
109 self::WARNING => 'WARNING',
110 self::NOTICE => 'NOTICE',
111 self::INFO => 'INFO',
112 self::DEBUG => 'DEBUG'
113 );
114
115 /**
116 * Resolves the name of a log level.
117 *
118 * @param integer $level Log level.
119 * @return string Log level name.
120 */
121 static public function getName($level) {
122 self::validateLevel($level);
123 return self::$levels[$level];
124 }
125
126 /**
127 * Checks a level for validity,
128 * whether it is an integer and in the range of 0-7.
129 *
130 * @param integer $level log level to validate
131 * @return boolean TRUE if the given log level is valid, FALSE otherwise
132 */
133 static public function isValidLevel($level) {
134 return \TYPO3\CMS\Core\Utility\MathUtility::isIntegerInRange($level, self::EMERGENCY, self::DEBUG);
135 }
136
137 /**
138 * Validates a log level.
139 *
140 * @param integer $level log level to validate
141 * @return void
142 * @throws \RangeException if the given log level is invalid
143 */
144 static public function validateLevel($level) {
145 if (!self::isValidLevel($level)) {
146 throw new \RangeException('Invalid Log Level.', 1321637121);
147 }
148 }
149
150 /**
151 * Normalizes level by converting it from string to integer
152 *
153 * @param string $level
154 * @return integer|string
155 */
156 static public function normalizeLevel($level) {
157 if (is_string($level) && defined(__CLASS__ . '::' . strtoupper($level))) {
158 $level = constant(__CLASS__ . '::' . strtoupper($level));
159 }
160
161 return $level;
162 }
163
164 }
165
166
167 ?>