aubes / shadow-logger-bundle
用于匿名化的Monolog处理器
v1.2.0
2023-04-13 08:52 UTC
Requires
- php: >=7.4
- monolog/monolog: ^2.0 | ^3.0
- symfony/http-foundation: ^5.4 |^6.0
- symfony/http-kernel: ^5.4 |^6.0
- symfony/polyfill-php80: ^1.0
- symfony/property-access: ^5.4 | ^6.0
Requires (Dev)
- friendsofphp/php-cs-fixer: ^3.1
- phpmd/phpmd: ^2.10
- phpspec/prophecy-phpunit: >=v2.0.1
- phpunit/phpunit: >=9.6
- vimeo/psalm: ^5.9
README
这个Symfony扩展包提供了一种Monolog处理器,用于转换日志数据,以遵守GDPR或匿名化敏感数据。
它允许IP匿名化,对日志中的数据进行编码或删除。
安装
composer require aubes/shadow-logger-bundle
配置
配置如下所示
# config/packages/shadow-logger.yaml shadow_logger: # If enabled, add "shadow-debug" on "extra" with debug information when exception occurred debug: '%kernel.debug%' # If enabled, remove value when exception occurred strict: true # Register ShadowProcessor on channels or handlers, not both # To configure channels or handlers is recommended for performance reason # Logging channels the ShadowProcessor should be pushed to handlers: ['app'] # Logging handlers the ShadowProcessor should be pushed to #channels: ['app'] encoder: salt: '%env(SHADOW_LOGGER_ENCODER_SALT)%' mapping: # Context fields context: custom_field: [] # Array of Transformer aliases # Examples: user_ip: ['ip'] user_name: ['hash'] user_birthdate: ['remove'] # Extra fields extra: custom_field: [] # Array of Transformer aliases
映射
字段名可以包含点以进入数组。
例如,如果'extra'包含以下数组
'user' => [ 'id' => /* ... */, 'name' => [ 'first' => /* ... */, 'last' => /* ... */, ], ]
可以修改ip
和name
字段
# config/packages/shadow-logger.yaml shadow_logger: mapping: extra: user.ip: ['ip'] user.name.first: ['hash'] user.name.last: ['remove']
警告,为了性能,最好使用不带点的字段名。内部,当字段名包含点时,使用PropertyAccessor而不是简单的数组键访问。
转换器
目前,此扩展包提供以下转换器
- ip:匿名化IPv4或IPv6(参看:
Symfony\Component\HttpFoundation\IpUtils::anonymize
) - hash:使用hash函数编码值
- string:将
scalar
转换为string
或对对象调用__toString
- remove:删除值(替换为
--obfuscated--
) - encrypt:加密值(仅在配置了加密器的情况下可用,参看:加密转换器)
链式转换器
可以链式调用转换器,例如编码一个"Stringable"对象
# config/packages/shadow-logger.yaml shadow_logger: # [...] mapping: context: custom_field: ['string', 'hash']
哈希转换器
编码器配置
# config/packages/shadow-logger.yaml shadow_logger: # [...] encoder: algo: 'sha256' # cf: https://php.ac.cn/manual/fr/function.hash-algos.php salt: '%env(SHADOW_LOGGER_ENCODER_SALT)%' binary: false
加密转换器
此扩展包不提供加密类。
要使用"encrypt"转换器,需要手动配置加密器。
首先,需要创建一个Adapter类并扩展EncryptorInterface
// src/Encryptor/EncryptorAdapter.php namespace App\Encryptor; use Aubes\ShadowLoggerBundle\Encryptor\EncryptorInterface; class EncryptorAdapter implements EncryptorInterface { // [...] public function encrypt(string $data, string $iv): string { // [...] return $encryptedValue; } public function generateIv(): string { // [...] return $iv; } }
然后,将您的类注册为服务(如果未使用服务发现)
# config/services.yaml services: App\Encryptor\EncryptorAdapter: ~
最后,在ShadowLoggerBundle中配置您的服务ID
# config/packages/shadow-logger.yaml shadow_logger: # [...] encryptor: 'App\Encryptor\EncryptorAdapter'
此转换器将值替换为数组
[ 'iv' => , // Random IV used to encrypt the value 'value' => , // Encrypted value ]
自定义转换器
首先需要创建一个转换器类并扩展TransformerInterface
// src/Transformer/CustomTransformer.php namespace App\Transformer; class CustomTransformer implements TransformerInterface { public function transform($data) { // [...] return $value; } }
然后,将您的类注册为带有'shadow_logger.transformer'标签的服务
# config/services.yaml services: App\Transformer\CustomTransformer: tags: - { name: 'shadow_logger.transformer', alias: 'custom' }