Skip to content
Permalink

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also or learn more about diff comparisons.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also . Learn more about diff comparisons here.
base repository: relaxdiego/relaxdiego.github.com
Failed to load repositories. Confirm that selected base ref is valid, then try again.
Loading
base: master
Choose a base ref
...
head repository: uenz/relaxdiego.github.com
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: master
Choose a head ref
Can’t automatically merge. Don’t worry, you can still create the pull request.
  • 2 commits
  • 1 file changed
  • 1 contributor

Commits on Nov 3, 2018

  1. bug fix in filter

    uenz committed Nov 3, 2018
    Copy the full SHA
    492eea7 View commit details
  2. Bug fix in formatter

    uenz committed Nov 3, 2018
    Copy the full SHA
    c66f1c2 View commit details
Showing with 7 additions and 7 deletions.
  1. +7 −7 _posts/2014-07-05-logging-in-python.md
14 changes: 7 additions & 7 deletions _posts/2014-07-05-logging-in-python.md
Original file line number Diff line number Diff line change
@@ -5,7 +5,7 @@ comments: true
categories: Python, Logging, Filtering, Custom
---
<sup>Co-Written by [Hari Dara](https://github.com/haridsv)</sup>

<sup>Small code fix by [uenz](https://github.com/uenz)</sup>
Last week I've had to wrangle with Python's documentation because I needed one
of the apps I'm writing to centrally remove sensitive information from the logs. After
several attempts at understanding the documentation, I've come to appreciate
@@ -111,9 +111,9 @@ a logger configured as the above.

## Redacting logs using a Filter

Here's a [detailed flowchart](https://docs.python.org/2/howto/logging.html#logging-flow)
Here's a [detailed flowchart](https://docs.python.org/2/howto/logging.html#logging-flow)
of how the logging framework handles log messages. The handler flow on the right side
gives us our first hint on how to centrally filter out sensitive information
gives us our first hint on how to centrally filter out sensitive information
from our logs.

Declaring a filter requires that we create a new section in our config file called
@@ -129,7 +129,7 @@ filters:
- "metoo!"
{% endhighlight %}

I declared only one filter there and the class name is `RedactingFilter` as indicated
I declared only one filter there and the class name is `RedactingFilter` as indicated
by the `()` key. All other keys will be supplied to the `RedactingFilter` constructor. In
the above example, an array of strings called `patterns` will be passed. Let's
see how the `RedactingFilter` class might be defined. Let's assume we have a `filters.py`
@@ -147,7 +147,7 @@ class RedactingFilter(logging.Filter):
self._patterns = patterns

def filter(self, record):
msg = self.redact(record.msg)
record.msg = self.redact(record.msg)
if isinstance(record.args, dict):
for k in record.args.keys():
record.args[k] = self.redact(record.args[k])
@@ -203,14 +203,14 @@ class RedactingFormatter(object):

def __getattr__(self, attr):
return getattr(self.orig_formatter, attr)

{% endhighlight %}

In the code right after `logging` is configured ([see](#configuring-your-loggers)), we need to add the below code to wrap all formatters (*a word of caution*: the below code assumes that all the handlers are on the root logger, but this may not always be the case):

{% highlight python linenos %}
for h in logging.root.handlers:
h.setFormatter(FilterFormatter(h.formatter))
h.setFormatter(RedactingFormatter(h.formatter, patterns=['hide me','me also']))
{% endhighlight %}