fyre / log
A logging library.
Requires
- fyre/config: ^4.0
- fyre/container: ^1.0
- fyre/filesystem: ^2.0
- fyre/path: ^2.0
Requires (Dev)
- friendsofphp/php-cs-fixer: ^3.59
- fyre/php-cs-fixer-config: ^1.0
- phpunit/phpunit: ^11
This package is auto-updated.
Last update: 2024-12-11 08:31:07 UTC
README
FyreLog is a free, open-source logging library for PHP.
Table Of Contents
Installation
Using Composer
composer require fyre/log
In PHP:
use Fyre\Log\LogManager;
Basic Usage
$logManager = new LogManager($container);
Default configuration options will be resolved from the "Log" key in the Config.
Autoloading
It is recommended to bind the LogManager to the Container as a singleton.
$container->singleton(LogManager::class);
Any dependencies will be injected automatically when loading from the Container.
$logManager = $container->use(LogManager::class);
Methods
Build
Build a Logger.
$config
is an array containing configuration options.
$logger = $logManager->build($config);
Logger dependencies will be resolved automatically from the Container.
Clear
Clear instances and configs.
$logManager->clear();
Get Config
Get a Logger config.
$key
is a string representing the Logger key.
$config = $logManager->getConfig($key);
Alternatively, if the $key
argument is omitted an array containing all configurations will be returned.
$config = $logManager->getConfig();
Handle
$type
is a string representing the type of log.$message
is a string representing the log message.$data
is an array containing data to insert into the message string.
$logManager->handle($type, $message, $data);
Has Config
Determine whether a Logger config exists.
$key
is a string representing the Logger key, and will default toLogManager::DEFAULT
.
$hasConfig = $logManager->hasConfig($key);
Is Loaded
Determine whether a Logger instance is loaded.
$key
is a string representing the Logger key, and will default toLogManager::DEFAULT
.
$isLoaded = $logManager->isLoaded($key);
Set Config
Set the Logger config.
$key
is a string representing the Logger key.$options
is an array containing configuration options.
$logManager->setConfig($key, $options);
Unload
Unload a Logger.
$key
is a string representing the Logger key, and will default toLogManager::DEFAULT
.
$logManager->unload($key);
Use
Load a shared Logger instance.
$key
is a string representing the Logger key, and will default toLogManager::DEFAULT
.
$logger = $logManager->use($key);
Logger dependencies will be resolved automatically from the Container.
Loggers
You can load a specific logger by specifying the className
option of the $config
variable above.
Custom loggers can be created by extending \Fyre\Log\Logger
, ensuring all below methods are implemented.
Can Handle
Determine whether a log level can be handled.
$level
is a number indicating the log level.
$canHandle = $logger->canHandle($level);
By default, this method will return true if the $level
is below or equal to the threshold
defined in the logger config, otherwise false.
Handle
Handle a message log.
$type
is a string representing the type of log.$message
is a string representing the log message.
$logger->handle($type, $message);
File
The File logger can be loaded using custom configuration.
$options
is an array containing configuration options.className
must be set to\Fyre\Log\Handlers\FileLogger
.dateFormat
is a string representing the date format, and will default to "Y-m-d H:i:s".threshold
is a number representing the log threshold, and will default to 0.suffix
is a string representing the filename suffix, and will default to null (or "-cli" if running from the CLI).path
is a string representing the directory path, and will default to "/var/log".extension
is a string representing the file extension, and will default to "log".maxSize
is a number representing the maximum file size before log rotation, and will default to 1048576.
$container->use(Config::class)->set('Log.file', $options);
Logging
Generally, logging is done by calling the handle
method of a LogManager instance.
This will call the canHandle
method of all defined logger configs, and if that returns true then the handle
method will also be called.
The default log levels are shown below (in order of severity).
$message
is a string representing the log message.$data
is an array containing data to insert into the message string.
$logManager->handle('emergency', $message, $data); // 1 $logManager->handle('alert', $message, $data); // 2 $logManager->handle('critical', $message, $data); // 3 $logManager->handle('error', $message, $data); // 4 $logManager->handle('warning', $message, $data); // 5 $logManager->handle('notice', $message, $data); // 6 $logManager->handle('info', $message, $data); // 7 $logManager->handle('debug', $message, $data); // 8
There are default placeholders that can also be used in log messages:
- {post_vars} will be replaced with the
$_POST
data. - {get_vars} will be replaced with the
$_GET
data. - {server_vars} will be replaced with the
$_SERVER
data. - {session_vars} will be replaced with the
$_SESSION
data. - {backtrace} will be replaced with the backtrace.
See the MessageFormatter::formatMessage method for details about message formatting.