Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- namespace application\components;
- use \Monolog\Handler\NullHandler;
- use \Monolog\Handler\StreamHandler;
- use \Monolog\Formatter\LineFormatter;
- use \Phoenix\ErrorsLog\RavenHandler;
- use \Phoenix\ErrorsLog\DbHandler;
- use \Phoenix\ErrorsLog\DbPaymentHandler;
- use \Psr\Log\LogLevel as LogLevel;
- class Logger extends \CComponent implements \Psr\Log\LoggerInterface
- {
- private $loggers = [];
- private $defaultName = 'php';
- private $sentryStatus = false;
- private $sentrySkipPercent = 0;
- private $allowDebugLevel = true;
- private $allowInfoLevel = true;
- private $doubleLogging = false;
- private $lowPriorityLevels = [
- 'error',
- 'warning',
- 'notice',
- 'info',
- 'debug'
- ];
- private $maxDepth = 4;
- const EVENT_LOG_FILE = 'event.log';
- public function __construct()
- {
- }
- public function setSentryStatus($sentryStatus)
- {
- $this->sentryStatus = $sentryStatus;
- }
- public function setSentrySkipPercent($sentrySkipPercent)
- {
- $this->sentrySkipPercent = $sentrySkipPercent;
- }
- public function setAllowDebugLevel($allowDebugLevel)
- {
- $this->allowDebugLevel = $allowDebugLevel;
- }
- public function setAllowInfoLevel(bool $allowInfoLevel)
- {
- $this->allowInfoLevel = $allowInfoLevel;
- }
- public function setDoubleLogging($doubleLogging)
- {
- $this->doubleLogging = $doubleLogging;
- }
- public function setMaxDepth($depth)
- {
- $this->maxDepth = $depth;
- }
- public function init()
- {
- }
- private function loggerInstance($loggerName)
- {
- if (!isset($this->loggers[$loggerName])) {
- $logger = new \Monolog\Logger($loggerName);
- if ($this->sentryStatus) {
- $ravenClient = \application\components\ErrorLogger::getInstance()->getLoggerClient();
- $app = \Yii::app();
- if (empty($ravenClient) && !empty($app)) {
- $ravenClient = \Yii::app()->dic['sentryClient'];
- }
- if (!empty($ravenClient)) {
- $ravenClient->setLoggerName($loggerName);
- $ravenHandler = new RavenHandler($ravenClient);
- $logger->pushHandler($ravenHandler);
- } else {
- $nullHandler = new NullHandler();
- $logger->pushHandler($nullHandler);
- }
- if ($this->doubleLogging) {
- $dbHandler = new DbHandler();
- $logger->pushHandler($dbHandler);
- }
- } else {
- $dbHandler = new DbHandler();
- $logger->pushHandler($dbHandler);
- }
- $dbHandler = new DbPaymentHandler();
- $logger->pushHandler($dbHandler);
- $this->loggers[$loggerName] = $logger;
- }
- return $this->loggers[$loggerName];
- }
- private function arrayCopy($array, $maxDepth = -1)
- {
- if (is_object($array) && $maxDepth === 0) {
- return '[object]';
- }
- if (!is_array($array)) {
- return $array;
- }
- if ($maxDepth === 0) {
- return '[array]';
- }
- $result = [];
- foreach ($array as $k => $v) {
- if (is_object($v)) {
- $result[$k] = '[Class] ' . get_class($v);
- } elseif (is_array($v)) {
- $result[$k] = $this->arrayCopy($v, $maxDepth - 1);
- } else {
- $result[$k] = $v;
- }
- }
- return $result;
- }
- /**
- * Logs with an arbitrary level.
- *
- * @param mixed $level
- * @param string $message
- * @param array $context
- * @return null
- */
- public function log($level, $message, array $context = array())
- {
- if ($this->sentryStatus
- && $this->sentrySkipPercent > 0
- && in_array($level, $this->lowPriorityLevels)
- && (rand(1, 10000) / 100) <= $this->sentrySkipPercent
- ) {
- return;
- }
- if (!$this->allowDebugLevel && $level === 'debug') {
- return;
- }
- if (!$this->allowInfoLevel && $level === 'info') {
- return;
- }
- if (!empty($context['error_source'])) {
- $loggerName = $context['error_source'];
- } else {
- $loggerName = $this->defaultName;
- }
- if ((empty($context['file']) && empty($context['line'])) || empty($context['backtrace'])) {
- $errorTrace = debug_backtrace();
- foreach ($errorTrace as $errorTraceDepth => $errorTraceData) {
- if (isset($errorTraceData['function']) &&
- isset($errorTraceData['class']) &&
- ($errorTraceData['function'] == __FUNCTION__) &&
- ($errorTraceData['class'] == __CLASS__)
- ) {
- break;
- }
- }
- if (empty($context['file'])) {
- $errorFile = null;
- $errorLine = null;
- if (isset($errorTrace[$errorTraceDepth + 1])) {
- if (!empty($errorTrace[$errorTraceDepth + 1]['file'])) {
- $errorFile = $errorTrace[$errorTraceDepth + 1]['file'];
- }
- if (!empty($errorTrace[$errorTraceDepth + 1]['line'])) {
- $errorLine = $errorTrace[$errorTraceDepth + 1]['line'];
- }
- }
- $context['file'] = $errorFile;
- $context['line'] = $errorLine;
- }
- if (empty($context['backtrace'])) {
- if ($loggerName === $this->defaultName && !isset($context['fatal_error'])) {
- $backtrace = [];
- //dont store entire object, just class name
- foreach ($errorTrace as $traceRow) {
- $row = [
- 'file' => isset($traceRow['file']) ? $traceRow['file'] : null,
- 'line' => isset($traceRow['line']) ? $traceRow['line'] : null,
- 'type' => isset($traceRow['type']) ? $traceRow['type'] : null,
- 'class' => isset($traceRow['class']) ? $traceRow['class'] : null,
- 'function' => isset($traceRow['function']) ? $traceRow['function'] : null,
- ];
- if (isset($traceRow['args']) && is_array($traceRow['args'])) {
- $row['args'] = $this->arrayCopy($traceRow['args'], $this->maxDepth);
- }
- $backtrace[] = $row;
- }
- $context['backtrace'] = array_slice($backtrace, $errorTraceDepth + 1);
- } else {
- $context['backtrace'] = false;
- }
- }
- }
- $this->loggerInstance($loggerName)->log($level, $message, $context);
- }
- /**
- * System is unusable.
- *
- * @param string $message
- * @param array $context
- * @return null
- */
- public function emergency($message, array $context = array())
- {
- $this->log(LogLevel::EMERGENCY, $message, $context);
- }
- /**
- * Event on phoenix platform
- *
- * @param json_encode $message - array('type' => string, 'body' => array, 'eventAt' => string)
- * eventAt is optionality and use for past events
- * @return null
- */
- public function logEvent($message)
- {
- $logger = new \Monolog\Logger(__FUNCTION__);
- $streamHandler = new StreamHandler($this->getLogEventFilePath(), LogLevel::INFO, true, 0666);
- $output = "%datetime% %eventType% %body%\n";
- $formatter = new LineFormatter($output, 'Y-m-d H:i:s');
- $streamHandler->setFormatter($formatter);
- $logger->pushHandler($streamHandler);
- $logger->pushProcessor(function ($record) {
- $message = json_decode($record['message'], true);
- $record['eventType'] = $message['type'];
- $record['body'] = json_encode($message['body']);
- if (isset($message['eventAt'])) {
- $record['datetime'] = $message['eventAt'];
- }
- return $record;
- });
- $logger->addInfo($message);
- }
- public function getLogEventFilePath()
- {
- return \Yii::app()->basePath .'/runtime/logs/' . self::EVENT_LOG_FILE;
- }
- /**
- * Action must be taken immediately.
- *
- * Example: Entire website down, database unavailable, etc. This should
- * trigger the SMS alerts and wake you up.
- *
- * @param string $message
- * @param array $context
- * @return null
- */
- public function alert($message, array $context = array())
- {
- $this->log(LogLevel::ALERT, $message, $context);
- }
- /**
- * Critical conditions.
- *
- * Example: Application component unavailable, unexpected exception.
- *
- * @param string $message
- * @param array $context
- * @return null
- */
- public function critical($message, array $context = array())
- {
- $this->log(LogLevel::CRITICAL, $message, $context);
- }
- /**
- * Runtime errors that do not require immediate action but should typically
- * be logged and monitored.
- *
- * @param string $message
- * @param array $context
- * @return null
- */
- public function error($message, array $context = array())
- {
- $this->log(LogLevel::ERROR, $message, $context);
- }
- /**
- * Exceptional occurrences that are not errors.
- *
- * Example: Use of deprecated APIs, poor use of an API, undesirable things
- * that are not necessarily wrong.
- *
- * @param string $message
- * @param array $context
- * @return null
- */
- public function warning($message, array $context = array())
- {
- $this->log(LogLevel::WARNING, $message, $context);
- }
- /**
- * Normal but significant events.
- *
- * @param string $message
- * @param array $context
- * @return null
- */
- public function notice($message, array $context = array())
- {
- $this->log(LogLevel::NOTICE, $message, $context);
- }
- /**
- * Interesting events.
- *
- * Example: User logs in, SQL logs.
- *
- * @param string $message
- * @param array $context
- * @return null
- */
- public function info($message, array $context = array())
- {
- $this->log(LogLevel::INFO, $message, $context);
- }
- /**
- * Detailed debug information.
- *
- * @param string $message
- * @param array $context
- * @return null
- */
- public function debug($message, array $context = array())
- {
- $this->log(LogLevel::DEBUG, $message, $context);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement