Code Review Stack Exchange is a question and answer site for peer programmer code reviews. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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?

share|improve this question
up vote 2 down vote accepted

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)
share|improve this answer
    
Didn't know about variable interpolation. Thanks! – TheOne Aug 24 '14 at 19:31

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.