set json api functions parameter names
[tine20] / tine20 / Tinebase / Log.php
1 <?php
2 /**
3  * Tine 2.0
4  * 
5  * @package     Tinebase
6  * @subpackage  Log
7  * @license     http://www.gnu.org/licenses/agpl.html AGPL Version 3
8  * @copyright   Copyright (c) 2013 Metaways Infosystems GmbH (http://www.metaways.de)
9  * @author      Philipp Schüle <p.schuele@metaways.de>
10  * 
11  */
12 /**
13  * class Tinebase_Log
14  * 
15  * @package Tinebase
16  * @subpackage Log
17  */
18 class Tinebase_Log extends Zend_Log
19 {
20     /**
21      * 
22      * @var Tinebase_Log_Formatter
23      */
24     protected $_formatter;
25
26     /**
27      * Keeps flipped priorities from _priorities array in Zend_Log
28      * @var array
29      */
30     protected $_flippedPriorities;
31
32     /**
33      * Class constructor.  Create a new logger
34      *
35      * @param Zend_Log_Writer_Abstract|null  $writer  default writer
36      */
37     public function __construct(Zend_Log_Writer_Abstract $writer = null)
38     {
39         parent::__construct($writer);
40         $this->_flippedPriorities = array_flip($this->_priorities);
41     }
42
43     /**
44      * adds a priority in flippedPriorities array
45      * @param string  $name
46      * @param integer $priority
47      */
48     public function addPriority($name, $priority)
49     {
50         parent::addPriority($name, $priority);
51         $this->_flippedPriorities[strtoupper($name)] = $priority;
52     }
53
54     /**
55      * Checks the priority and calls respective log is its it can be called
56      * @param string $method
57      * @param string $params
58      */
59     public function __call($method, $params)
60     {
61         $priority = strtoupper($method);
62         if(isset($this->_flippedPriorities[$priority])) {
63             if(Tinebase_Core::isLogLevel($this->_flippedPriorities[$priority])) {
64                 parent::__call($method, $params);
65             }
66         }
67     }
68
69     /**
70      * add strings to replace in log output (passwords for example)
71      * 
72      * @param string $search
73      * @param string $replace
74      */
75     public function addReplacement($search, $replace = '********')
76     {
77         $this->getFormatter()->addReplacement($search, $replace);
78     }
79     
80     /**
81      * 
82      * @return Tinebase_Log_Formatter
83      */
84     public function getFormatter()
85     {
86         if (!$this->_formatter instanceof Tinebase_Log_Formatter) {
87             $this->_formatter = new Tinebase_Log_Formatter();
88         }
89         
90         return $this->_formatter;
91     }
92     
93     /**
94      * add new log writer defined by a config object/array
95      * 
96      * @param Tinebase_Config_Struct|Zend_Config|array $loggerConfig
97      * 
98      * @throws Tinebase_Exception_NotFound
99      */
100     public function addWriterByConfig($loggerConfig)
101     {
102         $loggerConfig = ($loggerConfig instanceof Tinebase_Config_Struct || $loggerConfig instanceof Zend_Config)
103             ? $loggerConfig : new Tinebase_Config_Struct($loggerConfig);
104
105         if (isset($loggerConfig->active) && $loggerConfig->active == false) {
106             // writer deactivated
107             return;
108         }
109
110         if (empty($loggerConfig->filename)) {
111             throw new Tinebase_Exception_NotFound('filename missing in logger config');
112         }
113
114         $filename = $loggerConfig->filename;
115         $writer = new Zend_Log_Writer_Stream($filename);
116         
117         $writer->setFormatter($this->getFormatter());
118
119         $priority = ($loggerConfig->priority) ? (int)$loggerConfig->priority : Zend_Log::EMERG;
120         $filter = new Zend_Log_Filter_Priority($priority);
121         $writer->addFilter($filter);
122
123         // add more filters here
124         if (isset($loggerConfig->filter->user)) {
125             $writer->addFilter(new Tinebase_Log_Filter_User($loggerConfig->filter->user));
126         }
127         if (isset($loggerConfig->filter->message)) {
128             $writer->addFilter(new Zend_Log_Filter_Message($loggerConfig->filter->message));
129         }
130         
131         $this->addWriter($writer);
132     }
133     
134     /**
135      * get max log priority
136      * 
137      * @param Tinebase_Config_Struct|Zend_Config $loggerConfig
138      * @return integer
139      */
140     public static function getMaxLogLevel($loggerConfig)
141     {
142         $logLevel = $loggerConfig && $loggerConfig->priority ? (int)$loggerConfig->priority : Zend_Log::EMERG;
143         if ($loggerConfig && $loggerConfig->additionalWriters) {
144             foreach ($loggerConfig->additionalWriters as $writerConfig) {
145                 $writerConfig = ($writerConfig instanceof Tinebase_Config_Struct || $writerConfig instanceof Zend_Config)
146                     ? $writerConfig : new Tinebase_Config_Struct($writerConfig);
147                 if ($writerConfig->priority && $writerConfig->priority > $logLevel) {
148                     $logLevel = (int) $writerConfig->priority;
149                 }
150             }
151         }
152         return $logLevel;
153     }
154 }