subjective-php/psr-log-mongodb

A concrete implementation of the PSR LoggerInterface using mongo

v2.1.0 2023-05-08 18:56 UTC

This package is auto-updated.

Last update: 2024-10-08 22:02:32 UTC


README

Code Quality

Latest Stable Version Latest Unstable Version License

Total Downloads Monthly Downloads Daily Downloads

This is an implementation of PSR-3 using MongoDB as backend storage.

Document Structure

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

Example Document

{
	"_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

Basic Usage

<?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');

Message/Context Interpolation

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']);

Requirements

PSR Log MongoDB requires PHP 7.0 (or later).

Composer

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

Contact

Developers may be contacted at:

Run Build

With a checkout of the code get Composer in your PATH and run:

composer install
./vendor/bin/phpunit
./vendor/bin/phpcs