I have a function, that is used in flask, for logging application errors:
def log_exception(self, exc_info):
self.logger.error('''
Time: %s
Path: %s
HTTP Method: %s
POST_ARGS: %s
GET_ARGS: %s
Client IP Address: %s
User Agent: %s
User Platform: %s
User Browser: %s
User Browser Version: %s
Username: %s
Email: %s''' % (
datetime.now(),
request.path,
request.method,
request.form,
request.args,
request.remote_addr,
request.user_agent.string,
request.user_agent.platform,
request.user_agent.browser,
request.user_agent.version,
current_user,
getattr(current_user, 'email', 'no email'),
), exc_info=exc_info)
As you can see the indentation is quite ugly and the long list of formatting args makes it even worse.
Using textwrap.dedent helped the situation, but the formatting args is still problematic:
def log_exception(self, exc_info):
self.logger.error(
dedent('''Time: %s
Path: %s
HTTP Method: %s
POST_ARGS: %s
GET_ARGS: %s
Client IP Address: %s
User Agent: %s
User Platform: %s
User Browser: %s
User Browser Version: %s
Username: %s
Email: %s''') % (datetime.now(),
request.path,
request.method,
request.form,
request.args,
request.remote_addr,
request.user_agent.string,
request.user_agent.platform,
request.user_agent.browser,
request.user_agent.version,
current_user,
getattr(current_user, 'email', 'no email')),
exc_info=exc_info
)
Is this the best I can do?