thinkphp 建单独的日志目录

文件:thinkphp/library/think/Log.php

 

/**
     * [payLog 添加日志log1]
     * @param  [type] $mark        [备注]
     * @param  [type] $log_content [内容]
     * @param  string $keyp        [名]
     * @return [type]              [description]
     */
    public static function mylog($mark, $log_content, $keyp = "")
    {
        $max_size = 30000000;
        $file_path = dirname(ROOT_PATH) . '/runtime/tlogs/' . date('Ymd');
        if (!file_exists(dirname($file_path))) {
            mkdir(dirname($file_path), 0755, true);
        }
        if (!file_exists($file_path)) {
            mkdir($file_path, 0755, true);
        }

        if ($keyp == "") {
            $log_filename = $file_path . '/' . date('Ym-d') . ".log";
        } else {
            $log_filename = $file_path . '/' . $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 = json_encode($log_content, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES);
        }

        file_put_contents($log_filename, '   ' . $d->format('Y-m-d H:i:s u') . " key:" . $mark . "\r\n" . $log_content . "\r\n------------------------ --------------------------\r\n", FILE_APPEND);
    }

 

 

调用:

use think\facade\Log;

Log::mylog('说明:', $input, '文件名');

 

posted @ 2022-03-10 11:42  张志健  阅读(340)  评论(0编辑  收藏  举报