Skip to content
/ log Public

Simple and easy configurable PHP logfile system

License

Notifications You must be signed in to change notification settings

schalkt/log

Repository files navigation

Simple and easy configurable PHP log system

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.

About

Simple and easy configurable PHP logfile system

Topics

Resources

License

Security policy

Stars

Watchers

Forks

Packages

 
 
 

Languages