首页 > 其他分享 >laravel: 日志配置

laravel: 日志配置

时间:2024-09-18 14:02:53浏览次数:11  
标签:laravel Pipeline Illuminate 配置 api 日志 php data

一,日志按天切分:

修改.env

root@lhdpc:/data/api# vi .env

指定LOG_CHANNEL值为daily即可,代码:

LOG_CHANNEL=daily

二,配置laravel日志中记录url/方法/参数

1,config/logging.php

        'daily' => [
            'driver' => 'daily',
            'path' => storage_path('logs/laravel.log'),
            'level' => env('LOG_LEVEL', 'debug'),
            'days' => env('LOG_DAILY_DAYS', 14),
            'replace_placeholders' => true,
            'process_psr7' => true,
            'tap' => [App\extend\logging\RequestLogHandler::class],
        ],

2,App\extend\logging\RequestLogHandler类:

<?php
namespace App\extend\logging;

use Illuminate\Log\Logger;
use Monolog\Formatter\LineFormatter;

class RequestLogHandler
{
    /**
     * 自定义给定的日志记录器实例。
     */
    public function __invoke(Logger $logger): void
    {

        foreach ($logger->getHandlers() as $handler) {
            $url = request()->fullUrl();
            $method = request()->method();
            $parameters = request()->all();

            $formatted = "{$method} {$url} with parameters: " . json_encode($parameters)."\n";

            $handler->setFormatter((new LineFormatter(
                '[%datetime%] %channel%.%level_name%: '.$formatted.' %message% %context% %extra%'."\n"
            ))->includeStacktraces());
        }
    }
}

3,查看日志中的效果:

[2024-09-18T13:49:54.339678+08:00] local.ERROR: POST http://192.168.219.6/api/virtual/valid?content=test1111 with parameters: {"content":"test1111"}
 Division by zero {"exception":"[object] (DivisionByZeroError(code: 0): Division by zero at /data/api/app/Http/Controllers/Controller.php:109)
[stacktrace]
#0 /data/api/vendor/laravel/framework/src/Illuminate/Routing/ControllerDispatcher.php(46): App\\Http\\Controllers\\Controller->valid()
#1 /data/api/vendor/laravel/framework/src/Illuminate/Routing/Route.php(260): Illuminate\\Routing\\ControllerDispatcher->dispatch()
#2 /data/api/vendor/laravel/framework/src/Illuminate/Routing/Route.php(206): Illuminate\\Routing\\Route->runController()
#3 /data/api/vendor/laravel/framework/src/Illuminate/Routing/Router.php(808): Illuminate\\Routing\\Route->run()
#4 /data/api/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(144): Illuminate\\Routing\\Router->Illuminate\\Routing\\{closure}()
#5 /data/api/vendor/laravel/framework/src/Illuminate/Routing/Middleware/SubstituteBindings.php(50): Illuminate\\Pipeline\\Pipeline->Illuminate\\Pipeline\\{closure}()
#6 /data/api/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(183): Illuminate\\Routing\\Middleware\\SubstituteBindings->handle()
#7 /data/api/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(119): Illuminate\\Pipeline\\Pipeline->Illuminate\\Pipeline\\{closure}()
#8 /data/api/vendor/laravel/framework/src/Illuminate/Routing/Router.php(807): Illuminate\\Pipeline\\Pipeline->then()
#9 /data/api/vendor/laravel/framework/src/Illuminate/Routing/Router.php(786): Illuminate\\Routing\\Router->runRouteWithinStack()
#10 /data/api/vendor/laravel/framework/src/Illuminate/Routing/Router.php(750): Illuminate\\Routing\\Router->runRoute()
#11 /data/api/vendor/laravel/framework/src/Illuminate/Routing/Router.php(739): Illuminate\\Routing\\Router->dispatchToRoute()
#12 /data/api/vendor/laravel/framework/src/Illuminate/Foundation/Http/Kernel.php(200): Illuminate\\Routing\\Router->dispatch()
#13 /data/api/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(144): Illuminate\\Foundation\\Http\\Kernel->Illuminate\\Foundation\\Http\\{closure}()
#14 /data/api/vendor/laravel/framework/src/Illuminate/Foundation/Http/Middleware/TransformsRequest.php(21): Illuminate\\Pipeline\\Pipeline->Illuminate\\Pipeline\\{closur
e}()
#15 /data/api/vendor/laravel/framework/src/Illuminate/Foundation/Http/Middleware/ConvertEmptyStringsToNull.php(31): Illuminate\\Foundation\\Http\\Middleware\\TransformsR
equest->handle()
#16 /data/api/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(183): Illuminate\\Foundation\\Http\\Middleware\\ConvertEmptyStringsToNull->handle()
#17 /data/api/vendor/laravel/framework/src/Illuminate/Foundation/Http/Middleware/TransformsRequest.php(21): Illuminate\\Pipeline\\Pipeline->Illuminate\\Pipeline\\{closur
e}()
#18 /data/api/vendor/laravel/framework/src/Illuminate/Foundation/Http/Middleware/TrimStrings.php(51): Illuminate\\Foundation\\Http\\Middleware\\TransformsRequest->handle
()
#19 /data/api/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(183): Illuminate\\Foundation\\Http\\Middleware\\TrimStrings->handle()
#20 /data/api/vendor/laravel/framework/src/Illuminate/Http/Middleware/ValidatePostSize.php(27): Illuminate\\Pipeline\\Pipeline->Illuminate\\Pipeline\\{closure}()
#21 /data/api/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(183): Illuminate\\Http\\Middleware\\ValidatePostSize->handle()
#22 /data/api/vendor/laravel/framework/src/Illuminate/Foundation/Http/Middleware/PreventRequestsDuringMaintenance.php(110): Illuminate\\Pipeline\\Pipeline->Illuminate\\P
ipeline\\{closure}()
#23 /data/api/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(183): Illuminate\\Foundation\\Http\\Middleware\\PreventRequestsDuringMaintenance->handle()
#24 /data/api/vendor/laravel/framework/src/Illuminate/Http/Middleware/HandleCors.php(62): Illuminate\\Pipeline\\Pipeline->Illuminate\\Pipeline\\{closure}()
#25 /data/api/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(183): Illuminate\\Http\\Middleware\\HandleCors->handle()
#26 /data/api/vendor/laravel/framework/src/Illuminate/Http/Middleware/TrustProxies.php(57): Illuminate\\Pipeline\\Pipeline->Illuminate\\Pipeline\\{closure}()
#27 /data/api/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(183): Illuminate\\Http\\Middleware\\TrustProxies->handle()
#28 /data/api/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(119): Illuminate\\Pipeline\\Pipeline->Illuminate\\Pipeline\\{closure}()
#29 /data/api/vendor/laravel/framework/src/Illuminate/Foundation/Http/Kernel.php(175): Illuminate\\Pipeline\\Pipeline->then()
#30 /data/api/vendor/laravel/framework/src/Illuminate/Foundation/Http/Kernel.php(144): Illuminate\\Foundation\\Http\\Kernel->sendRequestThroughRouter()
#31 /data/api/vendor/laravel/framework/src/Illuminate/Foundation/Application.php(1188): Illuminate\\Foundation\\Http\\Kernel->handle()
#32 /data/api/public/index.php(17): Illuminate\\Foundation\\Application->handleRequest()
#33 {main}
"} []

 

