php记录日志的实现代码
程序员文章站
2022-12-07 08:26:52
复制代码 代码如下: $ss_log_level = 0; $ss_log_filename = /tmp/ss-log; $ss_log_levels = arr...
复制代码 代码如下:
$ss_log_level = 0;
$ss_log_filename = /tmp/ss-log;
$ss_log_levels = array(
none => 0,
error => 1,
info => 2,
debug => 3
);
//设置日志级别
function ss_log_set_level ($level = error) {
global $ss_log_level;
$ss_log_level = $level;
}
//记录日志
function ss_log ($level, $message) { global $ss_log_level, $ss-log-filename;
if ($ss_log_levels[$ss_log_level] < $ss_log_levels[$level]) {
// 不显示log信息
return false;
}
$fd = fopen($ss_log_filename, "a+");
fputs($fd, $level. - [.ss_times*****p_pretty().] - .$message."n");
fclose($fd);
return true;
}
//清空日志
function ss_log_reset () {
global $ss_log_filename;
@unlink($ss_log_filename);
}
//调用示例
复制代码 代码如下:
ss_log_set_level(info);
ss_log(error, "testing level error");
ss_log(info, "testing level info");
ss_log(debug, "testing level debug");
ss_log_reset();