ICON Plugin Development: Logging from modules in util folder

Hey Folks,
Reading the docs (SDK Guide | InsightConnect Documentation) I see references to using self.logger() from within an action to log things to the console. I put the majority of my code in modules outside of action.py ( “import plugin_folder.util.module_name”)… How do I log from those modules? I tried logging.getLogger(), but that isn’t working. (As in, the text isn’t showing up in the logs tab… it shows up when I use icon-plugin run)

  • What debug level is set for the default logger?
  • Is there a specific name I should be asking for with getLogger?
1 Like

Hi Matt - the best way of logging from here is to pass a logger instance from an action/connection to the classes/functions within the util files. A good approach here is to create a class that encompasses your util functions and then instantiate that with a logger instance (perhaps from your connection). This way you can avoid passing the logger instance each time you call a function. Example code…

class Util:
    
    def __init__(self, logger):
        self.logger = logger
        
    def some_function(self):
        self.logger.info("Logging something here...")

Let me know if you have any other questions around this!

1 Like

Makes sense, I’m already using classes so it’s an easy fix for me. That said, I was hoping to avoid explicitly passing a logger to the class though. Disclaimer: I’m not a software engineer, but my assumption was that the python logging framework does a good job at wiring things up between modules, and that something like below would be more “pythonic”:

import logging
class Util:

    def __init__(self):
        self.logger = logging.getLogger(__name__)

I guess the trade-off being… you don’t want to dirty up the logs tab of the job UI with random logs from boto3, or whatever 3rd party libs get imported…