This is an implementation of PSR-3 using MongoDB as backend storage.
Each mongo document constructed will have the following fields.
- timestamp The current UTC date/time
- level The RFC-5424 Log Level
- message The log message
- context Extraneous information that does not fit well in a string
{
"_id" : ObjectId("57fc0050fc77ae5c017e52b1"),
"timestamp" : ISODate("2016-10-08T02:02:12.944Z"),
"level" : "info",
"message" : "api access",
"context" : {
"method" : "GET",
"resource" : "/widgets/123",
"status" : 200
}
}
The logger does not handle log retention. Expire Data from Collections by Setting TTL
<?php
use SubjectivePHP\Psr\Log\MongoLogger;
use MongoDB\Client;
$collection = (new Client())->selectDatabase('testing')->selectCollection('logs');
$logger = new MongoLogger($collection);
$logger->debug('Some debug info');
The message may contain placeholders which can be replaced with values from the context array. In the example below the final logged message will be
User chadicus was created
<?php
use SubjectivePHP\Psr\Log\MongoLogger;
use MongoDB\Client;
$collection = (new Client())->selectDatabase('testing')->selectCollection('logs');
$logger = new MongoLogger($collection);
$logger->info('User {username} was created', ['username' => 'chadicus']);
PSR Log MongoDB requires PHP 7.0 (or later).
To add the library as a local, per-project dependency use Composer! Simply add a dependency on subjective-php/psr-log-mongodb
to your project's composer.json
.
composer require subjective-php/psr-log-mongodb
Developers may be contacted at:
With a checkout of the code get Composer in your PATH and run:
composer install
./vendor/bin/phpunit
./vendor/bin/phpcs