You can definitely do what you're looking to do: Python handlers can specify at what level they log, and what associated formatters to use. By default, Indigo defines the following handler:
- Code: Select all
################################################################################
# Finally, we define a logging handler that emits log messages into the Indigo
# Event Log.
################################################################################
class IndigoLogHandler(logging.Handler, object):
def __init__(self, displayName, level=logging.NOTSET):
super(IndigoLogHandler, self).__init__(level)
self.displayName = displayName
def emit(self, record):
# First, determine if it needs to be an Indigo error
is_error = False
if record.levelno in (logging.ERROR, logging.CRITICAL):
is_error = True
type_string = self.displayName
# For any level besides INFO and ERROR (which Indigo handles), we append
# the debug level (i.e. Critical, Warning, Debug, etc) to the type string
if record.levelno not in (logging.INFO, logging.ERROR):
type_string += u" %s" % record.levelname.title()
# Then we write the message
indigo.server.log(message=self.format(record), type=type_string, isError=is_error)
As you can see, we take any string (passed into the record param as record.message) and pass it to the handler's format() method (which calls the formatters that you add to the handler and which we define by default as '%(message)s') to generate the string we pass as the message to the server's log() method (and we add an extra type string for anything other than INFO and ERROR).
We then add this handler to the standard logger that we create for the plugin (self.logger) and set it's level to INFO (many developers will change this in their init() method based on debugging, etc).
However, what you can do is to create your own handler (using the one above as a template) that would only emit messages of DEBUG or lower and then add that handler to the self.logger object (loggers can have an unlimited number of handlers). Make sure to set the handler's level to DEBUG (or THREADDEBUG if you're using that Indigo created level) so that it emits the different format, and associate the format with the handler.
So, in pseudo code:
- Code: Select all
# Define the handler class
class MyDebugHandler(logging.Handler, object):
def __init__(self, displayName, level=logging.NOTSET):
super(IndigoLogHandler, self).__init__(level)
self.displayName = displayName
def emit(self, record):
# add whatever stuff is specific to your handler, but the most important thing
# is to only to log things that match record.levelno <= logging.DEBUG
# (that might not be exactly right, but it's close I think)
# In the init method for your plugin, do these
my_debug_handler = MyDebugHandler(pluginDisplayName, logging.THREADDEBUG)
my_debug_handler.setFormatter(some_format_here)
self.logger.addHandler(my_debug_handler)
I probably got some of the details wrong, but that should get you started.