solvrtech/laravel-logbook 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

solvrtech/laravel-logbook

Composer 安装命令:

composer require solvrtech/laravel-logbook

包简介

This package provides integration with LogBook.

README 文档

README

Laravel-logbook package extends Laravel's logging functionality by adding additional capabilities to send the log messages to the targeted LogBook installation. You can install the package by running the following command in the root folder of your Laravel application:

composer require solvrtech/laravel-logbook

then publish the config/logging.php config file as follows:

php artisan vendor:publish --tag=logbook

Edit the channels stack in the config/logging.php file to use “logbook” as follows:

// ...
'channels' => [
    // ...
    
    'logbook' => [
        'driver' => 'logbook',
        'level' => env('LOG_LEVEL', 'debug')
    ],
]

laravel-logbook also needs appropriate configuration in your application’s .env file.
For example:

LOG_CHANNEL=logbook
LOG_LEVEL=debug
LOGBOOK_API_URL="https://logbook.com"
LOGBOOK_API_KEY="4eaa39a6ff57c4..."

# Instance ID is a unique identifier per instance of your apps
LOGBOOK_INSTANCE_ID="default"
  • LOGBOOK_INSTANCE_ID
    Define a unique identifier for the app’s deployment. This is useful when you have multiple app instances or deployments (e.g. in horizontally-scaled environments). For example, you can set “app-1” for the first instance and “app-2” for the second one. The instance ID information will be shown as part of log details in LogBook.
  • LOGBOOK_API_URL
    The actual URL of your LogBook installation
  • LOGBOOK_API_KEY
    The API key that LogBook had generated for the app
  • LOG_CHANNEL
    Must use logbook as defined in the config/logging.php file
  • LOG_LEVEL
    Specify the minimum log level that will be submitted to your LogBook installation. The ordered log levels are (from lowest to highest):
  1. DEBUG
  2. INFO
  3. NOTICE
  4. WARNING
  5. ERROR
  6. CRITICAL
  7. ALERT
  8. EMERGENCY

For example, if you set LOG_LEVEL=WARNING, then only higher priority log levels such as WARNING, ERROR, CRITICAL, ALERT, and EMERGENCY are going to be submitted to your LogBook installation.

Submitting logs into LogBook

To submit any log message, you just need to use the Laravel Log facade in your controller or service class:

use Illuminate\Support\Facades\Log;

class UserController extends Controller {

public function show($message) {
      Log::emergency($message);
      Log::alert($message);
      Log::critical($message);
      Log::error($message);
      Log::warning($message);
      Log::notice($message);
      Log::info($message);
      Log::debug($message);
      // ...
   }
}

More info about Laravel logging can be found in their documentation page.

Submitting logs asynchronously

By default, logs from your application will be submitted synchronously as soon as they are recorded and this might lead to a performance issue for your application. Fortunately, you can submit the logs asynchronously by queuing the logs ( inside database or Redis) and then create a background task to submit the queue of logs in batch.

1. Storage for Queues

Set the following configuration in your application .env file:

# "database" or "redis"
LOGBOOK_TRANSPORT="database"
LOGBOOK_BATCH=15
  • LOGBOOK_TRANSPORT: specifies the type of storage for queuing mechanisms. Supported values are “database” or “redis”.
  • LOGBOOK_BATCH: maximum number of logs to be sent from your application into LogBook in a batch.

After configuring the storage for queuing of submitted logs, you will need to create a background task that will run: php artisan logbook:log:consume periodically. You can set this by using Systemd or Supervisor.

2.a Using Systemd

Create a new service file, for example /etc/systemd/system/log-consume.service, then add the following configurations into the file:

[Unit]
Description=Log Consume
After=network.target

[Service]
ExecStart=/usr/bin/php artisan logbook:log:consume
WorkingDirectory=/path/to/your/laravel
User=www-data
Restart=always

[Install]
WantedBy=multi-user.target

Start the service and enable it during system reboot:

sudo systemctl start log-consume && sudo systemctl enable log-consume

2.b Using Supervisor

Create a new configuration file for the log consume service, for example /etc/supervisor/conf.d/log-consume.conf. Add the following configurations into the file:

[program:log-consume]
command=php /path/to/your/laravel/artisan logbook:log:consume
directory=/path/to/your/laravel
autostart=true
autorestart=true
stderr_logfile=/var/log/log-consume.err.log
stdout_logfile=/var/log/log-consume.out.log
user=www-data

To start the service, run the following commands:

sudo supervisorctl reread && sudo supervisorctl update && sudo supervisorctl start log-consume

Optional: set your application version

Application version is an optional parameter that can also be included inside log submission data into your LogBook installation. To do so, add the "version" config in /config/app.php file:

return [
    // ...

    'version' => "1.0.0"
];

It's worth noting that while it's recommended to set the application version, it is an optional step. When the "version" config is not found, log submission should work normally, but the version information will not be found in the submitted logs.

solvrtech/laravel-logbook 适用场景与选型建议

solvrtech/laravel-logbook 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 92 次下载、GitHub Stars 达 3, 最近一次更新时间为 2022 年 11 月 22 日, 在 PHP 生态内属于活跃度较高的组件。

我们在过去多个企业项目中使用过 solvrtech/laravel-logbook 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 solvrtech/laravel-logbook 我们能提供哪些服务?
定制开发 / 二次开发

基于 solvrtech/laravel-logbook 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 92
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 3
  • 点击次数: 21
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 3
  • Watchers: 1
  • Forks: 1
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2022-11-22