Overview

Packages

  • CONTENIDO
  • Core
    • Authentication
    • Backend
    • Cache
    • CEC
    • Chain
    • ContentType
    • Database
    • Debug
    • Exception
    • Frontend
      • Search
      • URI
      • Util
    • GenericDB
      • Model
    • GUI
      • HTML
    • I18N
    • LayoutHandler
    • Log
    • Security
    • Session
    • Util
    • Validation
    • Versioning
    • XML
  • Module
    • ContentRssCreator
    • ContentSitemapHtml
    • ContentSitemapXml
    • ContentUserForum
    • NavigationTop
    • ScriptCookieDirective
  • 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
  • Smarty
    • Cacher
    • Compiler
    • Config
    • Debug
    • PluginsBlock
    • PluginsFilter
    • PluginsFunction
    • PluginsInternal
    • PluginsModifier
    • PluginsModifierCompiler
    • PluginsShared
    • Security
    • Template
    • TemplateResources
  • Swift
    • ByteStream
    • CharacterStream
    • Encoder
    • Events
    • KeyCache
    • Mailer
    • Mime
    • Plugins
    • Transport

Classes

  • Swift_FailoverTransport
  • Swift_LoadBalancedTransport
  • Swift_MailTransport
  • Swift_Plugins_Loggers_ArrayLogger
  • Swift_Plugins_Loggers_EchoLogger
  • Swift_SendmailTransport
  • Swift_SmtpTransport
  • Swift_Transport_AbstractSmtpTransport
  • Swift_Transport_Esmtp_Auth_CramMd5Authenticator
  • Swift_Transport_Esmtp_Auth_LoginAuthenticator
  • Swift_Transport_Esmtp_Auth_PlainAuthenticator
  • Swift_Transport_Esmtp_AuthHandler
  • Swift_Transport_EsmtpTransport
  • Swift_Transport_FailoverTransport
  • Swift_Transport_LoadBalancedTransport
  • Swift_Transport_MailTransport
  • Swift_Transport_SendmailTransport
  • Swift_Transport_SimpleMailInvoker
  • Swift_Transport_StreamBuffer

Interfaces

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