consolidation / log
Improved Psr-3 / Psr\Log logger based on Symfony Console components.
Installs: 45 254 595
Dependents: 7
Suggesters: 0
Security: 0
Stars: 151
Watchers: 3
Forks: 5
Open Issues: 0
Requires
- php: >=8.0.0
- psr/log: ^3
- symfony/console: ^5 || ^6 || ^7
Requires (Dev)
- phpunit/phpunit: ^7.5.20 || ^8 || ^9
- squizlabs/php_codesniffer: ^3
- yoast/phpunit-polyfills: ^0.2.0
- 3.x-dev
- 3.1.0
- 3.0.0
- 2.x-dev
- 2.1.1
- 2.1.0
- 2.0.4
- 2.0.3
- 2.0.2
- 2.0.1
- 2.0.0
- 1.x-dev
- 1.1.1
- 1.1.0
- 1.0.6
- 1.0.5
- 1.0.4
- 1.0.3
- 1.0.2
- 1.0.1
- 1.0.0
- 0.1.3
- 0.1.2
- 0.1.1
- 0.1.0
- dev-larowlan-symfony7
- dev-test-with-8.3
- dev-test-on-8.2
- dev-bc-for-3
- dev-remove-success
- dev-psr-log-2
- dev-backport-settable-interface
- dev-test-more-log-levels
- dev-settable-output-interface
- dev-symfony-6
- dev-php-8.1
- dev-highest-on-8
- dev-github-actions
- dev-symfony5
- dev-php-74
- dev-expanded-testing
- dev-manage-styles
- dev-logger-manager
- dev-g1a
- dev-composer-scenarios
- dev-split-out-scenarios
- dev-test-multiple-symfony-versions
- dev-phpunit6
- dev-interpolation-compatibility
- dev-coveralls
This package is auto-updated.
Last update: 2024-11-05 01:07:37 UTC
README
Improved PSR-3 Psr\Log logger based on Symfony Console components.
Component Status
In use in Robo.
Motivation
Consolidation\Log provides a PSR-3 compatible logger that provides styled log output to the standard error (stderr) stream. By default, styling is provided by the SymfonyStyle class from the Symfony Console component; however, alternative stylers may be provided if desired.
Usage
$logger = new \Consolidation\Log\Logger($output);
$logger->setLogOutputStyler(new LogOutputStyler()); // optional
$logger->warning('The file {name} does not exist.', ['name' => $filename]);
String interpolation -- that is, the substitution of replacements, such as {name}
in the example above, is not required by PSR-3, and is not implemented by default in the Psr\Log project. However, it is recommended by PRS-3, and is often done, e.g. in the Symfony Console logger.
Consolidation\Log supports string interpolation.
A logger manager can be used to delegate all log messages to one or more loggers.
$logger = new \Consolidation\Log\LoggerManager();
$logger->add('default', new \Consolidation\Log\Logger($output));
This is useful if, for example, you need to inject a logger into application objects early (e.g. into a dependency injection container), but the output object to log to will not be available until later.
Comparison to Existing Solutions
Many Symfony Console compoenents use SymfonyStyle to format their output messages. This helper class has methods named things like success
and warning
, making it seem like a natural choice for reporting status.
However, in practice it is much more convenient to use an actual Psr-3 logger for logging. Doing this allows a Symfony Console component to call an external library that may not need to depend on Symfony Style. Having the Psr\Log\LoggerInterface serve as the only shared IO-related interface in common between the console tool and the libraries it depends on promots loose coupling, allowing said libraries to be re-used in other contexts which may wish to log in different ways.
Symfony Console provides the ConsoleLogger to fill this need; however, ConsoleLogger does not provide any facility for styling output, leaving SymfonyStyle as the preferred logging mechanism for style-conscienscious console coders.
Consolidation\Log provides the benefits of both classes, allowing for code that both behaved technically correctly (redirecting to stderr) without sacrificing on style.
Monolog also provides a full-featured Console logger that might be applicable for some use cases.