alexmacarthur/laravel-loki-logging
Composer 安装命令:
composer require alexmacarthur/laravel-loki-logging
包简介
Send your Laravel logs to a Grafana Loki server.
README 文档
README
A package for sending your logs to a Grafana Loki server. Set it up, schedule a recurring job, and each log will be batched and sent asynchronously as your application runs.
Installation and Setup
- Install the package by running
composer require alexmacarthur/laravel-loki-logging. - Publish the configuration file:
php artisan vendor:publish --provider=AlexMacArthur\\LaravelLokiLogging\\L3ServiceProvider
- Create a new log channel in your
config/logging.phpfile:
'loki' => [ 'driver' => 'monolog', 'handler' => \AlexMacArthur\LaravelLokiLogging\L3Logger::class, ]
-
Configure at least a
LOG_CHANNELenvironment variable to use the channel you created in the previous step. See more available environment variables below. -
Configure the
loki:persistjob to run at a regular interval. Unless there's reason to do otherwise, every minute is a good start.
Schedule::command('loki:persist')->everyMinute()->withoutOverlapping(); // Or using the class directly... use AlexMacArthur\LaravelLokiLogging\L3Persister; Schedule::command(L3Persister::class)->everyMinute()->withoutOverlapping();
Log::info('Start logging!');
Environment Variables
By default, the following environment variables are used for logging.
| Name | Description |
|---|---|
LOG_CHANNEL |
Required. Must be set to 'loki', or whatever you named the channel added to your logging.php configuration. |
LOG_SERVER |
Required. The Loki server to which logs are sent. |
LOG_USERNAME |
Optional. The username for basic authentication. |
LOG_PASSWORD |
Optional. The password for basic authentication. |
LOG_APP |
Optional. Used for the application label on every log. Falls back to APP_NAME. |
LOG_FORMAT |
Optional. The format used for each log message. The level_name and message variables can be used to build the format. By default, it's [{level_name}] {message} |
Configuration
The following configuration properties are used when forming and sending logs:
| Key | Description |
|---|---|
context |
Values to be assigned as labels in the message, used to organize and index logs by Grafana. Read more about labels here. Defaults are set at a configuration level, but individual values can be overridden by using the second parameter of Laravel's logging interface: Log::info('my log', ['application' => 'override']); |
format |
How log messages should be formatted. Variable substitutions are available. |
loki.server |
The Loki server where data should be sent. |
loki.username |
Username for HTTP basic authentication. Defaults to empty. |
loki.password |
Password for HTTP basic authentication. Defaults to empty. |
Variable Substitution
All tags and log messages can be enhanced with variable names provided by Monolog:
- message
- context
- level
- level_name
- channel
- datetime
- extra
To use them, wrap them in curly braces:
'format' => 'My log level is: {level_name}, and my message is: {message}',
Authentication
Loki does not provide any authentication out of the box, but it's highly recommended to configure via reverse proxy. This package only supports Basic Auth. If you place your server behind nginx, this can be set up here.
Shout-Out
This package was originally forked from @devcake-deventer's laravel-loki-logging package. Thank you for the great starting point!
alexmacarthur/laravel-loki-logging 适用场景与选型建议
alexmacarthur/laravel-loki-logging 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 3.71k 次下载、GitHub Stars 达 4, 最近一次更新时间为 2025 年 08 月 06 日, 在 PHP 生态内属于活跃度较高的组件。
我们在过去多个企业项目中使用过 alexmacarthur/laravel-loki-logging 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 alexmacarthur/laravel-loki-logging 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
统计信息
- 总下载量: 3.71k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 4
- 点击次数: 0
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2025-08-06