Explain Codes LogoExplain Codes Logo

Python logging: use milliseconds in time format

python
logging
datetime
custom-formatter
Nikita BarsukovbyNikita Barsukov·Aug 28, 2024
TLDR

The one-two combo to include milliseconds in your Python log timestamps is by leveraging the power of %(asctime)s and %(msecs)03d format, serving as your right and left jabs. Check out this quick example:

import logging # Define formatter with milliseconds. Equipped for battle against time. formatter = logging.Formatter('%(asctime)s.%(msecs)03d %(message)s', '%Y-%m-%d %H:%M:%S') # Setup logger with the formatter. It's morphin' time! logger = logging.getLogger('app') console_handler = logging.StreamHandler() console_handler.setFormatter(formatter) logger.addHandler(console_handler) logger.setLevel(logging.INFO) # Log with message including milliseconds because...Precise time logging, Assemble! logger.info('Log message with milliseconds')

This nifty formatter pads the milliseconds to three decimal places, serving up clear, consistent logs as if on a silver platter.

Crafting a deluxe custom formatter

In situations when you need to take the timestamp format beyond the ordinary, create a custom formatter. Here's how you fashion one:

import logging from datetime import datetime class CustomFormatter(logging.Formatter): """Custom logging formatter, because formatters have dress codes too.""" def formatTime(self, record, datefmt=None): ct = datetime.fromtimestamp(record.created) if datefmt: s = ct.strftime(datefmt) else: s = ct.strftime(self.default_time_format) if self.default_msec_format: s = self.default_msec_format % (s, record.msecs) return s # Define the custom formatter with milliseconds. Dressing up our formatter in precious milliseconds. formatter = CustomFormatter('%(asctime)s %(message)s', '%Y-%m-%d %H:%M:%S.%f') # Rest is the same as the 'Fast answer' section

With this CustomFormatter class, the formatTime method is overwritten letting you use %f for microsecond precision or manipulate the timestamp to your heart's content.

Easy-peasy setup with basicConfig

The logging.basicConfig() lets you set up your logging configuration faster than you can say "Sonic the Hedgehog". That's perfect for those moments when milliseconds matter. New-age Python has improved formatting options, such as %f for microseconds:

logging.basicConfig(format='%(asctime)s.%(msecs)03d %(message)s', datefmt='%Y-%m-%d %H:%M:%S')

Ideal when you want a one-liner to spruce up the root logger with a new suit and tie (aka the desired format).

It's important to be mindful of your Python version, as they come with different accessories in their backpack. Navigate through these version differences like a boss:

  • For Python 2.6, strut your stuff with the default_msec_format in a custom formatter.
  • For modern Python (2.7+), don the %f in the datefmt parameter.

These tips shield you from unexpected surprises across different runtime environments or post upgrades.

Crystal clear formats with ISO 8601 and RFC 3339

Need a timestamp format that's as clear as a summer's day? Try out these top models: ISO 8601 and RFC 3339. More standardized, these timescales include milliseconds too:

logging.basicConfig(format='%(asctime)s.%(msecs)03d %(message)s', datefmt='%Y-%m-%dT%H:%M:%S.%fZ')

Sport this format for a easily recognized, universally attractive timestamp. Oscars for log formats, anyone?

Lighting up milliseconds

In this time tapestry:

'Timing Canvas': Stitched timeline — [HH:mm:ss.fff]

Milliseconds are like lightbulbs, illuminating the thread details:

'Timing Tapestry': Well-lit timeline — [HH:mm:ss.fff]

Compatibility - The key to meaningful visualisation

Ensure your visualization tools can cleverly interpret the added milliseconds.

Tailoring precise solutions for unique needs

Aligning with use case eccentricities

Sometimes, unusual time formats might call out to you for these situations:

  • Integration with another system throwing a non-standard time format party.
  • Human-readable logs sipping tea in various locales.
  • Keeping in line with an industry inside joke on time representation.

In such instances, craft custom formatters to comfortably fit these unique requirements.

Journeying without extra baggage

The solutions served so far travel light with no dependencies. Built-in logging and datetime are strong enough to carry advanced time formatting solo, ensuring your application stays feather-light and high-flyin'.

Practical code examples: No tall tales

Showing the practical applicability of each concept, these code snippets are your trusty sidekicks ready to aid your log formatting quest without embellishing the truth.

References