Overview

Packages

  • Core
    • Authentication
    • Backend
    • Cache
    • CEC
    • Chain
    • ContentType
    • Database
    • Datatype
    • Debug
    • Exception
    • Frontend
      • Search
      • URI
      • Util
    • GenericDB
      • Model
    • GUI
      • HTML
    • I18N
    • LayoutHandler
    • Log
    • Security
    • Session
    • Util
    • Validation
    • Versioning
    • XML
  • Module
    • ContentSitemapHtml
    • ContentSitemapXml
    • ContentUserForum
    • NavigationTop
  • mpAutoloaderClassMap
  • None
  • Plugin
    • ContentAllocation
    • CronjobOverview
    • FormAssistant
    • FrontendLogic
    • FrontendUsers
    • Linkchecker
    • ModRewrite
    • Newsletter
    • Repository
      • FrontendNavigation
      • KeywordDensity
    • SearchSolr
    • SmartyWrapper
    • UrlShortener
    • UserForum
    • Workflow
  • PluginManager
  • Setup
    • Form
    • GUI
    • Helper
      • Environment
      • Filesystem
      • MySQL
      • PHP
    • UpgradeJob

Classes

  • cLog
  • cLogWriter
  • cLogWriterFile
  • cModuleLog
  • Overview
  • Package
  • Class
  • Tree
  • Deprecated
  • Todo
  1: <?php
  2: /**
  3:  * This file contains the log class.
  4:  *
  5:  * @package    Core
  6:  * @subpackage Log
  7:  * @version    SVN Revision $Rev:$
  8:  *
  9:  * @author     Dominik Ziegler
 10:  * @copyright  four for business AG <www.4fb.de>
 11:  * @license    http://www.contenido.org/license/LIZENZ.txt
 12:  * @link       http://www.4fb.de
 13:  * @link       http://www.contenido.org
 14:  */
 15: 
 16: defined('CON_FRAMEWORK') || die('Illegal call: Missing framework initialization - request aborted.');
 17: 
 18: /**
 19:  * This class contains the main functionalities for the logging in CONTENIDO.
 20:  *
 21:  * Examples:
 22:  *
 23:  * $writer = cLogWriter::factory("File", array('destination' => 'contenido.log'));
 24:  * $log = new cLog($writer);
 25:  *
 26:  * $log->addPriority("CONTENIDO", 10);
 27:  * $log->log("Contenido Log Message.", "CONTENIDO");
 28:  * $log->contenido("Same log entry in short notation.");
 29:  * $log->removePriority("CONTENIDO");
 30:  *
 31:  * $log->emerg("System down.");
 32:  *
 33:  * $log->log('Notice Log Message', cLog::NOTICE);
 34:  *
 35:  * $log->buffer('Buffered Log Message', cLog::WARN);
 36:  * $log->commit();
 37:  *
 38:  * @package    Core
 39:  * @subpackage Log
 40:  */
 41: class cLog {
 42:     const EMERG   = 0;
 43:     const ALERT   = 1;
 44:     const CRIT    = 2;
 45:     const ERR     = 3;
 46:     const WARN    = 4;
 47:     const NOTICE  = 5;
 48:     const INFO    = 6;
 49:     const DEBUG   = 7;
 50: 
 51:     /**
 52:      * @var cLogWriter Contains the local log writer instance.
 53:      */
 54:     protected $_writer;
 55: 
 56:     /**
 57:      * @var array Contains all shortcut handlers
 58:      */
 59:     protected $_shortcutHandlers = array();
 60: 
 61:     /**
 62:      * @var array Contains all available priorities
 63:      */
 64:     protected $_priorities = array();
 65: 
 66:     /**
 67:      * @var array Contains all default priorities
 68:      */
 69:     protected $_defaultPriorities = array();
 70: 
 71:     /**
 72:      * @var array Contains all buffered messages
 73:      */
 74:     protected $_buffer = array();
 75: 
 76:     /**
 77:      * Creates a new instance of the CONTENIDO Log mechanism.
 78:      *
 79:      * The log format interface of cLog is capable of being extended by subclasses. See the note about
 80:      * the log shortcuts below.
 81:      *
 82:      *
 83:      * About Log Shortcuts
 84:      * -------------------
 85:      * Log shortcuts are placeholders which are replaced when a log entry is created. Placeholders start with a
 86:      * percentage sign (%) and contain one or more characters. Each placeholder is handled by an own function which
 87:      * decides what to do.
 88:      *
 89:      * @param  mixed  writer   Writer object (any subclass of cLogWriter), or false if cLog should handle the writer creation
 90:      */
 91:     public function __construct($writer = false) {
 92:         global $cfg;
 93: 
 94:         $createWriter = false;
 95: 
 96:         if ($writer == false) {
 97:             $createWriter = true;
 98:         } else if (!is_object($writer) || ($writer instanceof cLogWriter) == false) {
 99:             cWarning(__FILE__, __LINE__, "The passed class is not a subclass of cLogWriter. Creating new one.");
100:             $createWriter = true;
101:         }
102: 
103:         if ($createWriter == true) {
104:             $options = array('destination' => $cfg['path']['contenido_logs'] . 'data/contenido.log');
105:             $writer = cLogWriter::factory("File", $options);
106:         }
107: 
108:         $this->setWriter($writer);
109:         $this->setShortcutHandler("%date", array($this, "shDate"));
110:         $this->setShortcutHandler("%level", array($this, "shLevel"));
111:         $this->setShortcutHandler("%message", array($this, "shMessage"));
112: 
113:         $this->getWriter()->setOption('log_format', '[%date] [%level] %message', false);
114: 
115:         $reflection = new ReflectionClass($this);
116:         $this->_priorities = $this->_defaultPriorities = array_flip($reflection->getConstants());
117:     }
118: 
119:     /**
120:      * Returns the local writer instance.
121:      * @return    cLogWriter
122:      */
123:     public function getWriter() {
124:         return $this->_writer;
125:     }
126: 
127:     /**
128:      * Sets the local writer instance.
129:      *
130:      * @param    cLogWriter    $writer    Writer instacne
131:      */
132:     public function setWriter(cLogWriter $writer) {
133:         $this->_writer = $writer;
134:     }
135: 
136:     /**
137:      * Defines a custom shortcut handler.
138:      *
139:      * Each shortcut handler receives an array with the
140:      * message and the priority of the entry.
141:      *
142:      * @param string $shortcut Shortcut name
143:      * @param string $handler Name of the function to call
144:      * @throws cInvalidArgumentException if the given shortcut is empty or
145:      *         already in use or if the handler is not callable
146:      * @return bool True if setting was successful
147:      */
148:     public function setShortcutHandler($shortcut, $handler) {
149:         if ($shortcut == '') {
150:             throw new cInvalidArgumentException('The shortcut name must not be empty.');
151:         }
152: 
153:         if (substr($shortcut, 0, 1) == "%") {
154:             $shortcut = substr($shortcut, 1);
155:         }
156: 
157:         if (is_callable($handler) == false) {
158:             throw new cInvalidArgumentException('The specified shortcut handler does not exist.');
159:         }
160: 
161:         if (array_key_exists($shortcut, $this->_shortcutHandlers)) {
162:             throw new cInvalidArgumentException('The shortcut ' . $shortcut . ' is already in use!');
163:         }
164: 
165:         $this->_shortcutHandlers[$shortcut] = $handler;
166: 
167:         return true;
168:     }
169: 
170:     /**
171:      * Unsets a specific shortcut handler.
172:      *
173:      * @param string $shortcut Name of the shortcut
174:      * @throws cInvalidArgumentException if the given shortcut handler does not
175:      *         exist
176:      * @return boolean
177:      */
178:     public function unsetShortcutHandler($shortcut) {
179:         if (!in_array($shortcut, $this->_shortcutHandlers)) {
180:             throw new cInvalidArgumentException('The specified shortcut handler does not exist.');
181:         }
182: 
183:         unset($this->_shortcutHandlers[$shortcut]);
184:         return true;
185:     }
186: 
187:     /**
188:      * Buffers a log message for committing them on a later moment.
189:      *
190:      * @param    string    $message    Message to buffer
191:      * @param    mixed    $priority    Priority of the log entry (optional)
192:      */
193:     public function buffer($message, $priority = NULL) {
194:         $this->_buffer[] = array($message, $priority);
195:     }
196: 
197:     /**
198:      * Commits all buffered messages and empties the message buffer if parameter is not false.
199:      *
200:      * @param    boolean    $revoke    Flag, whether the buffer is cleared or not (optional, default: true)
201:      */
202:     public function commit($revoke = true) {
203:         if (count($this->_buffer) == 0) {
204:             cWarning(__FILE__, __LINE__, "There are no buffered messages to commit.");
205:             return false;
206:         }
207: 
208:         foreach ($this->_buffer as $bufferInfo) {
209:             $this->log($bufferInfo[0], $bufferInfo[1]);
210:         }
211: 
212:         if ($revoke == true) {
213:             $this->revoke();
214:         }
215:     }
216: 
217:     /**
218:      * Empties the message buffer.
219:      */
220:     public function revoke() {
221:         $this->_buffer = array();
222:     }
223: 
224:     /**
225:      * Logs a message using the local writer instance
226:      *
227:      * @param    string    $message    Message to log
228:      * @param    mixed      $priority    Priority of the log entry (optional)
229:      */
230:     public function log($message, $priority = NULL) {
231:         if ($priority && is_int($priority) == false && in_array($priority, $this->_priorities)) {
232:             $priority = array_search($priority, $this->_priorities);
233:         }
234: 
235:         if ($priority === NULL || array_key_exists($priority, $this->_priorities) == false) {
236:             $priority = $this->getWriter()->getOption('default_priority');
237:         }
238: 
239:         $logMessage = $this->getWriter()->getOption('log_format');
240:         $lineEnding = $this->getWriter()->getOption('line_ending');
241: 
242:         foreach ($this->_shortcutHandlers as $shortcut => $handler) {
243:             if (substr($shortcut, 0, 1) != "%") {
244:                 $shortcut = "%" . $shortcut;
245:             }
246: 
247:             $info = array(
248:                 'message' => $message,
249:                 'priority' => $priority
250:             );
251: 
252:             $value = call_user_func($handler, $info);
253: 
254:             $logMessage = str_replace($shortcut, $value, $logMessage);
255:         }
256: 
257:         $this->getWriter()->write($logMessage . $lineEnding, $priority);
258:     }
259: 
260:     /**
261:      * Adds a new priority to the log.
262:      *
263:      * @param string $name Name of the log priority
264:      * @param int $value Index value of the log priority
265:      * @throws cInvalidArgumentException if the given name is empty, already
266:      *         exists or the value already exists
267:      */
268:     public function addPriority($name, $value) {
269:         if ($name == '') {
270:             throw new cInvalidArgumentException('Priority name must not be empty.');
271:         }
272: 
273:         if (in_array($name, $this->_priorities)) {
274:             throw new cInvalidArgumentException('The given priority name already exists.');
275:         }
276: 
277:         if (array_key_exists($value, $this->_priorities)) {
278:             throw new cInvalidArgumentException('The priority value already exists.');
279:         }
280: 
281:         $this->_priorities[$value] = $name;
282:     }
283: 
284:     /**
285:      * Removes a priority from log.
286:      * Default properties can not be removed.
287:      *
288:      * @param string $name Name of the log priority to remove
289:      * @throws cInvalidArgumentException if the given name is empty, does not
290:      *         exist or is a default priority
291:      */
292:     public function removePriority($name) {
293:         if ($name == '') {
294:             throw new cInvalidArgumentException('Priority name must not be empty.');
295:         }
296: 
297:         if (in_array($name, $this->_priorities) == false) {
298:             throw new cInvalidArgumentException('Priority name does not exist.');
299:         }
300: 
301:         if (in_array($name, $this->_defaultPriorities) == true) {
302:             throw new cInvalidArgumentException('Removing default priorities is not allowed.');
303:         }
304: 
305:         $priorityIndex = array_search($name, $this->_priorities);
306: 
307:         unset($this->_priorities[$priorityIndex]);
308:     }
309: 
310:     /**
311:      * Magic call method for direct priority named calls.
312:      *
313:      * @param    string    $method        Name of the method
314:      * @param    array    $arguments    Array with the method arguments
315:      * @throws cInvalidArgumentException if the given priority is not supported
316:      */
317:     public function __call($method, $arguments) {
318:         $priorityName = strtoupper($method);
319: 
320:         if (in_array($priorityName, $this->_priorities) == false) {
321:             throw new cInvalidArgumentException('The given priority ' . $priorityName . ' is not supported.');
322:         }
323: 
324:         $priorityIndex = array_search($priorityName, $this->_priorities);
325: 
326:         return $this->log($arguments[0], $priorityIndex);
327:     }
328: 
329:     /**
330:      * Shortcut Handler Date.
331:      * Returns the current date
332:      * @return    string    The current date
333:      */
334:     public function shDate() {
335:         return date("Y-m-d H:i:s");
336:     }
337: 
338:     /**
339:      * Shortcut Handler Level.
340:      * Returns the canonical name of the priority.
341:      * The canonical name is padded to 10 characters to achieve a better formatting.
342:      * @return    string    The canonical log level
343:      */
344:     public function shLevel($info) {
345:         $logLevel = $info['priority'];
346:         return str_pad($this->_priorities[$logLevel], 10, " ", STR_PAD_BOTH);
347:     }
348: 
349:     /**
350:      * Shortcut Handler Message.
351:      * Returns the log message.
352:      * @return    string    The log message
353:      */
354:     public function shMessage($info) {
355:         return $info['message'];
356:     }
357: }
358: 
359: ?>
CMS CONTENIDO 4.9.1 API documentation generated by ApiGen 2.8.0