1

To clarify, I am trying to log function calls in a database in the following manner

# Method to write to database a log of every function call along with the parameters
def write_program_log(function_name, function_parameters_json_format):

And here is what I am pasting inside of my functions

def connection_sniffer():

    # Program Logging Call
    if gvl.global_program_logging_state:

        # First convert params into JSON object
        pythonValues = dict(locals())
        jsonValues = json.dumps(pythonValues)
        write_program_log(__name__, jsonValues)

i was expecting __name__ to refer to the name of the local scope function, but instead the variable is equal to __main__

Is there something else I can use to easily get the name of the local scope function without having to manually type out the name? Thanks!

  • `__name__` is a built-in name referring to the current *module*, nothing really to do with *scopes*. – chepner Jan 20 '23 at 16:48
  • You could also place your logging code within a function using something like [this](https://stackoverflow.com/a/6618825/12479639) at the end of your logged function. `frame.f_back.f_locals` and `frame.f_back.f_code.co_name` would get the `locals` and name. – Axe319 Jan 20 '23 at 16:49
  • 1
    Are you using the `logging` module? A `LogRecord` contains the name of the function where the logger was used. You just need to provide a `Formatter` to the logger to make use of it. – chepner Jan 20 '23 at 16:51
  • (If you're *not* using `logging`, you probably should be: it's a big wheel to reinvent.) – chepner Jan 20 '23 at 16:52

0 Answers0