Skip to content

Instantly share code, notes, and snippets.

@DiabloHorn
Created February 28, 2021 15:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save DiabloHorn/81624b885e1adf5c82972470a7926ee4 to your computer and use it in GitHub Desktop.
Save DiabloHorn/81624b885e1adf5c82972470a7926ee4 to your computer and use it in GitHub Desktop.
Example base class to build plugins with logging
# 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