Skip to content

Commit

Permalink
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
ENH: Use the PsrLogMessageProcessor so that custom factory created lo…
Browse files Browse the repository at this point in the history
…ggers can use context.
LiamKearn committed Jun 5, 2023

Unverified

This user has not yet uploaded their public signing key.
1 parent 3e7365c commit dd43efe
Showing 8 changed files with 260 additions and 169 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -41,6 +41,8 @@ public function dostuff()
$this->auditLogger->info('stuff happened');
// You can also pass an arbitrary context array which will be included in the log.
$this->auditLogger->warn('stuff happened', ['defcon' => 'amber']);
// By default we use the PsrLogMessageProcessor so you can created contextualised messages using the {key} syntax.
$this->auditLogger->info('stuff happened {defcon}', ['defcon' => 'amber']);
}
```

@@ -49,6 +51,7 @@ Here is what will appear in the audit log on your dev machine (the exact format
```
Aug 24 11:09:02 SilverStripe_audit[80615]: stuff happened [] {"real_ip":"127.0.0.1","url":"/do-stuff/","http_method":"GET","server":"localhost","referrer":null}
Aug 24 11:09:02 SilverStripe_audit[80615]: stuff happened {"defcon":"amber"} {"real_ip":"127.0.0.1","url":"/do-stuff/","http_method":"GET","server":"localhost","referrer":null}
Aug 24 11:09:02 SilverStripe_audit[80615]: stuff happened amber {"defcon":"amber"} {"real_ip":"127.0.0.1","url":"/do-stuff/","http_method":"GET","server":"localhost","referrer":null}
```

## Troubleshooting
3 changes: 2 additions & 1 deletion code/AuditFactory.php
Original file line number Diff line number Diff line change
@@ -6,6 +6,7 @@
use Monolog\Formatter\LineFormatter;
use Monolog\Handler\SyslogHandler;
use Monolog\Logger;
use Monolog\Processor\PsrLogMessageProcessor;
use Monolog\Processor\WebProcessor;
use SilverStripe\Core\Injector\Factory;

@@ -21,7 +22,6 @@ public function create($service, array $params = [])
throw new Exception('AuditFactory does not support passing params.');
}

$obj = null;
switch ($service) {
case 'AuditLogger':
$log = new Logger('audit');
@@ -34,6 +34,7 @@ public function create($service, array $params = [])
]));

$syslog->pushProcessor(new RealIPProcessor());
$syslog->pushProcessor(new PsrLogMessageProcessor());
$formatter = new LineFormatter("%level_name%: %message% %context% %extra%");
$syslog->setFormatter($formatter);
$log->pushHandler($syslog);
Loading

0 comments on commit dd43efe

Please sign in to comment.