-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLogger.php
136 lines (116 loc) · 2.99 KB
/
Logger.php
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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
<?php
/**
* --------------------------------
* PEL - Phalcon Extensions Library
* --------------------------------
*
* This code is distributed under New BSD license.
* License is bundled with this package in file LICENSE.txt.
*
* @author Jiri Pazdernik <[email protected]>
*/
namespace Pel;
class Logger
{
/**
* Logger status
*
* @var boolean
*/
private $__enabled;
/**
* Array of logger adapters
*
* @var array
*/
private $__loggers = array();
/**
* Constructor
*/
public function __construct()
{
$this->create("void");
$this->setEnabled(true);
}
/**
* Returns specified previously created logger adapter
*
* @param string $name name of logger
* @throws \Phalcon\Logger\Exception
* @return \Pel\Logger\Adapter
*/
public function __get($name)
{
if (! isset($this->__loggers[$name])) {
throw new \Phalcon\Logger\Exception("Logger with name '{$name}' doesn't exists");
}
return ($this->__enabled) ? $this->__loggers[$name] : $this->__loggers["void"];
}
/**
* Create logger adapter
*
* @param string $loggerName name of the adapter
* @param array $config (OPTIONAL) configuration
* @return \Pel\Logger\Adapter
*/
public function create($loggerName, $config = array())
{
$adapter = self::factory($loggerName, $config);
$this->__loggers[$loggerName] = $adapter;
return $adapter;
}
/**
* Factory for creating logger adaters
*
* @param string $loggerName name of the adapter
* @param array $config (OPTIONAL) configuration
* @throws \Phalcon\Logger\Exception
* @return \Pel\Logger\Adapter
*/
public static function factory($loggerName, $config = array())
{
if ($config instanceof \Phalcon\Config) {
$config = $config->toArray();
}
if (isset($config["enabled"]) && (boolean) $config["enabled"]) {
$type = isset($config["adapter"]) ? ucfirst(strtolower($config["adapter"])) : "File";
$className = "\Pel\Logger\Adapter\\" . $type;
if (! class_exists($className)) {
throw new \Phalcon\Logger\Exception("Logger adapter: '{$type}' doesn't exists");
}
switch ($type) {
case "File":
case "Stream":
$name = isset($config["name"]) ? $config["name"] : null;
if (null === $name) {
throw new \Phalcon\Logger\Exception("Logger adapter '{$type}' requests parameter 'name'");
}
if (("File" == $type) && (false === file_put_contents($name, "", FILE_APPEND))) {
throw new \Phalcon\Logger\Exception("File: '{$name}' cannot be created or is not writable, logger cannot be created");
}
$adapter = new $className($name);
break;
default:
$adapter = new $className();
break;
}
} else {
$adapter = new Logger\Adapter\Void();
}
if (isset($config["level"])) {
$adapter->setLogLevel((int) $config["level"]);
}
return $adapter;
}
/**
* Set logger status
*
* @param boolean $value status
* @return \Pel\Logger
*/
public function setEnabled($value)
{
$this->__enabled = (boolean) $value;
return $this;
}
}