4
\$\begingroup\$

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?

Jamal
35.2k13 gold badges134 silver badges238 bronze badges
asked Aug 24, 2014 at 14:05
\$\endgroup\$

1 Answer 1

3
\$\begingroup\$

How about this?

def log_exception(self, exc_info):
 msg = ''
 msg += 'Time: %s\n' % datetime.now()
 msg += 'Path: %s\n' % request.path
 msg += 'HTTP Method: %s\n' % request.method
 msg += 'POST_ARGS: %s\n' % request.form
 msg += 'GET_ARGS: %s\n' % request.args
 msg += 'Client IP Address: %s\n' % request.remote_addr
 msg += 'User Agent: %s\n' % request.user_agent.string
 msg += 'User Platform: %s\n' % request.user_agent.platform
 msg += 'User Browser: %s\n' % request.user_agent.browser
 msg += 'User Browser Version: %s\n' % request.user_agent.version
 msg += 'Username: %s\n' % current_user
 msg += 'Email: %s\n' % getattr(current_user, 'email', 'no email')
 self.logger.error(msg, exc_info=exc_info)

I guess, logging an exception doesn't happen every often, so efficiency shouldn't be a concern.

Alternatively, you can emulate variable interpolation and avoid formatting altogether:

now = datetime.now() 
email = getattr(current_user, 'email', 'no email')
msg = _('''\
 Time: {now}
 Path: {request.path}
 HTTP Method: {request.method}
 POST_ARGS: {request.form}
 GET_ARGS: {request.args}
 Client IP Address: {request.remote_addr}
 User Agent: {request.user_agent.string}
 User Platform: {request.user_agent.platform}
 User Browser: {request.user_agent.browser}
 User Browser Version: {request.user_agent.version}
 Username: {current_user}
 Email: {email}
''')
self.logger.error(dedent(msg), exc_info=exc_info)
answered Aug 24, 2014 at 16:03
\$\endgroup\$
0

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.