123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134 |
- <?php
- /**
- * 日志
- *
- * @author mybsdc <[email protected]>
- * @date 2019/3/3
- * @time 12:01
- */
- namespace Luolongfei\Lib;
- use Monolog\Logger;
- use Monolog\Handler\StreamHandler;
- use Bramus\Monolog\Formatter\ColoredLineFormatter;
- class Log
- {
- /**
- * @var Logger
- */
- protected static $loggerInstance;
- /**
- * 由于php不能在类外使用已实例化的对象来访问静态属性,但可以在类外访问类里的静态方法,故定义此方法实现类外访问静态属性
- *
- * 注意,info等方法不写日志,error方法才写日志到指定目录
- *
- * @return Logger
- * @throws \Exception
- */
- public static function logger()
- {
- if (!self::$loggerInstance instanceof Logger) {
- $handler = new StreamHandler(
- config('debug') ? 'php://stdout' : sprintf('%s/logs/%s.log', ROOT_PATH, date('Y-m/d')),
- config('debug') ? Logger::DEBUG : Logger::INFO
- );
- if (config('debug')) {
- $handler->setFormatter(new ColoredLineFormatter(null, "[%datetime%] %channel%.%level_name%: %message%\n"));
- }
- $logger = new Logger('pusher');
- $logger->pushHandler($handler);
- self::$loggerInstance = $logger;
- }
- return self::$loggerInstance;
- }
- /**
- * @param $message
- * @param array $context
- *
- * @return bool
- * @throws \Exception
- */
- public static function debug($message, array $context = [])
- {
- return self::logger()->addDebug($message, $context);
- }
- /**
- * @param $message
- * @param array $context
- *
- * @return bool
- * @throws \Exception
- */
- public static function info($message, array $context = [])
- {
- return self::logger()->addInfo($message, $context);
- }
- /**
- * @param $message
- * @param array $context
- *
- * @return bool
- * @throws \Exception
- */
- public static function notice($message, array $context = [])
- {
- return self::logger()->addNotice($message, $context);
- }
- /**
- * @param $message
- * @param array $context
- *
- * @return bool
- * @throws \Exception
- */
- public static function warning($message, array $context = [])
- {
- return self::logger()->addWarning($message, $context);
- }
- /**
- * @param $message
- * @param array $context
- *
- * @return bool
- * @throws \Exception
- */
- public static function error($message, array $context = [])
- {
- return self::logger()->addError($message, $context);
- }
- /**
- * @param $message
- * @param array $context
- *
- * @return bool
- * @throws \Exception
- */
- public static function alert($message, array $context = [])
- {
- return self::logger()->addAlert($message, $context);
- }
- /**
- * @param $message
- * @param array $context
- *
- * @return bool
- * @throws \Exception
- */
- public static function emergency($message, array $context = [])
- {
- return self::logger()->addEmergency($message, $context);
- }
- }
|