0

I am trying to add timestamp details to log message getting printed.

From This:

DEBUG:crate.client.http:Sending request to /_sql? ...
INFO:reporter.reporter:Notification successfully processed 

To This:

[2020-07-15 10:41:41.568+00] DEBUG:crate.client.http:Sending request to /_sql? ...
[2020-07-15 10:41:42.102+00] INFO:reporter.reporter:Notification successfully processed 

So, I added asctime format in basicconfig part of my code by adding format='[%(asctime)s %(message)s',datefmt='%m/%d/%Y %I:%M:%S %p]:

def log():
    r = EnvReader(log=logging.getLogger(__name__).info)
    level = r.read(StrVar('LOGLEVEL', 'INFO')).upper()

    logging.basicConfig(level=level,
            format='[%(asctime)s %(message)s',datefmt='%m/%d/%Y %I:%M:%S %p]')
    return logging.getLogger(__name__)


Also, the part after change giving output as: 


[08/10/2020 07:47:06 AM] Env variable QL_DEFAULT_DB not set, using default value of: crate
[08/10/2020 07:47:06 AM] Backend selected for tenant 'None' is: crate
[08/10/2020 07:47:06 AM] Env variable CRATE_HOST set to , using this value.
[08/10/2020 07:47:06 AM] Env variable CRATE_PORT not set, using default value of: 4200
[08/10/2020 07:47:06 AM] Env variable KEEP_RAW_ENTITY not set, using default value of: False
[08/10/2020 07:47:06 AM] Env variable LOGLEVEL not set, using default value of: INFO
[08/10/2020 07:47:06 AM] Notification successfully processed
x.x.x.x - - [10/Aug/2020 07:47:06] "POST /v2/notify HTTP/1.1" 200 -
[08/10/2020 07:47:06 AM]  - - [10/Aug/2020 07:47:06] "POST /v2/notify HTTP/1.1" 200 -

So the problem is that i want millisecond to also get printed with it in the format like this : [2020-07-15 10:41:41.568+00]

How can i use millisecond to print with asctime or any other way to print timestamp in python?

Any help on this will be appreciable.

damini chopra
  • 133
  • 2
  • 12
  • Does `.%f` (for the microseconds) help? I don't know a way to truncate it after the milliseconds. Note: might not work on all systems. – alani Aug 10 '20 at 08:23
  • 1
    Does this answer your question? [Python logging: use milliseconds in time format](https://stackoverflow.com/questions/6290739/python-logging-use-milliseconds-in-time-format) – BowlOfRed Aug 10 '20 at 08:41
  • No .%f is not working on Python version 3.8.3 – damini chopra Aug 10 '20 at 09:18
  • By adding , ='%(asctime)s.%(msecs)03d %(message)s',datefmt='%m-%d-%Y %I:%M:%S') ---- msecs solved the problem. Thanks – damini chopra Aug 10 '20 at 10:38

0 Answers0