https://github.com/webarchitect609/log-tools
PSR-3 compatible logger tools
https://github.com/webarchitect609/log-tools
Last synced: 4 months ago
JSON representation
PSR-3 compatible logger tools
- Host: GitHub
- URL: https://github.com/webarchitect609/log-tools
- Owner: webarchitect609
- License: bsd-3-clause
- Created: 2019-02-14T13:30:03.000Z (over 7 years ago)
- Default Branch: master
- Last Pushed: 2024-06-28T15:26:14.000Z (almost 2 years ago)
- Last Synced: 2025-11-27T16:46:18.002Z (7 months ago)
- Language: PHP
- Size: 22.5 KB
- Stars: 1
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE.md
Awesome Lists containing this project
README
Log Tools
=========
[](https://travis-ci.org/webarchitect609/log-tools)
[](https://github.com/webarchitect609/log-tools/releases)
[](https://packagist.org/packages/webarchitect609/log-tools)
[](https://www.php.net/supported-versions.php)
[](LICENSE.md)
[](https://packagist.org/packages/webarchitect609/)
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.
```php
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.
```php
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.
Licence & Author Information
----------------------------
[BSD-3-Clause](LICENSE.md)