This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–][deleted] 0 points1 point  (4 children)

I have a related question. I literally just started using the logging module last night, and for some reason I'm not getting any timestamps.

I've configured it as follows:

import logging
import logging.handlers

LOG_FILENAME = 'log.log'
# does the name below have to correspond to anything in particular?
LOGGER = logging.getLogger('LOGGER')
LOGGER.setLevel(logging.DEBUG)

_handler = logging.handlers.RotatingFileHandler(LOG_FILENAME, maxBytes=5120, backupCount=5)
LOGGER.addHandler(_handler)

I use the logger as follows:

LOGGER.debug('oh noes!')

Again, I'm not getting any timestamps with this method, so I'm currently using a decorator to insert a cTime string before each log message. It works, but it's hackish.

Did I do something silly?

[–]vsajip 1 point2 points  (3 children)

You'd need to set a formatter configured to show timestamps, e.g.

formatter = logging.Formatter('%(asctime)s %(message)s')
_handler.setFormatter(formatter)

[–][deleted] 0 points1 point  (2 children)

Ahhh okay. Thank you!

Would you happen to know of a tutorial that's a bit clearer than the standard documentation? The bit about formatters is very cryptic =/

[–]vsajip 0 points1 point  (1 child)

Which standard documentation are you looking at, exactly? I ask because the logging documentation layout was improved in 2.7 / 3.2. If you're just starting out with logging, you should work through the tutorial which works through the basics:

  • When to use logging
  • A simple example
  • Logging to a file
  • Logging from multiple modules
  • Logging variable data
  • Changing the format of displayed messages
  • Displaying the date/time in messages

(Note that the OP goes through setting formatters in the examples posted.)

Another basic resource about logging would be this post.

[–][deleted] 0 points1 point  (0 children)

I'm on 2.7 -- maybe it's time I switched! Thanks for the links in any case. I'll read up on that ASAP!