Logger.php 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. <?php
  2. namespace ThurData\Servers\KerioEmail\Core\HandlerError;
  3. use \Monolog\Formatter\LineFormatter;
  4. use \Monolog\Handler\StreamHandler;
  5. use \ThurData\Servers\KerioEmail\Core\Interfaces\LoggerInterface;
  6. use \ThurData\Servers\KerioEmail\Core\HandlerError\ErrorCodes\ErrorCodesLib;
  7. use \ThurData\Servers\KerioEmail\Core\HandlerError\Exceptions\Exception;
  8. /**
  9. * Description of Logger
  10. *
  11. * @author Rafal Ossowski <rafal.os@thurdata.com>
  12. */
  13. class Logger implements LoggerInterface
  14. {
  15. /**
  16. * @var \ThurData\Servers\KerioEmail\Core\HandlerError\Logger
  17. */
  18. protected static $instance;
  19. protected $name;
  20. /**
  21. * @var \Monolog\Logger
  22. */
  23. protected $logger;
  24. protected $mainPath = null;
  25. protected $handlers = [];
  26. /**
  27. * @param string $name
  28. * @param string $debugName
  29. * @param string $warningName
  30. * @param string $errorName
  31. */
  32. protected function __construct(
  33. $name = '', $debugName = '', $warningName = '', $errorName = ''
  34. )
  35. {
  36. // $this->name = $name;
  37. // $this->mainPath = ModuleConstants::getModuleRootDir() . DS . 'storage' . DS . 'logs' . DS;
  38. // if (is_dir($this->mainPath) === false)
  39. // {
  40. // mkdir($this->mainPath);
  41. // }
  42. // $this->logger = new \Monolog\Logger($this->name);
  43. //
  44. // foreach ([
  45. // $this->mainPath . $debugName => \Monolog\Logger::DEBUG,
  46. // $this->mainPath . $warningName => \Monolog\Logger::WARNING,
  47. // $this->mainPath . $errorName => \Monolog\Logger::ERROR
  48. // ] as $path => $status)
  49. // {
  50. // if (file_exists($path) === false)
  51. // {
  52. // $myfile = fopen($path, "w");
  53. // fclose($myfile);
  54. // File::setPermission($path);
  55. // }
  56. // $this->handlers[] = $this->buildHandlar($path, $status);
  57. // }
  58. //
  59. // $this->addHandlerToLogger();
  60. }
  61. private function __clone()
  62. {
  63. }
  64. public function isLoggerExist()
  65. {
  66. return isset($this->logger);
  67. }
  68. public function createLogger()
  69. {
  70. $this->logger = new \Monolog\Logger($this->name);
  71. //$this->addHandlerToLogger();
  72. return $this;
  73. }
  74. /**
  75. * @param string $name
  76. * @param array $arguments
  77. * @return mixed
  78. * @throws \ThurData\Servers\KerioEmail\Core\HandlerError\Exceptions\Exception
  79. */
  80. public function __call($name, $arguments)
  81. {
  82. if (method_exists($this->logger, $name))
  83. {
  84. return $this->logger->{$name}(
  85. (isset($arguments[0]) ? $arguments[0] : ''), (isset($arguments[1]) ? $arguments[1] : [])
  86. );
  87. }
  88. throw new Exception(ErrorCodesLib::CORE_LOG_000001, ['functionName' => $name]);
  89. }
  90. /**
  91. * @param string $message
  92. * @param array $context
  93. * @return bool
  94. */
  95. public function debug($message, array $context = [])
  96. {
  97. //return $this->logger->debug($message, $context);
  98. }
  99. /**
  100. * @param string $message
  101. * @param array $context
  102. * @return bool
  103. */
  104. public function error($message, array $context = [])
  105. {
  106. //return $this->logger->error($message, $context);
  107. }
  108. /**
  109. * @param string $message
  110. * @param array $context
  111. * @return bool
  112. */
  113. public function warning($message, array $context = [])
  114. {
  115. //return $this->logger->warning($message, $context);
  116. }
  117. /**
  118. * @param string $message
  119. * @param array $context
  120. * @return bool
  121. */
  122. public function err($message, array $context = [])
  123. {
  124. //return $this->logger->err($message, $context);
  125. }
  126. /**
  127. * @param string $message
  128. * @param array $context
  129. * @return bool
  130. */
  131. public function warn($message, array $context = [])
  132. {
  133. //return $this->logger->warn($message, $context);
  134. }
  135. /**
  136. * @param string $message
  137. * @param array $context
  138. * @return bool
  139. */
  140. public function addDebug($message, array $context = [])
  141. {
  142. //return $this->logger->addDebug($message, $context);
  143. }
  144. /**
  145. * @param string $message
  146. * @param array $context
  147. * @return bool
  148. */
  149. public function addWarning($message, array $context = [])
  150. {
  151. //return $this->logger->addWarning($message, $context);
  152. }
  153. /**
  154. * @param string $message
  155. * @param array $context
  156. * @return bool
  157. */
  158. public function addError($message, array $context = [])
  159. {
  160. //return $this->logger->addError($message, $context);
  161. }
  162. private function addHandlerToLogger()
  163. {
  164. $formatter = $this->getFormatter();
  165. foreach ($this->handlers as $handler)
  166. {
  167. $handler->setFormatter($formatter);
  168. $this->logger->pushHandler($handler);
  169. }
  170. }
  171. private function buildHandlar($path, $type)
  172. {
  173. return new StreamHandler($path, $type);
  174. }
  175. /**
  176. * @return LineFormatter
  177. */
  178. private function getFormatter()
  179. {
  180. return new LineFormatter(null, null, false, true);
  181. }
  182. /**
  183. * @param string $name
  184. * @param string $debugName
  185. * @param string $warningName
  186. * @param string $errorName
  187. * @return \ThurData\Servers\KerioEmail\Core\HandlerError\Logger
  188. */
  189. protected static function create(
  190. $name = 'default', $debugName = 'debug.log', $warningName = 'warning.log', $errorName = 'error.log'
  191. )
  192. {
  193. return new static($name, $debugName, $warningName, $errorName);
  194. }
  195. /**
  196. * @param string $name
  197. * @param string $debugName
  198. * @param string $warningName
  199. * @param string $errorName
  200. * @return \ThurData\Servers\KerioEmail\Core\HandlerError\Logger
  201. */
  202. public static function get(
  203. $name = 'default', $debugName = 'debug.log', $warningName = 'warning.log', $errorName = 'error.log'
  204. )
  205. {
  206. if (!isset(self::$instance))
  207. {
  208. self::$instance = self::create($name, $debugName, $warningName, $errorName);
  209. }
  210. if (self::$instance->isLoggerExist())
  211. {
  212. self::$instance->createLogger();
  213. }
  214. return self::$instance;
  215. }
  216. }