lifeonscreen/laravel-log-keeper

此包的最新版本(v1.1.0)没有可用的许可信息。

Laravel Log Keeper 帮助您在任意位置存储日志的同时进行日志轮转,并使用自定义的本地/远程保留策略。

v1.1.0 2018-06-22 07:22 UTC

This package is auto-updated.

Last update: 2024-08-29 02:40:16 UTC


README

Latest Version Total Downloads

Laravel Log Keeper 帮助您在任意位置存储日志的同时进行日志轮转,并使用自定义的本地/远程保留策略。

典型的 Laravel Log Keeper 使用场景是设置一个每天运行的定时任务,将本地日志存储在 S3 存储桶、(s)ftp、Dropbox 或任何其他文件系统驱动器中。

您可以定义本地和/或远程保留策略以及远程上传天数,默认为 7 天、30 天和 1 天。

  • 超过 1 天的文件将被压缩并上传到远程磁盘。
  • 超过 7 天的本地文件将从本地磁盘删除。
  • 超过 31 天(远程天数+上传后天数)的远程文件将从远程磁盘永久删除。

亮点

  • 完全控制日志的存档位置、时间和方式
  • 防止服务器空间不足
  • 本地和远程存档的自定义保留策略

安装

Laravel Log Keeper 通过 Composer 提供

{
    "require": {
        "lifeonscreen/laravel-log-keeper": "1.*"
    }
}

设置

Laravel

注册服务提供者

// config/app.php

'providers' => [
    ...
    LifeOnScreen\LaravelLogKeeper\Providers\LaravelServiceProvider::class,
    ...
],

注册定时任务

// app/Console/Kernel.php

protected $commands = [
    ...
    \LifeOnScreen\LaravelLogKeeper\Commands\LogKeeper::class
    ...
];

...

protected function schedule(Schedule $schedule)
{
    ...
    $schedule->command('laravel-log-keeper')->daily();
    ...
}

日志格式

要使用 Laravel Log Keeper,您的日志文件必须是每日格式,该格式定义在您的 config/app.php 文件中。

    ...
    'log' => 'daily',
    ...

环境

您可以通过在 .env 文件中放置以下变量来覆盖以下变量:

示例

# .env
...

LARAVEL_LOG_KEEPER_REMOTE_DISK           = "s3"
LARAVEL_LOG_KEEPER_LOCAL_RETENTION_DAYS  = 3
LARAVEL_LOG_KEEPER_REMOTE_RETENTION_DAYS = 15
LARAVEL_LOG_KEEPER_REMOTE_PATH           = "myproject1-prod-01"

...

可以使用此命令发布配置

php artisan vendor:publish --provider=LifeOnScreen\LaravelLogKeeper\Providers\LaravelServiceProvider

配置文件

// ----------------------------------------------------------------------------
    // Enable or Disable the Laravel Log Keeper.
    // If it is set to false, no operations will be performed and it will be logged
    // if the logs are enabled
    // ----------------------------------------------------------------------------
    'enabled'                     => env('LARAVEL_LOG_KEEPER_ENABLED', true),

    // ----------------------------------------------------------------------------
    // Enable or Disable the Laravel Log Keeper for remote operations.
    // if it is set to false, the local files older than the local retention will be
    // delete without being uploaded to the remote disk
    // ----------------------------------------------------------------------------
    'enabled_remote'              => env('LARAVEL_LOG_KEEPER_ENABLED_REMOTE', true),

    // ----------------------------------------------------------------------------
    // Where in the remote location it will be stored. You can leave it blank
    // or specify a custom folder like proj1-prod or proj1-integ so that you could
    // use the same s3 bucket for storing the logs in different environments
    // ----------------------------------------------------------------------------
    'remote_path'                 => rtrim(env('LARAVEL_LOG_KEEPER_REMOTE_PATH'), '/'),

    // ----------------------------------------------------------------------------
    // How many days a file will be kept on the local disk before
    // being uploaded to the remote disk.
    // Default is 7 days.
    // Local files with more than 7 days will be compressed using bzip2 and uploaded
    // to the remote disk. They will also be deleted from the local disk after being
    // uploaded
    // If value is set to 0 logs will be kept forever.
    // ----------------------------------------------------------------------------
    'local_retention_days'        => env('LARAVEL_LOG_KEEPER_LOCAL_RETENTION_DAYS', 7),

    // ----------------------------------------------------------------------------
    // When file be uploaded to remote location.
    // Default is 1 day.
    // ----------------------------------------------------------------------------
    'upload_to_remote_after_days' => env('LARAVEL_LOG_KEEPER_UPLOAD_TO_REMOTE_DAYS', 1),

    // ----------------------------------------------------------------------------
    // How many days a file will be kept on the remote for.
    // The days here means days after the upload on server. So 30 would actually
    // 30 + 1 = 31
    // Only files older than 31 days would be deleted from the remote disk
    // If value is set to 0 logs will be kept forever.
    // ----------------------------------------------------------------------------
    'remote_retention_days'       => env('LARAVEL_LOG_KEEPER_REMOTE_RETENTION_DAYS', 30),

    'remote_retention_days_calculated' =>
        env('LARAVEL_LOG_KEEPER_REMOTE_RETENTION_DAYS', 30) +
        env('LARAVEL_LOG_KEEPER_UPLOAD_TO_REMOTE_DAYS', 1),

    // ----------------------------------------------------------------------------
    // Which config/filesystems.php disk will be used for remote disk.
    // This would be typically a AWS S3 Disk, (s)ftp, Dropbox or any other configured
    // disk that will store the old logs
    // ----------------------------------------------------------------------------
    'remote_disk'                      => env('LARAVEL_LOG_KEEPER_REMOTE_DISK'),

    // ----------------------------------------------------------------------------
    // Define whether Laravel Log Keeper will log actions or not.
    // The log will be stored in the logs folders with name
    // laravel-log-keeper-{yyyy-mm-dd}.log
    // ----------------------------------------------------------------------------
    'log'                              => env('LARAVEL_LOG_KEEPER_LOG', true)

安全

如果您发现任何安全相关的问题,请直接联系我们。

鸣谢