vanilla / laravel
Laravel 工作中的实用工具和命令。
Requires
- php: >=8.0
- laravel/framework: ^10.0
- vanilla/garden-utils: ^1.1
Requires (Dev)
- phpunit/phpunit: ^10.2
- vimeo/psalm: ^5.13
README
一组在 Vanilla Laravel 服务中使用的常用 Laravel 实用工具和配置。
安装
使用以下命令通过 composer 安装此包:
composer require vanilla/laravel
此包利用 Laravel 的包自动发现机制,因此应自动应用。
额外的日志上下文
如果通过 composer 安装包,则此配置将自动完成。
在 Web 请求期间做出的每一个操作都会应用以下额外的数据。
{ // ... Basic log data here. "tags": ["webRequest"], "request": { "hostname": "www.your-service.com", "method": "GET", "path": "/some/path/here?queryParams", "protocol": "https", "url": "https://www.your-service.com/some/path/here?queryParams", "clientIP": "0.0.0.0" } }
改进的 JSON 日志格式化器
要应用此功能,请更新 config/logging.php
文件中您想要的日志通道,以使用新的日志格式化器。
config/logging.php
return [ // ...Other configs here "channels" => [ // ...Other configs here "single" => [ "driver" => "single", "formatter" => VanillaLogFormatter::class, "path" => storage_path("logs/laravel.log"), "level" => env("LOG_LEVEL", "debug"), ], "syslog" => [ "driver" => "syslog", "formatter" => VanillaLogFormatter::class, "level" => env("LOG_LEVEL", "debug"), ], // ...Other configs here ], // ...Other configs here ];
改进的日志格式化器提供了以下行为:
- 将日志输出为以
$json:
预先序列化的 JSON,符合标准的 vanillav2
日志架构。 Vanilla\Laravel\Exception\ContextException
实例现在将它们的上下文序列化。- 异常堆栈跟踪的序列化已改进。
- 框架不包含存储库的基本路径。
- 某些常见的框架始终排除(例如日志格式化和异常处理程序中的框架)。
- 供应商堆栈框架被折叠。
- 仅包括文件和行号。
- 所有日志都获得最小的堆栈跟踪。
改进的异常处理
要应用此功能,请将 Exceptions\Handler.php
中的异常处理程序从 Illuminate\Foundation\Exceptions\Handler
扩展到 Vanilla\Laravel\Exceptions\ExceptionHandler
。
它具有以下改进行为:
- API 响应将始终序列化为 JSON。
- 抛出的
ContextExceptions
将序列化它们的上下文。 - 如果启用
app.debug
配置,则堆栈跟踪将返回在 JSON 输出中。
上下文异常
在抛出异常时,您可能有一些有用的结构化数据,您希望捕获用于记录或 HTTP 响应。
对于这些情况,抛出或扩展 Vanilla\Laravel\Exceptions\ContextException
。它添加了一个可选的 context
数组。
想要向捕获的 ContextException
添加更多上下文?请使用 ContextException::withContext($newContext)
。
配置验证
此包注册了一个新的命令 artisan config:validate
,该命令将在 config:cache
之前自动运行。它将遍历您的配置文件,查找 ValidateConfigCommand::KEY
键下的验证规范,并根据这些规则验证其余配置文件。
示例
orch.php
return [ /// /// Config validate here!!! /// ValidateConfigCommand::KEY => [ "type" => ["in:orchestration,local"], "base_url" => ["required", "url"], "search_service_base_url" => ["required", "url"], "hostname" => ["string"], "token" => ["string", "required"], "network" => ["string", "required"], "zone" => ["string", "required"], ], /* |-------------------------------------------------------------------------- | Orchestration |-------------------------------------------------------------------------- | | These fields are used for accessing orchestration and fetching site info. */ "type" => env("ORCH_TYPE", $isOrchestration ? "orchestration" : "local"), // Base URL for orchestration. "base_url" => env("ORCH_BASE_URL"), // Optional. Primarily used in localhost to force `Host` header with a base_url of an internalIP for the VPN. "hostname" => env("ORCH_HOSTNAME"), // Access token used to call orchestration. "token" => env("ORCH_TOKEN"), // Which orchestration network to use. "network" => env("ORCH_NETWORK"), // Which orchestration zone to use. "zone" => env("ORCH_ZONE"), // Base url to use when calling elasticsearch. "search_service_base_url" => env("ORCH_SEARCH_SERVICE_BASE_URL"), ];