truncating strings

Roy Smith roy at panix.com
Tue Aug 23 18:29:56 CEST 2011


I want to log a string but only the first bunch of it, and add "..."
to the end if it got truncated.  This certainly works:

          log_message = message
          if len(log_message) >= 50:
            log_message = log_message[:50] + '...'
          logger.error("FAILED: '%s', '%s', %s, %s" % (log_message,
route, params, e.code))

but it bugs me that there should be some cleaner way to do this.  I'm
fantasizing about something along the lines of:

          logger.error("FAILED: '%s{50}', '%s', %s, %s" % (message,
route, params, e.code))

does anything like this exist?



More information about the Python-list mailing list