Skip to content

freshcells/guzzle-message-anonymizer-formatter

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

25 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Guzzle Message Anonymizer Formatter

Anonymize or truncate parts of json or xml payloads and request headers before logging with a guzzle middleware.
For data protection you might not want to log personal data, like names, addresses etc in your requests.

Usage

Example usage for Json Payloads:

...
$formatter = new GuzzleMessageJsonAnonymizerFormatter(
    ['PersonalData'],
    $substitute = '*****',
    AbstractAnonymizerFormatter::DEBUG,
    ['Authorization' => '*****']
);
$loggerMiddleware = Middleware::log($logger, $formatter);
$stack->push($loggerMiddleware);
$config  = [
    ...
    'handler'  => $stack,
];
$client  = new Client($config);

This will log:

<<<<<<<<
HTTP/1.1 200 OK
Date: Thu, 18 Jun 2020 10:04:21 GMT
Content-Type: application/json
Authorization: *****
...
{
  ...
  "Payload": {
    "Foo": "Bar", 
    ...
    "PersonalData": "*****"
  }, 
  ...
}

Usage Symfony

Declare the Formatter service:

Freshcells\GuzzleMessageAnonymizerFormatter\GuzzleMessageXmlAnonymizerFormatter:
  arguments:
    $elements:
      - 'none:FirstName'
      - 'none:LastName'
    $attributes:
      - 'none:Customer[@Age]'
    $namespaces:
      none: 'http://namespace.de/middleware/payment/'
    $truncateElements
        VeryLongText: 200

Override existing message formatter of a logger middleware:

csa_guzzle.logger.message_formatter:
  alias: 'Freshcells\GuzzleMessageAnonymizerFormatter\GuzzleMessageXMLAnonymizerFormatter'

Or declare a dedicated anonymizer_logger middleware:

guzzle.middleware.anonymizer_logger:
  class: Closure
  factory: ['GuzzleHttp\Middleware', log]
  arguments: ['@monolog.logger', '@Freshcells\GuzzleMessageAnonymizerFormatter\GuzzleMessageXmlAnonymizerFormatter', 'info']
  tags:
    - { name: csa_guzzle.middleware, alias: anonymizer_logger }

About

Anonymize Request data from guzzle before logging

Resources

Stars

Watchers

Forks

Packages

No packages published

Contributors 4

  •  
  •  
  •  
  •  

Languages