Logging in Maya

Hey, I have been looking into logging in Maya and I am at a bit of a loss. I was wondering how you guys go about it. Are there any accepted best practices? Tips or suggestions? Existing modules that make it easy? Thanks!

You can use either the logging module(which is super and is in the standard library) or you can use the pymel logger which has the same functionality as the logging module, so it really depends on you which one you want to use.
The python docs and this are a great way to understand it.

Simple usage:

import logging
logger = logging.getLogger(__name__)
hdlr = logging.FileHandler('C:/temp/atest.log')
formatter = logging.Formatter('%(asctime)s %(levelname)s %(message)s')
hdlr.setFormatter(formatter)
logger.addHandler(hdlr) 
logger.setLevel(logging.INFO)

logger.debug('DEBUG') # This won't be on the output since the logger's level is logging.INFO and not logging.DEBUG
logger.info('INFO')
logger.warning('WARNING')
logger.error('ERROR')
logger.critical('CRITICAL')

You can also enable the PyMel logging menu in Maya which lists all your loggers and allow your users to set whatever logging level they want. There is (still) a bug in the PyMel code, but a fix is in the file in this post (which is also good documentation on how to use it):
http://christianakesson.com/2012/218#Logger

Thanks for the help guys. I will take a look at both solutions!

Setup for ‘logging’ in python that I have been using at the moment.

Just use the message functions of the module logging itself (info, warning, debug, …)
This way you don’t have to pass around a logger object from module to module.
After this just setup a basicConfig and it will affect all print in all modules.

I did this setup last week, so i am not aware of future limitations.

Is this acutally a good setup to start with ?

code samples :

code in scripts / modules

import logging
logging.debug(‘debug message’)

code in beginning of process

import logging
logging.basicConfig(level=logging.DEBUG)