• 日志类


    <?php
    /**
     * 记录日志
     *
     * @package    library
     */
    defined('DYMall') or exit('Access Invalid!');
    define('BASE_DATA_PATH', '/data');
    
    class Log
    {
        const SQL = 'SQL';
        const ERR = 'ERR';
        const INFO = 'INFO';
        const RUN = 'RUN';
        private static $log = array();
        private static $logFile = "%s%s.log";
    
        //sql日志文件
        private static $sqlFile = 'sql_';
    
        /**
         * Notes:记录
         * User: Abel Yang
         * Date: 2020/5/30/030
         * Time: 15:40
         * @param $message
         * @param string $level
         */
        public static function record($message, $level = self::INFO)
        {
            $level = strtoupper($level);
            switch ($level) {
                case self::INFO:
                case self::ERR:
                case self::RUN:
                    self::info($message, $level);
                    break;
            }
        }
    
        /**
         * Notes:sql日志
         * User: Abel Yang
         * Date: 2020/5/30/030
         * Time: 16:46
         * @param $message
         */
        public static function sql($message){
            if (C('sql_debug')) {
                $content = "[" . self::getNow() . "] {$message}
    ";
                self::write($content, self::$sqlFile);
            }
        }
    
        public static function read()
        {
            return self::$log;
        }
    
        /**
         * Notes:普通日志
         * User: Abel Yang
         * Date: 2020/5/30/030
         * Time: 15:39
         * @param $message
         * @param $level
         */
        public static function info($message, $level = self::INFO)
        {
            self::write("[" . self::getNow() . "] " . self::getUrl() . "
    [{$level}]: {$message}
    ");
        }
    
        /**
         * Notes:错误日志
         * User: Abel Yang
         * Date: 2020/5/30/030
         * Time: 15:39
         * @param string $message
         */
        public static function error($message = "")
        {
            $stack = $message;
            $debugInfo = debug_backtrace();
            foreach ($debugInfo as $key => $val) {
                if (array_key_exists("file", $val)) $stack .= ",file:" . $val["file"];
                if (array_key_exists("line", $val)) $stack .= ",line:" . $val["line"];
                if (array_key_exists("function", $val)) $stack .= ",function:" . $val["function"];
            }
            self::info($stack, self::ERR);
        }
    
        private static function write($content, $filePrefix = "")
        {
            if (empty($content)) return;
    
            $logDir = BASE_DATA_PATH . '/log/';
            is_dir($logDir) ?: mkdir($logDir, 0777, true);
    
            $fileName = sprintf(self::$logFile, $filePrefix, date('Ymd'));
            $logFile = $logDir . $fileName;
            file_put_contents($logFile, "{$content}", FILE_APPEND);
        }
    
        private static function getNow()
        {
            list($usec, $sec) = explode(" ", microtime());
            return date('Y-m-d H:i:s', $sec) . " " . $usec;
        }
    
        private static function getUrl()
        {
            $url = $_SERVER['REQUEST_URI'] ? $_SERVER['REQUEST_URI'] : $_SERVER['PHP_SELF'];
            $url .= " ( app={$_GET['app']}&mod={$_GET['mod']} ) ";
            return $url;
        }
    }

    用法:

    $error = “报错信息”. PHP_EOL;
    Log::record($error, Log::ERR);

  • 相关阅读:
    AngularJs 控制台
    AngularJs 表单提交按钮状态
    Python3 import tensorflow 出现FutureWarning: Passing (type, 1) or '1type' 问题
    在windows 10 64位系统下安装TensorFlow
    AttributeError: module 'tensorflow' has no attribute 'Session'
    I tensorflow/core/platform/cpu_feature_guard.cc:142] Your CPU supports instructions that this TensorFlow binary was not compiled to use: AVX2
    解决vue多次提交
    解决pip安装时速度慢的问题
    Miniconda
    linux下安装anaconda
  • 原文地址:https://www.cnblogs.com/yszr/p/13516256.html
Copyright © 2020-2023  润新知