thinkphp5 使用日志记录
thinkphp5 简单使用日志记录
在目录文件找到 thinkphp/library/think/log.php
加入
public static function mylog($mark, $log_content, $keyp = "") {
$max_size = 30000000;
if ($keyp == "") {
$log_filename = RUNTIME_PATH . '/txt/' . date('Ym-d') . ".log";
} else {
$log_filename = RUNTIME_PATH . '/txt/' . $keyp . ".log";
}
if (file_exists($log_filename) && (abs(filesize($log_filename)) > $max_size)) {
rename($log_filename, dirname($log_filename) . DS . date('Ym-d-His') . $keyp . ".log");
}
$t = microtime(true);
$micro = sprintf("%06d", ($t - floor($t)) * 1000000);
$d = new \DateTime (date('Y-m-d H:i:s.' . $micro, $t));
if(is_array($log_content)){
$log_content = JSONReturn($log_content);
}
file_put_contents($log_filename, '' . $d->format('Y-m-d H:i:s') . " key:" . $mark . "-" . $log_content . "\r\n", FILE_APPEND);
}
#具体页面使用
先使用log日志的命名空间
use think\Log;
在具体方法中使用
$savename ='uid:'.$data['uid'].':ip:'.$data['ip'].;
Log::mylog('log', $savename , date('Ymd').'.log');