php项目流程中代码错误通过接口调用或者异步加载时流程中断或者异常开启错误提示不一定排查到,这时我们除了排查服务器日志还可以在源代码业务逻辑里打印日志,生产日志文件
php项目流程中代码错误通过接口调用或者异步加载时流程中断或者异常开启错误提示不一定排查到,这时我们除了排查服务器日志还可以在源代码业务逻辑里打印日志,生产日志文件,比如自定义日志方法或者自定义日志类等,以下简单示例:
比如生成的文件 如:20200424.log,用文本编辑器打开,内容如下:
[2020-04-24 10:29:19][DEBUG]=>Array
(
[code] => 40000
[msg] => 错误
)
自封装函数方法:
<?php
function loger($str=''){
is_array($str) && $str = print_r($str,true);
is_object($str) && $str = print_r($str,true);
$errorStr = '[' . date('Y-m-d H:i:s') . '][LOG] '.$str . PHP_EOL;
error_log($errorStr, 3, 'LOG' .date('Y_m_d') . '.log', 'extra');
}
?>
可以指定打印的日志路径:
function loger($str=''){
$path = './runtime/log/';
is_array($str) && $str = print_r($str,true);
is_object($str) && $str = print_r($str,true);
$errorStr = '[' . date('Y-m-d H:i:s') . '][LOG] '.$str . PHP_EOL;
error_log($errorStr, 3, $path.'LOG' .date('Y_m_d') . '.log', 'extra');
}
如果路径不存在可以自动创建,比如创建目录方法:
function createDirs($path){
if(!file_exists($path)){
createDirs(dirname($path));
mkdir($path, 0777);
}
}
结合一起可以:
<?php
// 定义路径
defined('LOG_PATH') or define('LOG_PATH', './public/log/');
// 创建路径
function createDirs($path){
if(!file_exists($path)){
createDirs(dirname($path));
mkdir($path, 0777);
}
}
// 打印日志
function logers($str){
if(!is_dir(LOG_PATH)){
createDirs($path)
}
is_array($str) && $str = print_r($str,true);
$errorStr = LOG_PATH.'[' . date('Y-m-d H:i:s') . '][LOG] '.$str . PHP_EOL;
error_log($errorStr, 3, 'LOG' .date('Y_m_d') . '.log', 'extra');
}
其他参考方式打印日志:
<?php
include 'log.php'; //引入log文件
$result = array('code'=>40000,'msg'=>'错误'); //假如这是程序业务逻辑返回的结果
loger($result); // 打印日志
?>
log.php 源码:
<?php
$path = './public/log/';
function createDirs($path){
if(!file_exists($path)){
createDirs(dirname($path));
mkdir($path, 0777);
}
}
createDirs($path);
define('OUTPUT_FILE',$path.date('Ymd').'.log'); //Output file.
/**
* The alias of pt() method, for more readable.
* @param string $s
*/
function vklog($s){
pt($s);
}
/**
* pt is short for print,this method is the main entry of the vk-log4php.
* @param string $s
*/
function pt($s){
pt_after_php430($s);
}
/**
* Log the string $s. for the php version before 4.3.0
* @param string $s
*/
function pt_before_php430($s){
$vk_debug_level = 1; //no use now , prepare for the future version.
if($vk_debug_level==1){
$outputString = '['.date('Y-m-d H:i:s').']=>'.$s."\n";// "\n" 是换行符。 结合tail -f c:/vk-debug-log.txt,即可得到即时调试信息。
vk_output($outputString);
}
}
/**
* Log the string $s for the php version after 4.3.0
* @param string $s
*/
function pt_after_php430($s){
$vk_debug_level = 1; //no use now , prepare for the future version.
if($vk_debug_level==1){
$traces = debug_backtrace(); //see the php manual for the function "debug_backtrace()" detail.
$traceTop = $traces[sizeof($traces)-1]; // get the top trace info arrary of the traces.
extract($traceTop); // This function extract the arrary to vars:
$outputString = '['.date('Y-m-d H:i:s').']=>'.$s."\n";// "\n" 是换行符。 结合tail -f c:/vk-debug-log.txt,即可得到即时调试信息。
vk_output($outputString);
}
}
function loger_r($s){
print_r($s);exit;
}
function loger_i($s){
var_dump($s);exit;
}
/**
* 默认记录登陆用户的userid
*/
function loger($s){
$vk_debug_level = 1;
//no use now , prepare for the future version.
$loglever='DEBUG';
$userid = '[zhl]';
$userid = '';
is_array($s) && $s = print_r($s,true); //print_r 增加 true 返回打印字符窜
if($vk_debug_level==1){
$traces = debug_backtrace();
//see the php manual for the function "debug_backtrace()" detail.
$traceTop = $traces[sizeof($traces)-1];
// get the top trace info arrary of the traces.
extract($traceTop);
// This function extract the arrary to vars:
$outputString = '['.date('Y-m-d H:i:s').']'.'['.$loglever.']'.$userid.'=>'.$s."\n";
// "\n" 是换行符。 结合tail -f c:/vk-debug-log.txt,即可得到即时调试信息。
vk_output($outputString);
}
}
function logers($s){
$vk_debug_level = 1;
//no use now , prepare for the future version.
$loglever='DEBUG';
is_array($s) && $s = print_r($s,true); //print_r 增加 true 返回打印字符窜
if($vk_debug_level==1){
$traces = debug_backtrace();
//see the php manual for the function "debug_backtrace()" detail.
$traceTop = $traces[sizeof($traces)-1];
// get the top trace info arrary of the traces.
extract($traceTop);
// This function extract the arrary to vars:
$outputString = $s;
// "\n" 是换行符。 结合tail -f c:/vk-debug-log.txt,即可得到即时调试信息。
vk_output($outputString);
}
}
/**
* 控制器loger输出
*/
function actionLoger($s){
$methodName = $_SERVER['REDIRECT_URL'];
loger($s, $methodName);
}
/**
* @param $outputString
* @return unknown_type
*/
function vk_output($outputString){
$fp = fopen(OUTPUT_FILE,'ab');
if(!$fp){
ob_start();
echo 'Can not open the log file: '.OUTPUT_FILE;
ob_end_flush();
exit;
}
fwrite($fp,$outputString);
}
?>