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