打印log

 

入口文件

//日志记录配置
if (!defined('DS')) {
    define('DS', '/');
}
if(!defined('APP_PATH_LOG')){
    define('APP_PATH_LOG', LOG_PATH.'admin/');
}
if(!defined('LOG_LEVEL')){
    define('LOG_LEVEL', LOG_INFO);
}

 

function log_message($msg, $level = LOG_INFO, $group = '') {
    $args = func_get_args ();
    if (! (is_int ( $level ) && $level <= LOG_DEBUG && $level >= LOG_EMERG)) {
        return;
    }
    if (defined ( 'LOG_LEVEL' )) {
        $logLevel = LOG_LEVEL;
    } else {
        $logLevel = LOG_ERR;
    }
    $logTypes = array (
            LOG_DEBUG => 'DEBUG', //7
            LOG_INFO => 'INFO',     //6
            LOG_NOTICE => 'NOTICE',
            LOG_WARNING => 'WARNING',
            LOG_ERR => 'ERR',
            LOG_CRIT => 'CRIT',
            LOG_ALERT => 'ALERT',
            LOG_EMERG => 'EMERG'  //0
    );


    $logType = $logTypes [$level];
    //取调用者的方法名
    $backtrace = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS & DEBUG_BACKTRACE_PROVIDE_OBJECT, 2);

    // $msg=sprintf('[%s:%s] ', get_called_class(), ''). $msg; 
    if(!empty($backtrace) && isset($backtrace[1])){
        $backtrace_caller = $backtrace[1];
        $call_class_name = isset($backtrace_caller['class']) ? $backtrace_caller['class'] : '';
        // $call_line = isset($backtrace_caller['line']) ? $backtrace_caller['line'] : '';
        $call_function_name = isset($backtrace_caller['function']) ? $backtrace_caller['function'] : '';
         $msg=sprintf('[%s:%s] ', $call_class_name, $call_function_name). $msg; 
    }

    $logdir = LOG_PATH;
    if (defined ( 'APP_PATH_LOG' )) {
        $logdir = APP_PATH_LOG;
    }

    $tmp_msg = '['.date ( 'Y-m-d H:i:s' )."] [{$logType}]";
    if(!empty($group)){
        $tmp_msg.=" - [{$group}]";
    }
    $msg =  $tmp_msg." - {$msg}\r\n";

    //线上文件日志不记录 debug级别日志, 当前日志级别 <= 设定最级别
    if ($level <= $logLevel) {
        $fn = date ( 'Ymd' ) . ".log";

        $fileName = $logdir . DS . $fn;

        if (! is_dir ( $logdir )) {
            mkdir ( $logdir, 0755, TRUE );
        }
        $fileName = $logdir . DS . $fn;
        if (! file_exists ( $fileName )) {
            error_log ( $msg, 3, $fileName );
            @chmod ( $fileName, 0644 );
        } else {
            error_log ( $msg, 3, $fileName );
        }    

        if (defined ( 'LOG_STDOUT' )) {
            echo $msg;
        }
     }
}

 

log_message('specialInsertingData:'. json_encode($specialInsertingData),LOG_INFO);

 


posted @ 2016-09-06 14:35  午时的海  阅读(286)  评论(0编辑  收藏  举报