Skip to content

Commit

Permalink
Merge pull request #4 from consilience/feature/issue3
Browse files Browse the repository at this point in the history
Issue #3 move processor definitions to config file.
  • Loading branch information
judgej authored Nov 21, 2021
2 parents 25bf402 + 078f2d1 commit 86ff357
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 22 deletions.
25 changes: 24 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,17 @@
[![Latest Unstable Version](https://poser.pugx.org/consilience/laravel-extended-logging/v/unstable)](https://packagist.org/packages/consilience/laravel-extended-logging)
[![License](https://poser.pugx.org/consilience/laravel-extended-logging/license)](https://packagist.org/packages/consilience/laravel-extended-logging)

<!-- TOC -->

- [Laravel and Lumen Extended Logging](#laravel-and-lumen-extended-logging)
- [Installation](#installation)
- [Configuration](#configuration)
- [Configuration Upgrade](#configuration-upgrade)
- [Example Usage](#example-usage)
- [TODO](#todo)

<!-- /TOC -->

# Laravel and Lumen Extended Logging

Provide some ready-made logging extensions to make it easier to deploy
Expand All @@ -25,6 +36,8 @@ that gets an application logging container-ready with minimal effort.

## Installation

For Laravel:

php composer require consilience/laravel-extended-logging

Lumen requires the provider to be registered in `bootstrap/app.php` so that the
Expand Down Expand Up @@ -121,9 +134,19 @@ for the package:

php artisan vendor:publish --tag=laravel-extended-logging-config

Just one option is supported at this time:
Two options are supported at this time:

* `json-pretty-print` - set to `true` to format the JSON output to be more human readable
* `processor` - a list of instantiated monolog processor objects

The list of processors, by default, will include the custom processors provided by this
package, and a few of the processors that monolog provides.
You can remove what yoy don't want, and add any others you may need.

### Configuration Upgrade

Since release 1.2.0 the processors to run have been located in the config file.
You will need to publish the config file again to use the processors.

## Example Usage

Expand Down
28 changes: 28 additions & 0 deletions config/laravel-extended-logging.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,35 @@
<?php

use Consilience\Laravel\ExtendedLogging\Processor\AppNameProcessor;
use Consilience\Laravel\ExtendedLogging\Processor\AuthUserProcessor;
use Consilience\Laravel\ExtendedLogging\Processor\JobNameProcessor;
use Consilience\Laravel\ExtendedLogging\Tap;
use Monolog\Processor\MemoryUsageProcessor;
use Monolog\Processor\ProcessIdProcessor;
use Monolog\Processor\PsrLogMessageProcessor;
use Monolog\Processor\UidProcessor;

return [
// Set to "pretty print" JSON output for easier reading during development.

'json-pretty-print' => false,

// Processors of Monolog\Processor\ProcessorInterface to use for each log.
// This package provides a few, monolog provides a bunch
// built in, and you can add your own.

'processors' => [
// Custom processors.

new AuthUserProcessor(),
new AppNameProcessor(),
new JobNameProcessor(),

// Standard monolog processors.

new UidProcessor(Tap::UID_LENGTH),
new PsrLogMessageProcessor,
new ProcessIdProcessor,
new MemoryUsageProcessor,
],
];
2 changes: 1 addition & 1 deletion src/LoggingServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
use Illuminate\Queue\Events\JobFailed;
use Illuminate\Queue\Events\JobProcessed;
use Illuminate\Queue\Events\JobProcessing;
use Queue;
use Illuminate\Support\Facades\Queue;

class LoggingServiceProvider extends ServiceProvider
{
Expand Down
32 changes: 12 additions & 20 deletions src/Tap.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,11 @@
* A tap to enable additional processors.
*/

use Monolog\Processor\PsrLogMessageProcessor;
use Monolog\Processor\UidProcessor;
use Monolog\Processor\ProcessIdProcessor;
use Monolog\Processor\MemoryUsageProcessor;

use Consilience\Laravel\ExtendedLogging\Processor\AuthUserProcessor;
use Consilience\Laravel\ExtendedLogging\Processor\AppNameProcessor;
use Consilience\Laravel\ExtendedLogging\Processor\JobNameProcessor;
use Monolog\Processor\ProcessorInterface;

class Tap
{
protected $uidLength = 16;
const UID_LENGTH = 16;

public function __invoke($logger)
{
Expand All @@ -27,26 +20,25 @@ public function __invoke($logger)
// enabled.

foreach ($logger->getHandlers() as $handler) {
// Custom processors.

$handler->pushProcessor(new AuthUserProcessor());
$handler->pushProcessor(new AppNameProcessor());
$handler->pushProcessor(new JobNameProcessor());

// Standard monolog processors.
collect(config('laravel-extended-logging.processors'))
->each(function ($processor) use ($handler) {
if (! $processor instanceof ProcessorInterface) {
return;
}

$handler->pushProcessor(new UidProcessor($this->uidLength));
$handler->pushProcessor(new PsrLogMessageProcessor);
$handler->pushProcessor(new ProcessIdProcessor);
$handler->pushProcessor(new MemoryUsageProcessor);
$handler->pushProcessor($processor);
});

// Additional options.

if (method_exists($handler->getFormatter(), 'setJsonPrettyPrint')) {
if ((bool)config('laravel-extended-logging.json-pretty-print')) {
$handler->getFormatter()->setJsonPrettyPrint(true);
} else {
// A current bug in monolog causes `false` to toggle the flag rather than reset it.
// A workaround for a beug in older versions.
// See https://github.com/Seldaek/monolog/issues/1469

$handler->getFormatter()->setJsonPrettyPrint(true);
$handler->getFormatter()->setJsonPrettyPrint(false);
}
Expand Down

0 comments on commit 86ff357

Please sign in to comment.