123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189 |
- <?php
- /**
- * 记录日志
- ***/
- defined('InShopNC') or exit('Access Invalid!');
- class scope_trace
- {
- private $mTag;
- public function __construct($tag)
- {
- $this->mTag = $tag;
- Log::record("{$this->mTag} begin----------------------------",Log::DEBUG);
- }
- public function __destruct()
- {
- Log::record("{$this->mTag} end ----------------------------",Log::DEBUG);
- }
- }
- 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;
- const cur_level = self::DEBUG;
- private static $log = array();
- private static $sqlog = false;
- public static function start_sql_log() {
- self::$sqlog = array();
- }
- public static function sql_log()
- {
- if(is_array(self::$sqlog)) {
- return self::$sqlog;
- } else {
- return [];
- }
- }
- public static function end_sql_log() {
- self::$sqlog = false;
- }
- private static function add_sql_log($log)
- {
- if(is_array(self::$sqlog)) {
- self::$sqlog[] = $log;
- }
- }
- ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
- static private $cur_path_file_name = '';
- static private $cur_path_file;
- public static function record_path($content)
- {
- $path_file = BASE_DATA_PATH . '/log/' . date('Ymd', time()) . '_path.log';
- if(self::$cur_path_file_name != $path_file)
- {
- if(self::$cur_path_file != null) {
- fclose(self::$cur_path_file);
- }
- self::$cur_path_file = fopen($path_file,'a');
- }
- if (@flock(self::$cur_path_file, LOCK_EX)) {
- fwrite(self::$cur_path_file,$content);
- fwrite(self::$cur_path_file,"\r\n");
- @flock(self::$cur_path_file, LOCK_UN);
- }
- }
- ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
- public static function record($message, $lev = self::ERR)
- {
- $now = @date('Y-m-d H:i:s', time());
- $pid = posix_getpid();
- if($lev == self::WAIT_HANDLE) {
- $level = 'WAIT_HANDLE';
- $log_file = BASE_DATA_PATH.'/log/'.date('Ymd',time()).'-wait.log';
- $content = "[{$pid} {$now}] {$level}: {$message}\r\n";
- file_put_contents($log_file,$content, FILE_APPEND);
- return;
- }
- if($lev == self::SQL) {
- $level = 'SQL';
- if(self::open_sql) {
- self::write($message,$level);
- }
- return;
- }
- if($lev >= self::cur_level && $lev <= self::RUN) {
- $level = self::get_level($lev);
- self::write($message,$level);
- }
- if($lev == self::ERR) {
- self::msg();
- }
- }
- private static $cur_file_name;
- private static $cur_file = null;
- private static function write($message,$level)
- {
- $now = @date('Y-m-d H:i:s', time());
- $pid = posix_getpid();
- $log_file = BASE_DATA_PATH . '/log/' . date('Ymd', time()) . '.log';
- if(self::$cur_file_name != $log_file)
- {
- if(self::$cur_file != null) {
- fclose(self::$cur_file);
- }
- self::$cur_file_name = $log_file;
- self::$cur_file = fopen($log_file,'a+');
- chmod($log_file,0777);
- }
- $content = "[{$pid} {$now}] {$level}: {$message}\r\n";
- $ret = fwrite(self::$cur_file,$content);
- if($ret === false) {
- self::$cur_file = fopen($log_file,'a+');
- chmod($log_file,0777);
- fwrite(self::$cur_file,$content);
- }
- }
- 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()).'.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;
- }
- }
|