Skip to content

Commit

Permalink
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.
  • Loading branch information
LiamKearn committed Jun 5, 2023
1 parent 3e7365c commit dd43efe
Show file tree
Hide file tree
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
Expand Up @@ -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']);
}
```

Expand All @@ -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
Expand Down
3 changes: 2 additions & 1 deletion code/AuditFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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');
Expand All @@ -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);
Expand Down
Loading

0 comments on commit dd43efe

Please sign in to comment.