pyRevit logger - mooove b!tch, get out the way

Good morning/afternoon/evening/time pyRevit team,

First off, a million thanks for pyRevit. What a product.

So my question is about the pyRevit logger (in defence of the topic title the guideline said make it interesting!). I have created my own logger object that I’m looking to use. It’s nothing remarkable, it just has different log levels for the file and console handlers.

However when I “from pyrevit import script” it overrides whatever logger I intend to use downstream of that line. My understanding is that the first logger or logging.config (both ootb python) that is imported and set the minimum log levels for any subsequent loggers used. As such I have tried:

  • Importing logging (ootb python) and setting levels to DEBUG. Then importing pyrevit.script
import logging
logging.basicConfig(level=logging.DEBUG)

from pyrevit import script
  • Importing pyrevit.script getting the logger using script.get_logger() and then removing the handlers, resetting the handler levels
import logging
from pyrevit import script
logger = script.get_logger()

for h in logger.handlers:
    if isinstance(h, logging.fileHandler):
        h.setLevel(logging.DEBUG)

  • Importing logging (ootb python), getting the root logger and resetting the levels
import logging
logging.get_logger().root.setLevel(logging.DEBUG)

  • Importing pyrevit.script, getting the logger using script.get_logger() and then set_verbose_mode() but that makes the console and file handlers the same level, adding too much noise to the output window for everyday users

As you can probably tell by now I’m not suuuuper experienced with the python logging module but I’ve done some stack overflowing and chatgpting but all of the solutions provided have not worked

So my question is: is there a solution for inhibiting pyRevit’s logger dictating the levels for subsequent loggers?

Thank you again for pyRevit and thanks for reading this far if you did!
Cheers, Pete

Hi @p_smithBVN welcome to the community :raised_hands: :raised_hands:

I believe this is the reason you can’t get rid of pyRevit’s logger configuration:

also, the rest of the code in that module is an highly opinionated customization of the logging facility.

Unfortunately I don’t have a solution for you, the last time I tried to make loggers from different python libraries work along with my own app logger was a while ago, and it didn’t end so well :sweat_smile:

1 Like

Thank you @sanzoghenzo for your response. This helped alot and I think I have now solved my situation.

All I did was import the module of my custom app_logger before importing pyrevit forms and script. Then in my app_logger module I set the logger class, as your above code snippet, to my custom logger class

script.py

...
import app_logger
from pyrevit import forms, script
...

app_logger.py

from duHast.Utilities.Objects.logger_object import LoggerObject


LEVEL = (10, 30)  # These are python logging levels
script_shop_logger = LoggerObject(log_name="ScriptShop", log_level=LEVEL)
logging.setLoggerClass(script_shop_logger.__class__)

Thanks for your help!

2 Likes

Hi @p_smithBVN,

Glad to see you here. I am loosely watching your duHast gh work. Cool stuff.

And 100 points for the +!+I3!

No other input from me other than that.

Thanks for the welcome @Jean-Marc

As much as I’d like to take credit for duHast I probably shouldn’t :sweat_smile: That’s Jan Christel’s baby. I’m a sporadic contributor. But there’s great potential for sure. Especially when coupled with tools like pyRevit, and RevitBatchProcessor

Enjoyed your interview on BIM pure!

Cheers, Pete

1 Like