thinkphp日志记录到文件

日志

php 复制代码
//控制器中
//这种方法调用的话,在general_technology下按照日期写入日志
LogService::requestLog('general_technology',$this->baseUrl .$url,$params,$res);
LogService::responseLog('general_technology/hebei_product_add_error', $syncData,$msg);
LogService::responseLog('general_technology/hebei_product_add_error', $syncData,$exception->getMessage());


//lOGservice

<?php

namespace app\common\service;

class LogService
{
    /**
     * 三方对接请求记录
     * @param string $apiUserId
     * @param        $msg
     * @param array  $data
     * @return void
     */
    public static function apiUserLog(string $apiUserId, $msg, array $data = [])
    {
        if (!is_string($msg) && empty($data)) {
            $data = $msg;
            $msg = '';
        }

        file_put_contents(self::buildFilePath("apiUser/{$apiUserId}", true, true), date('YmdHis') . "\t" . request()->ip() . "\t" . request()->method() . "\t" . request()->url(true) . "\t" . $msg . "\t" . json_encode($data, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES) . PHP_EOL, FILE_APPEND);
    }

    /**
     * 三方对接主动发起请求记录
     * @param string $apiUserId
     * @param string $url
     * @param        $params
     * @param        $response
     * @return void
     */
    public static function apiUserRequestLog(string $apiUserId, string $url, $params, $response)
    {
        $params = is_string($params) ? $params : json_encode($params, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES);
        $response = is_string($response) ? $response : json_encode($response, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES);

        file_put_contents(self::buildFilePath("apiUser/{$apiUserId}", 'request.log', true), date('YmdHis') . "\t$url\t$params\t{$response}" . PHP_EOL, FILE_APPEND);
    }

    /**
     * 通用请求外部记录
     * @param string       $filePath
     * @param string       $url
     * @param string|array $params
     * @param string|array $response
     * @return void
     */
    public static function requestLog(string $filePath, string $url, $params, $response)
    {
        is_string($params) || $params = json_encode($params, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES);
        is_string($response) || $response = json_encode($response, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES);

        file_put_contents(self::buildFilePath($filePath, 'request.log', true), date('YmdHis') . "\t$url\t$params\t{$response}" . PHP_EOL, FILE_APPEND);
    }

    /**
     * 通用外部请求记录
     * @param string       $filePath 日志文件路径
     * @param string|array $params   请求参数
     * @param string       $msg      其他消息
     * @return void
     */
    public static function responseLog(string $filePath, $params, string $msg = '')
    {
        is_string($params) || $params = json_encode($params, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES);

        file_put_contents(self::buildFilePath($filePath, true, true), date('YmdHis') . "\t" . request()->ip() . "\t" . request()->method() . "\t" . request()->url(true) . "\t$params\t$msg" . PHP_EOL, FILE_APPEND);
    }

    /**
     * 通用日志记录
     * @param string       $path     目录
     * @param string|bool  $fileName 文件名(true按日志切割)
     * @param string|mixed $msg      不为字符串时作为data处理
     * @param mixed        $data
     * @return void
     */
    public static function common(string $path, $fileName, $msg, $data = [])
    {
        $isDateSplit = is_bool($fileName);
        if ($isDateSplit) {
            $fileName = true;
        }

        if (!is_string($msg) && empty($data)) {
            $data = $msg;
            $msg = '';
        }

        $calledInfo = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS, 1)[0] ?? [];
        $calledInfo = ($calledInfo['file'] ?? '' ? str_replace(ROOT_PATH, '', $calledInfo['file']) : '') . ($calledInfo['type'] ?? '') . ($calledInfo['line'] ?? '');

        file_put_contents(self::buildFilePath($path, $fileName, $isDateSplit), date('YmdHis') . "\t{$calledInfo}\t{$msg}\t" . json_encode($data, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES) . PHP_EOL, FILE_APPEND);
    }

    /**
     * 构建日志文件路径
     * @param string      $path        目录
     * @param string|bool $fileName    文件名(true自动生成,false返回目录路径)
     * @param bool        $isDateSplit 是否按日期切割目录
     * @return string
     */
    public static function buildFilePath(string $path, $fileName = false, bool $isDateSplit = false): string
    {
        $path = RUNTIME_PATH . 'logService' . DS . str_replace('/', DS, trim($path ?: 'default', '/'));

        $isDateSplit && $path .= DS . date('Ym');

        is_dir($path) || @mkdir($path, 0777, true);

        if ($fileName === false) {
            return $path;
        } elseif (is_string($fileName)) {
            $isDateSplit && $fileName = date('d') . ($fileName ? ('_' . $fileName) : '');

            return $path . DS . $fileName;
        } else {
            $fileName = ($isDateSplit ? date('d') : date('Ymd')) . '.log';

            return $path . DS . $fileName;
        }
    }
}
相关推荐
Q_Q196328847527 分钟前
python+django/flask基于深度学习的个性化携程美食数据推荐系统
spring boot·python·深度学习·django·flask·node.js·php
Q_Q5110082852 小时前
python+django/flask的篮球馆/足球场地/运动场地预约系统
spring boot·python·django·flask·node.js·php
Q_Q5110082852 小时前
python+django/flask的城市供水管网爆管预警系统-数据可视化
spring boot·python·django·flask·node.js·php
悠悠~飘4 小时前
php基础-系统函数-第15天
php
laocaibulao15 小时前
mac电脑composer命令如何指定PHP版本
macos·php·composer
zz-zjx15 小时前
云原生LVS+Keepalived高可用方案(二)
开发语言·php·lvs
网络精创大傻15 小时前
PHP 与 Node.js:实际性能对比
开发语言·node.js·php
KevinLyu15 小时前
PHP内核详解· 内存管理篇(七)· 调整内存块大小
php
hj104319 小时前
php上传企业微信附件的方法
开发语言·php·企业微信
游九尘20 小时前
在 PHP 中打印数据(调试、输出内容)
php