| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243 |
- <?php
- namespace ThurData\Servers\KerioEmail\Core\HandlerError;
- use \Monolog\Formatter\LineFormatter;
- use \Monolog\Handler\StreamHandler;
- use \ThurData\Servers\KerioEmail\Core\Interfaces\LoggerInterface;
- use \ThurData\Servers\KerioEmail\Core\HandlerError\ErrorCodes\ErrorCodesLib;
- use \ThurData\Servers\KerioEmail\Core\HandlerError\Exceptions\Exception;
- /**
- * Description of Logger
- *
- * @author Rafal Ossowski <rafal.os@thurdata.com>
- */
- class Logger implements LoggerInterface
- {
- /**
- * @var \ThurData\Servers\KerioEmail\Core\HandlerError\Logger
- */
- protected static $instance;
- protected $name;
- /**
- * @var \Monolog\Logger
- */
- protected $logger;
- protected $mainPath = null;
- protected $handlers = [];
- /**
- * @param string $name
- * @param string $debugName
- * @param string $warningName
- * @param string $errorName
- */
- protected function __construct(
- $name = '', $debugName = '', $warningName = '', $errorName = ''
- )
- {
- // $this->name = $name;
- // $this->mainPath = ModuleConstants::getModuleRootDir() . DS . 'storage' . DS . 'logs' . DS;
- // if (is_dir($this->mainPath) === false)
- // {
- // mkdir($this->mainPath);
- // }
- // $this->logger = new \Monolog\Logger($this->name);
- //
- // foreach ([
- // $this->mainPath . $debugName => \Monolog\Logger::DEBUG,
- // $this->mainPath . $warningName => \Monolog\Logger::WARNING,
- // $this->mainPath . $errorName => \Monolog\Logger::ERROR
- // ] as $path => $status)
- // {
- // if (file_exists($path) === false)
- // {
- // $myfile = fopen($path, "w");
- // fclose($myfile);
- // File::setPermission($path);
- // }
- // $this->handlers[] = $this->buildHandlar($path, $status);
- // }
- //
- // $this->addHandlerToLogger();
- }
-
- private function __clone()
- {
-
- }
- public function isLoggerExist()
- {
- return isset($this->logger);
- }
-
- public function createLogger()
- {
- $this->logger = new \Monolog\Logger($this->name);
- //$this->addHandlerToLogger();
- return $this;
- }
- /**
- * @param string $name
- * @param array $arguments
- * @return mixed
- * @throws \ThurData\Servers\KerioEmail\Core\HandlerError\Exceptions\Exception
- */
- public function __call($name, $arguments)
- {
- if (method_exists($this->logger, $name))
- {
- return $this->logger->{$name}(
- (isset($arguments[0]) ? $arguments[0] : ''), (isset($arguments[1]) ? $arguments[1] : [])
- );
- }
-
- throw new Exception(ErrorCodesLib::CORE_LOG_000001, ['functionName' => $name]);
- }
-
- /**
- * @param string $message
- * @param array $context
- * @return bool
- */
- public function debug($message, array $context = [])
- {
- //return $this->logger->debug($message, $context);
- }
-
- /**
- * @param string $message
- * @param array $context
- * @return bool
- */
- public function error($message, array $context = [])
- {
- //return $this->logger->error($message, $context);
- }
-
- /**
- * @param string $message
- * @param array $context
- * @return bool
- */
- public function warning($message, array $context = [])
- {
- //return $this->logger->warning($message, $context);
- }
-
- /**
- * @param string $message
- * @param array $context
- * @return bool
- */
- public function err($message, array $context = [])
- {
- //return $this->logger->err($message, $context);
- }
-
- /**
- * @param string $message
- * @param array $context
- * @return bool
- */
- public function warn($message, array $context = [])
- {
- //return $this->logger->warn($message, $context);
- }
-
- /**
- * @param string $message
- * @param array $context
- * @return bool
- */
- public function addDebug($message, array $context = [])
- {
- //return $this->logger->addDebug($message, $context);
- }
-
- /**
- * @param string $message
- * @param array $context
- * @return bool
- */
- public function addWarning($message, array $context = [])
- {
- //return $this->logger->addWarning($message, $context);
- }
-
- /**
- * @param string $message
- * @param array $context
- * @return bool
- */
- public function addError($message, array $context = [])
- {
- //return $this->logger->addError($message, $context);
- }
- private function addHandlerToLogger()
- {
- $formatter = $this->getFormatter();
- foreach ($this->handlers as $handler)
- {
- $handler->setFormatter($formatter);
- $this->logger->pushHandler($handler);
- }
- }
- private function buildHandlar($path, $type)
- {
- return new StreamHandler($path, $type);
- }
- /**
- * @return LineFormatter
- */
- private function getFormatter()
- {
- return new LineFormatter(null, null, false, true);
- }
- /**
- * @param string $name
- * @param string $debugName
- * @param string $warningName
- * @param string $errorName
- * @return \ThurData\Servers\KerioEmail\Core\HandlerError\Logger
- */
- protected static function create(
- $name = 'default', $debugName = 'debug.log', $warningName = 'warning.log', $errorName = 'error.log'
- )
- {
- return new static($name, $debugName, $warningName, $errorName);
- }
- /**
- * @param string $name
- * @param string $debugName
- * @param string $warningName
- * @param string $errorName
- * @return \ThurData\Servers\KerioEmail\Core\HandlerError\Logger
- */
- public static function get(
- $name = 'default', $debugName = 'debug.log', $warningName = 'warning.log', $errorName = 'error.log'
- )
- {
- if (!isset(self::$instance))
- {
- self::$instance = self::create($name, $debugName, $warningName, $errorName);
- }
-
- if (self::$instance->isLoggerExist())
- {
- self::$instance->createLogger();
- }
-
- return self::$instance;
- }
- }
|