markdown Symfony:屏蔽日志文件中的敏感数据

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了markdown Symfony:屏蔽日志文件中的敏感数据相关的知识,希望对你有一定的参考价值。

## Add processor to monolog handler
```js
<service id="shopmacher.payment.logger.handler" class="Monolog\Handler\StreamHandler">
    //...
    <call method="pushProcessor">
        <argument type="service" id="service_processor.payment_logger_filtering" />
    </call>
</service>
```

## Processor service
```js
class PaymentLoggerFiltering
{
    /**
     * @var EventDispatcherInterface
     */
    private $eventDispatcher;

    /**
     * PaymentLoggerFiltering constructor.
     * @param EventDispatcherInterface $eventDispatcher
     */
    public function __construct(EventDispatcherInterface $eventDispatcher)
    {
        $this->eventDispatcher = $eventDispatcher;
    }

    /**
     * @param array $record
     * @return array
     */
    public function __invoke(array $record)
    {
        $event = new PaymentLoggerFilteringEvent($record);
        $this->eventDispatcher->dispatch(PaymentLoggerFilteringEvent::NAME, $event);

        return $event->getRecord();
    }
}
```

## Listener
```js
public static function getSubscribedEvents()
{
    return [
        PaymentLoggerFilteringEvent::NAME => ['maskData', 128]
    ];
}

/**
 * @param PaymentLoggerFilteringEvent $event
 */
public function maskData(PaymentLoggerFilteringEvent $event)
{
    $record = $event->getRecord();
    $record['context'] = $this->maskCreditCardInfo($record['context']);
    $record['context'] = $this->maskIBAN($record['context']);

    $event->setRecord($record);
}
```

## Data masker helper
```js
/**
 * Class DataMasker
 */
class DataMasker
{
  //...
  protected function maskText(string $text)
  {
      $len = \strlen($text);
      $unmaskedLen = self::UNMASKED_LEN * 2;
      if ($len > self::MAX_LEN) {
          $firstChars = substr($text, 0, self::UNMASKED_LEN);
          $lastChars = substr($text, $len -self::UNMASKED_LEN, self::UNMASKED_LEN);
          $hiddenChars = str_repeat(self::MASKING_CHAR, $len - $unmaskedLen);

          return $firstChars.$hiddenChars.$lastChars;
      }

      return str_repeat(self::MASKING_CHAR, $len);
  }
}
```

以上是关于markdown Symfony:屏蔽日志文件中的敏感数据的主要内容,如果未能解决你的问题,请参考以下文章

配置 symfony monolog 保留 apache 日志

Symfony 和 Docker - 缓存和日志目录权限

日志文件中的掩码数据库 URL

屏蔽python日志中的敏感信息

markdown Commandes Symfony

markdown 在Symfony中集成React