You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
importloggingdeftest():
logging.basicConfig(level=logging.INFO)
logging.debug("this is debug")
logging.info("this is info")
logging.warning("this is warning")
logging.error("this is error")
logging.critical("this is critical")
if__name__=="__main__":
test()
INFO:root:this is info
WARNING:root:this is warning
ERROR:root:this is error
CRITICAL:root:this is critical
Config Logging
importloggingdeftest():
logging.basicConfig(
level=logging.INFO,
format='%(asctime)s %(levelname)s %(filename)s:%(lineno)d %(levelname)s %(message)s',
datefmt='%Y-%m-%d %H:%M:%S',
# filename='test.log', # write to file if you want
)
logging.debug("this is debug")
logging.info("this is info")
logging.warning("this is warning")
logging.error("this is error")
logging.critical("this is critical")
2023-02-10 22:02:13 INFO test.py:12 this is info
2023-02-10 22:02:13 WARNING test.py:13 this is warning
2023-02-10 22:02:13 ERROR test.py:14 this is error
2023-02-10 22:02:13 CRITICAL test.py:15 this is critical
Integration with logging services
importloggingfromlogging.handlersimportSysLogHandlerPAPERTRAIL_HOST="logs.papertrailapp.com"PAPERTRAIL_PORT=12345deftest():
logging.basicConfig(
level=logging.INFO,
format='%(asctime)s %(levelname)s %(filename)s:%(lineno)d %(levelname)s %(message)s',
datefmt='%Y-%m-%d %H:%M:%S',
)
logger=logging.getLogger("test")
logger.setLevel(logging.DEBUG)
handler=SysLogHandler(address=(PAPERTRAIL_HOST, PAPERTRAIL_PORT))
logger.addHandler(handler)
# you can add multiple handlers to the same logger, e.g. a file handlerlogging.error("this is error")
logging.critical("this is critical")