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.
composer require schalkt/log
- 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}"'
- multiple log types in config
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);
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 Schalkt\Slog\Log;
require_once '/vendor/autoload.php';
Log::to()->info('Hello World!');
use Schalkt\Slog\Log;
require_once '/vendor/autoload.php';
Log::default(["folder" => APP_PATH . '/storage/logs/default']);
Log::to()->info('Hello World!');
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');
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');
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',
]
];
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}",
],
];
- {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')
- configurable output (STDOUT, STDERR)