How do I get the function name in my logs

Hi,

maybe another case of “too blind to search” or “this guy is starting to learn python” :slight_smile:

How would I get the function name in the logs without hardcoding it? self.rule_name I already know, but could not find something similar for the function name.
Extenting the logging configuration with %(funcname) does not work either, I get the following:

  File "/usr/local/lib/python3.11/logging/__init__.py", line 659, in formatMessage
    return self._style.format(record)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.11/logging/__init__.py", line 451, in format
    raise ValueError('Formatting field not found in record: %s' % e)
ValueError: Formatting field not found in record: 'funcname'

not long ago somebody postet something that i use now.
in the logging.yml i set:

format: '%(asctime)s.%(msecs)03d [%(levelname)-5s] [%(name)s:%(filename)s.%(funcName)-18s] - %(message)s'

to include the filename and also the function name. so i think you only missed the capital letter “N” in funcName

This was indeed the error. Now that I know what to look for, in the python documentation, it really is with an uppercase ‘N’

Thank you :slight_smile:

1 Like

Just out of curiosity:
Over the short traceback there should be an extended traceback where you see both the file, line nr and function calls. Is it not there for you?

I believe you are speaking of the full stacktrace in HABApp.log in case of errors, right? If so, yes, this is an important source of information.

What I was trying to do yesterday is to have a lok of the values over time of a few items which are controlled by the Tesla binding. I wanted to see a log of the behaviour over time during a drive, just to exclude that I am having wrong assumptions. And, instead of hardcoding every listener method name in the “Here I am, doing this with that” kind of logging, leaving all of that to the logging framework feels much easier.

That makes sense. I only thought about the error case but not about informational debug.