webarchitect609 / log-tools
PSR-3 compatible logger tools
Installs: 13 877
Dependents: 1
Suggesters: 0
Security: 0
Stars: 0
Watchers: 2
Forks: 0
Open Issues: 0
Requires
- php: ^7.4 || ^8.0
- monolog/monolog: ^1.24
- psr/log: ^1.0
Requires (Dev)
- friendsofphp/php-cs-fixer: ^3.29
- phpunit/phpunit: ^9.6
- roave/security-advisories: dev-master
README
PSR-3 compatible logger tools
Features
- Log any exception properly: with the stack trace, with all previous exceptions, etc
- Daily logger: setup directory with log files with 'Y_m_d' timestamp as a filename
Installation
composer require webarchitect609/log-tools
Usage
LogExceptionTrait
Use \WebArch\LogTools\Traits\LogExceptionTrait
instead of \Psr\Log\LoggerAwareTrait
in the class you want to be
able to log exceptions in more convenient way. Do not forget to implement \Psr\Log\LoggerAwareInterface
.
When an exception or error occurs feel free to use logException()
method to log it nice and easy. Exception chaining
is enabled by default, but in case you don't need it you can use setChaining(false)
method.
use Monolog\Handler\StreamHandler; use Monolog\Logger; use Psr\Log\LoggerAwareInterface; use Psr\Log\LogLevel; use WebArch\LogTools\Traits\LogExceptionTrait; class FooService implements LoggerAwareInterface { use LogExceptionTrait; public function __construct() { $this->setLogger( new Logger( 'FooLogger', [new StreamHandler(sys_get_temp_dir() . '/foo-service.log')] ) ); } public function bar() { try { throw new LogicException('Exception occurs in ' . __METHOD__); } catch (Throwable $exception) { /** * Exception will be logged with it's type, message, code, file, line and stack trace. */ $this->logException($exception, LogLevel::CRITICAL, ['var1' => 'ABC']); } } }
MonologLoggerFactory
Use \WebArch\LogTools\Factory\MonologLoggerFactory
to simplify creating logs on daily basis.
use WebArch\LogTools\Enum\SystemStream; use WebArch\LogTools\Factory\MonologLoggerFactory; $debug = false; $loggerFactory = new MonologLoggerFactory('/tmp/log/www', $debug); $logger = $loggerFactory->createFileLogger('bar', 'foo/baz.log', SystemStream::STDERR); /** * Creates `/tmp/log/www/foo/baz.log` * and outputs `[2019-02-15 12:42:59] bar.INFO: Hello, world! [] []` there * and to the STDERR. */ $logger->info( 'Hello, world!' );
Known Issues
None so far.