Advertisement
Guest User

Untitled

a guest
Dec 14th, 2011
186
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 6.24 KB | None | 0 0
  1. <?php
  2. namespace Logging;
  3.  
  4. if (!defined('FaabBB'))
  5.     die;
  6.    
  7. use Logging\OutputHandler as OutputHandler;
  8. use Language\StackTrace as StackTrace;
  9.  
  10. /**
  11.  * A {@link Logger} object is used to log messages for a specific system
  12.  *  or application component.
  13.  *
  14.  * @category logging
  15.  * @version 3.010
  16.  * @since 3.010
  17.  * @copyright Copyright &copy; 2011, FaabTech
  18.  * @author Fabian M.
  19.  */
  20. class Logger {
  21.     /**
  22.      * Array with saved {@link Logger} instances.
  23.      */
  24.     protected static $loggers = array();
  25.  
  26.     /**
  27.      * The name of this {@link Logger}.
  28.      */
  29.     protected $name = "";
  30.  
  31.     /**
  32.      * The {@link Handler}s of this logger.
  33.      */
  34.     protected $handlers = array();
  35.  
  36.     /**
  37.      * The {@link Level#value} of this {@link Logger}
  38.      */
  39.     protected $levelValue = 800;
  40.  
  41.     /**
  42.      * Get a {@link Logger} by name.
  43.      *
  44.      * @since 3.010
  45.      * @param $name The name of the {@link Logger} to get.
  46.      * @return the {@link Logger} instance.
  47.      */
  48.     public static function getLogger($name) {
  49.         if (isset(self::$loggers[$name]))
  50.             return self::$logger[$name];
  51.         $logger = new Logger($name);
  52.         self::$loggers[$name] = $logger;
  53.         return $logger;
  54.     }
  55.  
  56.     /**
  57.      * Creates a new {@link Logger} instance.
  58.      *
  59.      * @since 3.010
  60.      * @param $name The name of this {@link Logger}.
  61.      */
  62.     public function __construct($name) {
  63.         $this->name = $name;
  64.         $this->levelValue = Level::$INFO['value'];
  65.     }
  66.  
  67.     /**
  68.      * Add a {@link Handler} to this {@link Logger}.
  69.      *
  70.      * @since 3.010
  71.      * @param $handler The {@link Handler} to add.
  72.      */
  73.     public function addHandler($handler) {
  74.         array_push($this->handlers, $handler);
  75.     }
  76.  
  77.     /**
  78.      * Get the {@link Handler}s of this {@link Logger}.
  79.      *
  80.      * @since 3.010
  81.      * @return the {@link Handler}s of this {@link Logger}.
  82.      */
  83.     public function getHandlers() {
  84.         return $this->handlers;
  85.     }
  86.  
  87.     /**
  88.      * Set the {@link Level} of this {@link Logger}.
  89.      *
  90.      * @since 3.010
  91.      * @param $Level The {@link Level} to set.
  92.      */
  93.     public function setLevel($level) {
  94.         $this->levelValue = $level['value'];
  95.     }
  96.  
  97.     /**
  98.      * Get the {@link Level} of this {@link Logger}.
  99.      *
  100.      * @since 3.010
  101.      * @return the {@link Level} of this {@link Logger}.
  102.      */
  103.     public function getLevel() {
  104.         return $this->levelValue;
  105.     }
  106.  
  107.     /**
  108.      * Log a {@link LogRecord}.
  109.      * All the other logging methods in this class call through this method to actually perform any logging.
  110.      * Subclasses can override this single method to capture all log activity.
  111.      *
  112.      * @param record The {@link LogRecord} to be published.
  113.      */
  114.     protected function log($record, $msg = null) {
  115.         if ($msg == null) {
  116.             // String is given.
  117.             if (is_string($record)) {
  118.                 $s = new StackTrace(1, 0);
  119.                 $record = new LogRecord(Level::$INFO, $record, $s->getStackFrames());
  120.             }
  121.             $l = $record->getLevel();
  122.             if ($l['value'] < $this->levelValue ||
  123.                 $this->levelValue == Level::$OFF['value']) {
  124.                 return;
  125.             }
  126.            
  127.             foreach ($this->handlers as $handler) {
  128.                 $handler->publish($record);
  129.             }
  130.         } else {
  131.             $s = new StackTrace(2, 0);
  132.             $this->log(new LogRecord($record, $msg, $s->getStackFrames()));
  133.         }
  134.     }
  135.  
  136.     /**
  137.      * Log a {@link Level#SEVERE} message.
  138.      * If the logger is currently enabled for the {@link Level#SEVERE} message level then the given message is
  139.      *  forwarded to all the registered output {@link Handler} objects.
  140.      *
  141.      * @param msg The string message (or a key in the message catalog)
  142.      */
  143.     public function severe($msg) {
  144.         $l = Level::$SEVERE;
  145.         if ($l['value'] < $this->levelValue) {
  146.             return;
  147.         }
  148.  
  149.         $this->log(Level::$SEVERE, $msg);
  150.     }
  151.  
  152.     /**
  153.      * Log a {@link Level#WARNING} message.
  154.      * If the logger is currently enabled for the {@link Level#WARNING} message level then the given message is
  155.      *  forwarded to all the registered output {@link Handler} objects.
  156.      *
  157.      * @param msg The string message (or a key in the message catalog)
  158.      */
  159.     public function warning($msg) {
  160.         $l = Level::$WARNING;
  161.         if ($l['value'] < $this->levelValue) {
  162.             return;
  163.         }
  164.  
  165.         $this->log(Level::$WARNING, $msg);
  166.     }
  167.     /**
  168.      * Log a {@link Level#INFO} message.
  169.      * If the logger is currently enabled for the {@link Level#INFO} message level then the given message is
  170.      *  forwarded to all the registered output {@link Handler} objects.
  171.      *
  172.      * @param msg The string message (or a key in the message catalog)
  173.      */
  174.     public function info($msg) {
  175.         $l = Level::$INFO;
  176.         if ($l['value'] < $this->levelValue) {
  177.             return;
  178.         }
  179.  
  180.         $this->log(Level::$INFO, $msg);
  181.     }
  182.     /**
  183.      * Log a {@link Level#CONFIG} message.
  184.      * If the logger is currently enabled for the {@link Level#CONFIG} message level then the given message is
  185.      *  forwarded to all the registered output {@link Handler} objects.
  186.      *
  187.      * @param msg The string message (or a key in the message catalog)
  188.      */
  189.     public function config($msg) {
  190.         $l = Level::$CONFIG;
  191.         if ($l['value'] < $this->levelValue) {
  192.             return;
  193.         }
  194.  
  195.         $this->log(Level::$CONFIG, $msg);
  196.     }
  197.     /**
  198.      * Log a {@link Level#FINE} message.
  199.      * If the logger is currently enabled for the {@link Level#FINE} message level then the given message is
  200.      *  forwarded to all the registered output {@link Handler} objects.
  201.      *
  202.      * @param msg The string message (or a key in the message catalog)
  203.      */
  204.     public function fine($msg) {
  205.         $l = Level::$FINE;
  206.         if ($l['value'] < $this->levelValue) {
  207.             return;
  208.         }
  209.  
  210.         $this->log(Level::$FINE, $msg);
  211.     }
  212.     /**
  213.      * Log a {@link Level#FINER} message.
  214.      * If the logger is currently enabled for the {@link Level#FINER} message level then the given message is
  215.      *  forwarded to all the registered output {@link Handler} objects.
  216.      *
  217.      * @param msg The string message (or a key in the message catalog)
  218.      */
  219.     public function finer($msg) {
  220.         $l = Level::$FINER;
  221.         if ($l['value'] < $this->levelValue) {
  222.             return;
  223.         }
  224.  
  225.         $this->log(Level::$FINER, $msg);
  226.     }
  227.  
  228.     /**
  229.      * Log a {@link Level#FINEST} message.
  230.      * If the logger is currently enabled for the {@link Level#FINEST} message level then the given message is
  231.      *  forwarded to all the registered output {@link Handler} objects.
  232.      *
  233.      * @param msg The string message (or a key in the message catalog)
  234.      */
  235.     public function finest($msg) {
  236.         $l = Level::$FINEST;
  237.         if ($l['value'] < $this->levelValue) {
  238.             return;
  239.         }
  240.  
  241.         $this->log(Level::$FINEST, $msg);
  242.     }
  243.  
  244.  
  245. }
  246. ?>
  247.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement