Skip to content

Commit d1ddbe7

Browse files
authored
Merge pull request silverstripe#229 from bigfork/226-logging-options
NEW: Allow logging exceptions separately to non-fatal errors (fixes silverstripe#226)
2 parents 79253b1 + 36292de commit d1ddbe7

File tree

3 files changed

+28
-4
lines changed

3 files changed

+28
-4
lines changed

_config/logging.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ Name: omnipay-logging
44
# Set up a separate logger name for omnipay, but map it to the default implementation
55
SilverStripe\Core\Injector\Injector:
66
SilverStripe\Omnipay\Logger: '%$Psr\Log\LoggerInterface'
7+
SilverStripe\Omnipay\ExceptionLogger: '%$SilverStripe\Omnipay\Logger'
78

89
# Example how to set up a logger for omnipay that logs to a separate log-file named "omnipay.log"
910
# SilverStripe\Core\Injector\Injector:

docs/en/Logging.md

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
# Logging
22

3-
This module uses standard SilverStripe logging (via [monolog](https://github.com/Seldaek/monolog)).
3+
This module uses standard SilverStripe logging (via [monolog](https://github.com/Seldaek/monolog)).
44
Please read the [SilverStripe logging documentation](https://docs.silverstripe.org/en/4/developer_guides/debugging/error_handling/) on how to set up basic logging.
55

6-
By default, the Omnipay module logs to the default logger of SilverStripe.
6+
By default, the Omnipay module logs to the default logger of SilverStripe.
77
If you'd like, you can create a custom Log for Omnipay though (even with a custom error-level).
88

99
Here's an example config that configures a separate log output for SilverStripe, as well as one for Omnipay:
@@ -49,7 +49,7 @@ There's a config setting `SilverStripe\Omnipay\Helper\Logging.logStyle` that def
4949
- `'verbose'`: Verbose logging, but strips out sensitive information
5050
- `'simple'`: Simplified messages (only title, message and code)
5151

52-
There's also a setting that controls which data-fields will be sanitized, so that they don't show up in the logs. If you're logging on
52+
There's also a setting that controls which data-fields will be sanitized, so that they don't show up in the logs. If you're logging on
5353
a live environment, make sure to NOT log any sensitive information, such as credit-card numbers and CVV numbers!
5454

5555
You can control this "blacklist" via the `SilverStripe\Omnipay\Helper\Logging.loggingBlacklist` setting. By default the Helper class is configured like this:
@@ -62,3 +62,20 @@ SilverStripe\Omnipay\Helper\Logging:
6262
- 'token'
6363
- 'cvv'
6464
```
65+
66+
## Disable non-fatal error logging
67+
68+
In production environments, it is often desirable to disable logging anything that isn't an application error. This can be achieved
69+
by disabling the default `SilverStripe\Omnipay\Logger` logger instance, and adding a new logger for logging exceptions:
70+
71+
```yml
72+
---
73+
Name: payment-logging
74+
After:
75+
- '#omnipay-logging'
76+
---
77+
SilverStripe\Core\Injector\Injector:
78+
SilverStripe\Omnipay\Logger:
79+
class: 'Psr\Log\NullLogger'
80+
SilverStripe\Omnipay\ExceptionLogger: '%$Psr\Log\LoggerInterface'
81+
```

src/Service/PaymentService.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
*
3535
* Interfaces with the omnipay library.
3636
* @property LoggerInterface $logger
37+
* @property LoggerInterface $exceptionLogger
3738
*/
3839
abstract class PaymentService
3940
{
@@ -45,6 +46,7 @@ abstract class PaymentService
4546
*/
4647
private static $dependencies = [
4748
'logger' => '%$SilverStripe\Omnipay\Logger',
49+
'exceptionLogger' => '%$SilverStripe\Omnipay\ExceptionLogger',
4850
];
4951

5052
/**
@@ -449,7 +451,11 @@ protected function createMessage($type, $data = null)
449451
'Gateway' => $this->payment->Gateway
450452
]);
451453

452-
$this->logToFile($output, $type);
454+
if ($data instanceof \Exception) {
455+
$this->exceptionLogger->error($e->getMessage(), ['exception' => $e]);
456+
} else {
457+
$this->logToFile($output, $type);
458+
}
453459

454460
/** @var PaymentMessage $message */
455461
$message = Injector::inst()->create($type)->update($output);

0 commit comments

Comments
 (0)