initial setup of framework code
[cacert-mgr.git] / manager / library / log / Log.php
1 <?php
2 /**
3 * encapsulate Zend_Log with one or several log writers within an singleton class
4 * @author markus
5 * $Id: Log.php 77 2010-02-26 11:58:34Z markus $
6 */
7 class Log {
8 /**
9 * static pointer to instances
10 * @var array(Config)
11 */
12 private static $instances = array();
13
14 /**
15 * can handle several instances, distinct by instance name string
16 * @var string
17 */
18 private $instanceName = '';
19
20 /**
21 * config object
22 * @var Zend_Log
23 */
24 private $log = null;
25
26 /**
27 * make new logger, configuration is taken from system_config, section $instanceName
28 * @param string $instanceName
29 * @param string $application
30 */
31 protected function __construct($instanceName, $application = null) {
32 if ($instanceName === null)
33 throw new Exception(__METHOD__ . ': expected an instance name, got none');
34
35 $config = Config::getInstance(SYSTEM_CONFIG);
36 $log_config = $config->$instanceName;
37
38 $this->log = new Zend_Log();
39 if (isset($log_config->file) && intval($log_config->file->enabled) !== 0) {
40 $file_logger = new Zend_Log_Writer_Stream($log_config->file->name);
41
42 /**
43 *
44 $format = Zend_Log_Formatter_Simple::DEFAULT_FORMAT;
45 $formatter = new Zend_Log_Formatter_Simple($format);
46 $file_logger->setFormatter($formatter);
47 */
48 if (isset($application) && $application != '')
49 $this->log->setEventItem('application', $application);
50 $formatter = new Zend_Log_Formatter_Simple('%syslog_time% %application%[%pid%]: %priorityName%: %message%' . PHP_EOL);
51 $file_logger->setFormatter($formatter);
52 $this->log->addWriter($file_logger);
53 }
54 if (isset($log_config->syslog) && intval($log_config->syslog->enabled) !== 0) {
55 $param = array('facility' => $log_config->syslog->facility);
56 if (isset($application) && $application != '')
57 $param['application'] = $application;
58
59 $sys_logger = new Zend_Log_Writer_Syslog($param);
60 $formatter = new Zend_Log_Formatter_Simple('%priorityName%: %message%' . PHP_EOL);
61 $sys_logger->setFormatter($formatter);
62 $this->log->addWriter($sys_logger);
63 }
64
65 $filter = new Zend_Log_Filter_Priority(intval($log_config->priority));
66 $this->log->addFilter($filter);
67 }
68
69 /**
70 * get already existing instance, make new instance or throw an exception
71 * @param string $instanceName
72 * @param string $application
73 */
74 public static function getInstance($instanceName = null, $application = null) {
75 if ($instanceName === null) {
76 if (count(self::$instances) == 0)
77 throw new Exception(__METHOD__ . ': expected an instance name, got none');
78 return self::$instances[0];
79 }
80
81 if (!array_key_exists($instanceName, self::$instances)) {
82 self::$instances[$instanceName] = new Log($instanceName, $application);
83 }
84
85 return self::$instances[$instanceName];
86 }
87
88 /**
89 * return SYSTEM_LOG for convenience
90 * @return Zend_Log
91 */
92 public static function Log() {
93 return self::$instances[SYSTEM_LOG]->getLog();
94 }
95
96 /**
97 * get the Zend_Log object
98 * @return Zend_Log
99 */
100 public function getLog() {
101 $this->log->setEventItem('pid', posix_getpid());
102 $this->log->setEventItem('syslog_time', date('Y-m-d H:i:s'));
103 return $this->log;
104 }
105 }