Python logging using a single file (function name, file name, line number)

PHPz
Release: 2024-02-08 22:00:21
forward
1100 people have browsed it

使用单个文件的 Python 日志记录(函数名、文件名、行号)

Question content

I'm trying to understand how an application works. To do this, I insert debug commands as the first line in the body of each function, with the goal of logging the name of the function and the line number (within the code) that sends the message to the log output. Finally, since this application consists of many files, I would like to create a log file so that I can better understand the application's control flow.

This is what I know:

  1. To get the function name I could use function_name.__name__ but I don't want to use function_name (so I can quickly copy and paste the generic Log.info("Message") to all functions in the text). I know this can be done in C using the __func__ macro, but I'm not sure about python.

  2. To get the filename and line number, I have seen (and I believe) my application is using the Python locals() function, but using a syntax I don't fully understand, For example: options = " LOG.debug('%(flag)s : %(flag_get)s' % locals()) I tried using LOG.info("My message %s" % locals()) to generate something like {'self': <__main__.Class_name object at 0x22f8cd0>}. Any input on this?

  3. I know how to use logging and add handlers to it to log to a file, but I'm not sure if I can use a single file to log all log messages in the correct order of function calls in my project.


Correct Answer


You have some irrelevant questions here.

I'll start with the simplest: (3). Using logging you can aggregate all calls to a single log file or other output destination: they will be arranged in the order in which they occur within the process.

Next step: (2). locals() Provides a dictionary of the current scope. So, in a method with no other parameters, you have self in the scope, which contains a reference to the current instance. The trick used that confuses you is the string formatting of rhs using a dictionary as the % operator. "%(foo)s" % bar will be replaced with the value of bar["foo"].

Finally, you can use some introspection techniques, similar to those used by pdb to record more information:

def autolog(message):
    "Automatically log the current function details."
    import inspect, logging
    # Get the previous frame in the stack, otherwise it would
    # be this function!!!
    func = inspect.currentframe().f_back.f_code
    # Dump the message + the name of this function to the log.
    logging.debug("%s: %s in %s:%i" % (
        message, 
        func.co_name, 
        func.co_filename, 
        func.co_firstlineno
    ))
Copy after login

This will log the incoming message, plus the (original) function name, the name of the file in which it was defined, and the line in that file. See inspect - Inspecting live objects for more details.

As I mentioned in a comment before, you can also insert the import pdb; line at any time to enter the <code>pdb interactive debugging prompt. pdb.set_trace() and rerun your program. This enables you to step through the code, inspecting the data according to your selection.

The above is the detailed content of Python logging using a single file (function name, file name, line number). For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:stackoverflow.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template