How can I log current line, and stack info with Python?

前端 未结 8 1173
情深已故
情深已故 2021-02-02 11:01

I have logging function as follows.

logging.basicConfig(
    filename = fileName,
    format = \"%(levelname) -10s %(asctime)s %(message)s\",
    level = logging         


        
8条回答
  •  长情又很酷
    2021-02-02 11:46

    Current function name, module and line number you can do simply by changing your format string to include them.

    logging.basicConfig(
        filename = fileName,
        format = "%(levelname) -10s %(asctime)s %(module)s:%(lineno)s %(funcName)s %(message)s",
        level = logging.DEBUG
    )
    

    Most people only want the stack when logging an exception, and the logging module does that automatically if you call logging.exception(). If you really want stack information at other times then you will need to use the traceback module for extract the additional information you need.

提交回复
热议问题