Created
February 28, 2021 15:12
-
-
Save DiabloHorn/81624b885e1adf5c82972470a7926ee4 to your computer and use it in GitHub Desktop.
Example base class to build plugins with logging
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Example minimalistic plugin framework | |
# https://www.guidodiepen.nl/2019/02/implementing-a-simple-plugin-framework-in-python/ | |
import logging | |
class BasePlugin(object): | |
""" | |
Example class just to remember about logging stuff | |
We want to override the default formatting of the main logger, | |
without removing it alltogether | |
""" | |
pluginname = 'ExampleModule' | |
def __init__(self): | |
self.pluginlogger = self._setup_instance_logger() | |
def _setup_instance_logger(self): | |
plugininstancename = self.__class__.__name__ | |
#using the dot notation should make this a child of our main frameworklogger | |
mypluginlogger = logging.getLogger(f'frameworkname.{plugininstancename}') | |
mypluginglogger.propagate = False | |
mypluginhandler = logging.StreamHandler() | |
mypluginhandler.setFormatter(logging.Formatter(f'[{plugininstancename}] any formatting we want for this plugin')) | |
mypluginlogger.addHandler(mypluginhandler) | |
return mypluginlogger | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment