123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129 |
- <?php
- /**
- * 记录日志
- ***/
- defined('InShopNC') or exit('Access Invalid!');
- class Log
- {
- const open_sql = true;
- const SQL = 1;
- const INFO = 2;
- const DEBUG = 3;
- const WARING = 4;
- const ERR = 5;
- const RUN = 6;
- const WAIT_HANDLE = 10;
- static $moduls = array("default","session");
- const cur_level = self::INFO;
- private static $log = array();
- private static $stsql_log = array();
- public static function start_sql_log()
- {
- self::$stsql_log = array();
- }
- public static function sql_log()
- {
- return self::$stsql_log;
- }
- private static function add_sql_log($log)
- {
- if(is_array(self::$stsql_log)) {
- array_push(self::$stsql_log,$log);
- }
- }
- //public static function record($message, $lev = self::ERR,$module = 'default')
- public static function record($message, $lev = self::ERR)
- {
- // if(!in_array($lev,self::$moduls) && $lev < self::ERR ) {
- // return;
- // }
- $now = @date('Y-m-d H:i:s', time());
- if($lev == self::WAIT_HANDLE) {
- $level = 'WAIT_HANDLE';
- $log_file = BASE_DATA_PATH.'/log/'.date('Ymd',time()).'-wait.log';
- $content = "[{$now}] {$level}: {$message}\r\n";
- file_put_contents($log_file,$content, FILE_APPEND);
- return;
- }
- if($lev == self::SQL) {
- $level = 'SQL';
- $content = "[{$now}] {$level}: {$message}\r\n";
- if(self::open_sql) {
- $log_file = BASE_DATA_PATH.'/log/'.date('Ymd',time()).'-sql.log';
- file_put_contents($log_file,$content, FILE_APPEND);
- }
- self::add_sql_log($message);
- return;
- }
- if($lev >= self::cur_level && $lev <= self::RUN) {
- $level = self::get_level($lev);
- $log_file = BASE_DATA_PATH . '/log/' . date('Ymd', time()) . '.log';
- $content = "[{$now}] {$level}: {$message}\r\n";
- file_put_contents($log_file, $content, FILE_APPEND);
- }
- if($lev == self::ERR) {
- self::msg();
- }
- }
- public static function endl($lev = self::ERR)
- {
- $content = "\r\n";
- if($lev == self::SQL && self::open_sql) {
- $log_file = BASE_DATA_PATH.'/log/'.date('Ymd',time()).'-sql.log';
- file_put_contents($log_file,$content, FILE_APPEND);
- return;
- }
- if($lev >= self::cur_level) {
- $log_file = BASE_DATA_PATH . '/log/' . date('Ymd', time()) . '.log';
- file_put_contents($log_file, $content, FILE_APPEND);
- }
- }
- public static function msg()
- {
- $debugInfo = debug_backtrace();
- $stack = "[";
- 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"];
- }
- }
- $stack .= "]";
- return $stack;
- }
- private static function get_level($lev)
- {
- if($lev == self::INFO) return 'INFO';
- if($lev == self::DEBUG) return 'DEBUG';
- if($lev == self::WARING) return 'WARING';
- if($lev == self::ERR) return 'ERR';
- if($lev == self::RUN) return 'RUN';
- return 'Unknown';
- }
- public static function read()
- {
- return self::$log;
- }
- }
|