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;
        }
    }
}
相关推荐
xiangpanf1 小时前
PHP vs Python:30字看透两大语言差异
开发语言·php
xiangpanf2 小时前
PHP爬虫框架:Goutte vs Panther
开发语言·c++·vue.js·php
常利兵4 小时前
从0到1:搭建Spring Boot 3企业级认证授权平台
数据库·spring boot·php
t1987512819 小时前
使用深度神经网络解决无线网络资源分配问题的MATLAB实现
matlab·php·dnn
m0_4592524619 小时前
fastadmin动态渲染统计信息
开发语言·前端·javascript·php
AI成长日志20 小时前
【实用工具教程】Linux常用命令速查与实战场景:文件操作、进程管理与网络调试高频命令解析
linux·php
ccchen88820 小时前
适配帝国CMS 8.0:全新帝国CMS免登录采集发布插件
经验分享·爬虫·php·帝国cms自动采集发布插件·帝国cms8.0·帝国cms自动采集插件·帝国cms采集发布模块
vx-程序开发1 天前
springboot在线装修管理系统-计算机毕业设计源码56278
java·c语言·spring boot·python·spring·django·php
幽络源小助理1 天前
网页软件库源码(带1153条资源)-含详细搭建教程
php
博语小屋1 天前
多路转接select、poll
开发语言·网络·c++·php