Python Logging Module logging timestamp to include microsecond
Question: I am using python's logging module for logs, but needed the timestamp to
include microsecond. It seems the timestamp can only get as precise as
millisecond. Here's my test code

    
    
    import logging
    
    logging.basicConfig(format='%(asctime)s %(levelname)s {%(module)s} [%(funcName)s] %(message)s',
    
        datefmt='%Y-%m-%d,%H:%M:%S:%f', level=logging.INFO)
    
    class log2_test():
    
        def test_class(self):
    
            logging.warning("Warning2 inside the class")
    
    def get_started2():
    
        logging.info("Logged2 Here")
    
    if __name__ == '__main__':
        get_started2()
    

Here's the output I get --

    
    
    2015-07-09,16:36:37:f INFO {logger} [get_started2] Logged2 Here
    

Somehow, %f is not recognized. Python version is 2.7.6.

How do I get the timestamp to include microseconds? Thanks in advance.


Answer: I do not think `strftime()` would support the `%f` directly. The logger does
though provide milliseconds as a separate msecs
[attribute](https://docs.python.org/2/library/logging.html?highlight=msec#logrecord-
attributes), so you could just add it yourself after the existing timestamp as
follows:

    
    
    logging.basicConfig(format='%(asctime)s.%(msecs)03d %(levelname)s {%(module)s} [%(funcName)s] %(message)s', datefmt='%Y-%m-%d,%H:%M:%S', level=logging.INFO)
    

This gave me the following output using your script:

    
    
    2015-07-10,09:21:16.841 INFO {test script} [get_started2] Logged2 Here
    