标签:laravel,Pipeline,Illuminate,配置,api,日志,php,data
From: https://www.cnblogs.com/architectforest/p/18418369

相关文章

  • docker配置镜像加速器
    1.一键设置镜像加速:修改文件/etc/docker/daemon.json(如果不存在则创建)vi/etc/docker/daemon.json2.修改JSON文件更改为以下内容然后保存{"registry-mirrors":["https://docker.registry.cyou","https://docker-cf.registry.cyou","https://dockercf......
  • vue 配置代理 及 axios 请求封装和使用
    一.配置代理- vue.config.js const{defineConfig}=require('@vue/cli-service')module.exports=defineConfig({ transpileDependencies:true, //配置代理服务器 devServer:{  proxy:{   '/baidu':{    target:'https://ba......
  • ActiveMQ配置warpper.log的大小和数量[转载]
    ActiveMQ服务器data目录下wrapper.log文件,默认产生的日志是不覆盖的,文件的大小逐渐增大,经过查询资料,和阅读Activemq的官方文档,找到了解决方案:<1>找到wrapper.conf文件: <2>修改wrapper.conf文件:设置文件大小修改maxsize的值,默认是0,也就是无限制,设置10m表示最大为10m,可以重新......
  • Docker限制日志文件大小及个数
    对单个容器生效dockerrun--log-optmax-size=10m--log-optmax-file=3全局容器生效vim/etc/docker/daemon.json{"log-driver":"json-file","log-opts":{"max-size":"200m","max-file"......
  • 在CentOS系统中如何配置Java开发环境环境变量的手把手操作流程
    以下是一份关于“在CentOS系统中如何配置Java开发环境环境变量”的手把手操作流程,由浅入深,先整体后分部:一、引言Java是一种广泛使用的编程语言,其跨平台特性使得它在不同操作系统上都能运行。为了在CentOS系统中进行Java开发,我们需要配置Java开发环境。本文将详细介绍如何......
  • 在Windows系统中如何配置Java开发环境环境变量&零基础手把手教程
    以下是一份关于“在Windows系统中如何配置Java开发环境环境变量”的手把手操作流程,由浅入深,先整体后分部:一、引言Java是一种广泛使用的编程语言,其跨平台特性使得它在不同操作系统上都能运行。为了在Windows系统中进行Java开发,我们需要配置Java开发环境。本文将详细介绍如......
  • Spring Cloud全解析:服务调用之自定义Feign的配置
    自定义Feign的配置Feign的默认配置类是FeignClientsConfiguration,其内部定义了Feign默认使用的编码器、解码器、契约、重试机制等@Bean@ConditionalOnMissingBeanpublicDecoderfeignDecoder(){//解码器,将字节数组反序列化为方法返回值类型的对象,默认只支持反序列化为St......
  • windows 上 Tauri 开发环境配置
    Tauri是一个构建适用于所有主流桌面和移动平台的轻快二进制文件的框架。开发者们可以集成任何用于创建用户界面的可以被编译成HTML、JavaScript和CSS的前端框架,同时可以在必要时使用Rust、Swift和Kotlin等语言编写后端逻辑。Tauri使用MicrosoftC++构建工具进行开发以......
  • vscode配置java简易教程
    一、下载jdkjava官方地址:https://www.oracle.com/java/technologies/downloads/选择适合自己电脑的版本,并下载到一个自己喜欢的地方。二、配置环境变量记住自己jdk的地址,例:我的jdk下载到了D:\app\java1、JAVA_HOME配置点击win键后,搜索“环境变量”。如图,打开配置......
  • 【CMake】使用CMake在Visual Stdudio中配置glad和glfw
    下载glad和glfwgladgladglad下载:glad下载这个是......