Reputation: 489
All the requests and responses are logged in a web framework, but it is logging(using the logging module) in passwords also (because passwords are in login requests).
Can i selectively print 'XXXXXXX' for password or any other fields i dont wish to print?
in authentication.py
import logging
from logging import config
logging.config.dictConfig({'version': 1, 'delete_existing_loggers': False, 'handlers': '.....'})
LOGGER = logging.getLogger(__name__)
##code to get variables from http request packet (like method, parameters)(for example, methods, params can be: [authentication.login, username, password], [authentication.change_password, username, old_password, new_password], [authentication.generate_validation_code, username]),
LOGGER.info('method %s with %s parameters called', method, params)
so here i want, for specific methods, some variables should be 'xxxxx' instead of original value, specifically if the method is 'authentication.login' I want to print 'xxxx' for second parameter in the params.
Thanks.
Upvotes: 1
Views: 1369
Reputation: 489
In addition to solution provided by Christian, if handlers are passed in dictConfig or fileConfig, handler classes should be subclassed and filters need to be added.
Code is taken from following web page: http://streamhacker.com/2010/04/08/python-logging-filters/
class InfoFilter(logging.Filter):
def filter(self, rec):
return rec.levelno == logging.INFO
class InfoHandler(logging.StreamHandler):
def __init__(self, *args, **kwargs):
StreamHandler.__init__(self, *args, **kwargs)
self.addFilter(InfoFilter())
In addition if one wants to change record's arg field, one has to take it into a list and then change it and reassign it back.
Upvotes: 0
Reputation: 7257
Yes, that's possible. Take a look at the logging.Filter
class. You need to subclass it and then register it with your logger.
Example:
class PasswordLoggingFilter(logging.Filter):
def filter(self, record):
# Modify record, especially record.msg and/or record.args.
if want_to_keep_record:
return True
else:
return False
logger.addFilter(PasswordLoggingFilter())
For the details of the record
object, it's documented here.
Upvotes: 2