承接 schalkt/log 相关项目开发

从需求分析到上线部署,全程专人跟进,保证项目质量与交付效率

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

schalkt/log

Composer 安装命令:

composer require schalkt/log

包简介

Simple and easy configurable PHP logfile system

README 文档

README

A simple log system with pattern based path and messages. Objects and arrays are automatically converted to prettified JSON. You can also create CVS files. No need log rotation, just delete the older log folders if necessary.

Latest Stable Version Total Downloads PHP Version License GitHub issues Build GitHub stars

Install

  • composer require schalkt/log

Features

  • pattern based logfile path: /{TYPE}/{YEAR}/{YEAR}-{MONTH}/{TYPE}-{MONTH}-{DAY}
  • pattern based rows: {DATE} | {STATUS} --- {MESSAGE}
  • objects and arrays converted to prettified JSON automatically
  • customizable CSV row pattern: '"{DATE}";{MESSAGE};"{BACKTRACE.CLASS}";"{BACKTRACE.FUNCTION}"'
  • logrotate not required due to the pattern-based log path
  • multiple log types in config

Available log levels

  • Log::to()->info($message, $title = null);
  • Log::to()->error($message, $title = null);
  • Log::to()->critical($message, $title = null);
  • Log::to()->warning($message, $title = null);
  • Log::to()->notice($message, $title = null);
  • Log::to()->debug($message, $title = null);
  • Log::to()->exception(\Exception $ex, $title = null);
  • Log::to('import')->error('Unique id required');
  • Log::to('login')->notice($input, 'Invalid password');

Examples

Example folder structure with {TYPE}, {YEAR}, {MONTH} and {DATE} patterns

/storage/logs
    - /default
        - 2021
        - 2022
    - /logins
        - /2021
        - /2022
            - /2022-09
            - /2022-10
                - /INFO-2022-10-20.log
                - /INFO-2022-10-21.log
                - /INFO-2022-10-22.log
                - /ERROR-2022-10-22.log

Use default config

    use Schalkt\Slog\Log;

    require_once '/vendor/autoload.php';

    Log::to()->info('Hello World!');

Change default log folder

    use Schalkt\Slog\Log;

    require_once '/vendor/autoload.php';

    Log::default(["folder" => APP_PATH . '/storage/logs/default']);
    Log::to()->info('Hello World!');

Add new config

    use Schalkt\Slog\Log;

    require_once '/vendor/autoload.php';

    Log::config('import', [
        'folder' => APP_PATH . '/storage/logs/import',
        'folder_chmod' => 0700,
        'pattern_row' => '{DATE} {EOL} {STATUS} {EOL} {MESSAGE} {EOL} {REQUEST}',
    ]);

    // add an error to the import log
    Log::to('import')->error('Unique id required');

Load custom configs from file

    use Schalkt\Slog\Log;

    require_once '/vendor/autoload.php';

    // set config file path
    Log::configs('./config/logs.php');

    // add an error to the default log
    Log::to()->error('Password required');

    // add an input array to the login log with title
    Log::to('login')->notice($input, 'Invalid password');

Configs

Default config

return [
    'default' => [
        "folder" => APP_PATH . '/storage/logs/default',
        "folder_chmod" => 0770,
        "pattern_file" => "/{YEAR}-{MONTH}/{TYPE}-{YEAR}-{MONTH}-{DAY}",
        "pattern_row" => "{DATE} | {STATUS} --- {MESSAGE}",
        "extension" => "log",
        "format_date" => 'Y-m-d H:i:s',
    ]
];

Custom config file

return [
    "csv" => [
        "folder" =>  APP_PATH . '/storage/logs/csv',
        "header" => '"date";"message";"class";"function"',
        "pattern_file" => "/{TYPE}/{YEAR}-{MONTH}/{TYPE}-{YEAR}-{MONTH}-{DAY}",
        "pattern_row" => '"{DATE}";{MESSAGE};"{BACKTRACE.CLASS}";"{BACKTRACE.FUNCTION}"',
        "extension" => "csv",
    ],
    "login" => [
        "folder" => APP_PATH . '/storage/logs/login',
        "pattern_file" => "/logins/{TYPE}/{YEAR}-{MONTH}-{DAY}",
        "pattern_row" => "{DATE} {TITLE} {MESSAGE}",
    ],
];

Available variables in patterns

  • {MESSAGE} <- first parameter of function (required, string, array, object, any)
  • {TITLE} <- second parameter of function (not required, string or number)
  • {TYPE} <- came from log config
  • {STATUS} <- info, error, critical, warning, notice, debug, or exception
  • {REQUEST} <- dump $_REQUEST
  • {RAWBODY} <- file_get_contents('php://input')
  • {EOL} <- PHP_EOL
  • {DATE} <- date by config "format_date", default "Y-m-d H:i:s"
  • {YEAR} <- date('Y')
  • {MONTH} <- date('m')
  • {DAY} <- date('d')
  • {HOUR} <- date('H')
  • {MIN} <- date('i')
  • {IP}: Client's IP address.
  • {USER_AGENT}: User's browser user agent.
  • {REQUEST_METHOD}: HTTP request method (e.g., GET, POST).
  • {REQUEST_URI}: The requested URI.
  • {SESSION_ID}: Current session ID.
  • {HOSTNAME}: Server's hostname.
  • {EXECUTION_TIME}: Script execution time.
  • {MEMORY_USAGE}: Current memory usage.
  • {MEMORY_PEAK_USAGE}: Peak memory usage.

Flush Logs

The flush method allows you to delete all log files under a specific log type's folder. By default, it also deletes the base folder. However, you can control this behavior by passing a boolean parameter to the method.

Usage

// Delete all log files and the base folder
Log::to('custom')->flush();

// Delete only the log files but keep the base folder
Log::to('custom')->flush(false);

If the folder is protected (e.g., root directories like ./ or ../), the method will throw a LogException to prevent accidental deletion.

统计信息

  • 总下载量: 106
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 1
  • 点击次数: 9
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2020-04-09

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固