13 use Monolog\Formatter\LineFormatter;
14 use Monolog\Handler\RotatingFileHandler;
15 use Monolog\Handler\StreamHandler;
17 use Monolog\Processor\IntrospectionProcessor;
41 private $monologLogger =
null;
42 private $level = Logger::ERROR;
44 private static $defaultLevel = Logger::ERROR;
45 private static $logTarget =
'';
46 private static $levels = [];
54 if (strlen($configFile) > 0) {
55 self::configure($configFile);
56 if (!$this->isStreamTarget(self::$logTarget)) {
58 self::$logTarget = $fileUtil->realpath(WCMF_BASE.self::$logTarget).
'/';
59 $fileUtil->mkdirRec(self::$logTarget);
62 $level = isset(self::$levels[$name]) ? self::$levels[$name] : self::$defaultLevel;
63 $this->level = $level;
65 $output =
"[%datetime%] %level_name%: %channel%:%extra.line%: %message%\n";
66 $formatter =
new LineFormatter($output,
null,
true);
67 $processor =
new IntrospectionProcessor($level, [__CLASS__]);
68 if ($this->isStreamTarget(self::$logTarget)) {
69 $handler =
new StreamHandler(self::$logTarget, $level);
72 $handler =
new RotatingFileHandler(self::$logTarget.
'.log', 0, $level);
73 $handler->setFilenameFormat(
'{date}',
'Y-m-d');
75 $handler->setFormatter($formatter);
76 $handler->pushProcessor($processor);
78 $this->monologLogger =
new Logger($name, [$handler]);
84 public function debug($message) {
85 $this->monologLogger->addDebug($this->prepareMessage($message));
91 public function info($message) {
92 $this->monologLogger->addInfo($this->prepareMessage($message));
98 public function warn($message) {
99 $this->monologLogger->addWarning($this->prepareMessage($message));
106 $this->monologLogger->addError($this->prepareMessage($message));
113 $this->monologLogger->addCritical($this->prepareMessage($message));
120 return $this->level <= Logger::DEBUG;
127 return $this->level <= Logger::INFO;
134 return $this->level <= Logger::WARNING;
141 return $this->level <= Logger::ERROR;
148 return $this->level <= Logger::CRITICAL;
162 private function configure($configFile) {
163 if (!file_exists($configFile)) {
166 $config = parse_ini_file($configFile,
true);
169 if (isset($config[self::ROOT_SECTION_NAME])) {
171 self::$defaultLevel = isset($rootConfig[
'level']) ?
172 constant(
'Monolog\Logger::'.strtoupper($rootConfig[
'level'])) :
174 self::$logTarget = isset($rootConfig[
'target']) ?
175 $rootConfig[
'target'] : WCMF_BASE.self::$logTarget;
179 self::$levels = isset($config[self::LOGGER_SECTION_NAME]) ?
181 foreach (self::$levels as $key => $val) {
182 self::$levels[$key] = constant(
'Monolog\Logger::'.strtoupper($val));
191 private function prepareMessage($message) {
192 return is_string($message) ? $message :
193 (is_object($message) && method_exists($message,
'__toString') ? $message->__toString() :
202 private function isStreamTarget($target) {
203 return preg_match(
'/^.+?:\/\//', $target);