I want to print the current timestamp when the event failed or not in my python script. Adding only ...
datetime.datetime.now().strftime("%d.%b %Y %H:%M:%S")
.... at the beginning of each line, the same date appears on each line
[INFO] 04.Feb 2015 20:49:41: cl1 [ OK ] 04.Feb 2015 20:49:41: 8.8.8.8 ONLINE! [INFO] 04.Feb 2015 20:49:41: cl2 [ OK ] 04.Feb 2015 20:49:41: 8.8.8.8 ONLINE! [INFO] 04.Feb 2015 20:49:41: cl3 [ OK ] 04.Feb 2015 20:49:41: 8.8.8.8 ONLINE!
(I added time.sleep(5) between them)
My next idea was to create a function by calling the current time, but I cannot embed this function in the print command.
Rs.py file
OK = "[" + bc.OKGREEN + " OK " + bc.ENDC + "] " + datetime.datetime.now().strftime("%d.%b %Y %H:%M:%S") INFO = "[" + bc.OKBLUE + "INFO" + bc.ENDC + "] " + datetime.datetime.now().strftime("%d.%b %Y %H:%M:%S") WARN = "[" + bc.WARN + "WARN" + bc.ENDC + "] " + datetime.datetime.now().strftime("%d.%b %Y %H:%M:%S") ERR = "[" + bc.ERR + "FAIL" + bc.ENDC + "] " + datetime.datetime.now().strftime("%d.%b %Y %H:%M:%S") DEB = "[" + bc.HEADER + "DEBUG" + bc.ENDC + "] " + datetime.datetime.now().strftime("%d.%b %Y %H:%M:%S")
File myapp.py
import rs # importing rs.py print rs.OK + hostname + "is up!" time.sleep(3) print rs.ERR+ hostname + "is down!"
Print:
>>> [INFO] 04.Feb 2015 20:49:41: xxx is up! >>> [ERR ] 04.Feb 2015 20:49:41: xxx is down!
source share