logging and maya script editor

3,315 views
Skip to first unread message

ynedelin

unread,
Dec 6, 2011, 8:48:26 PM12/6/11
to python_inside_maya
I got this from the python docs page

if I run this in the script editor:

import logging
logging.basicConfig(format='%(asctime)s %(message)s')
logging.warning('is when this event was logged.')

I get this:

# Warning: root : is when this event was logged. #

How can I get the date to show in the script editor?

2010-12-12 11:41:42,612 is when this event was logged.

Yury

Justin Israel

unread,
Dec 6, 2011, 10:05:16 PM12/6/11
to python_in...@googlegroups.com
Hi there,

I believe the problem you are experiencing is that you are conflicting with maya's own logger. The python docs for logging explain that logging.basicConfig() should only be called in the main thread and before any other threads are started. 

What you might want to try is this example from the Basic Tutorial section:

But with a small modification:

#=======================

import logging


logger = logging.getLogger('simple_example')

logger.setLevel(logging.DEBUG)


ch = logging.StreamHandler()

ch.setLevel(logging.DEBUG)


# create formatter

formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')

ch.setFormatter(formatter)

logger.addHandler(ch)


# prevent logging from bubbling up to maya's logger

logger.propagate=0


# 'application' code

logger.debug('debug message')

logger.info('info message')

logger.warn('warn message')

logger.error('error message')

logger.critical('critical message')


#=======================

Could you explain your actual goal? Are you trying to modify maya's existing logger or just create your own for custom logging?



yury nedelin

unread,
Dec 6, 2011, 11:19:43 PM12/6/11
to python_in...@googlegroups.com
Thanks Justin

well my true issue is accumulation of log info every time I reload the script.
I realize that I might have a totally wrong way of doing this but,

let say I have this script called myScript.py

#===========================

import logging
logger = logging.getLogger('simple_example')
logger.setLevel(logging.DEBUG)
ch = logging.StreamHandler()
ch.setLevel(logging.DEBUG)

# create formatter
formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
ch.setFormatter(formatter)
logger.addHandler(ch)

# prevent logging from bubbling up to maya's logger
logger.propagate=0

def testLog():


    # 'application' code
    logger.debug('debug message')
    logger.info('info message')
    logger.warn('warn message')
    logger.error('error message')
    logger.critical('critical message')
#=================================

now if I

import myScript
myScript.testLog()

I get a nice print out.
However if I make a change to the script and reload

reload(myScript)
myScript.testLog()

my print out is old info and new info, so its adding to the log,

how can I clear the log every time I reload?



>Could you explain your actual goal? Are you trying to modify maya's existing logger or just create your >own for custom logging?

I was not trying to modify Maya's existing logger, I was just trying to use python logging module to see if its something I could use for my work. However how can I access Maya's existing Logger, that sounds interesting?


If I have something simple

import logging
logging.basicConfig()
log = logging.getLogger(__name__)
log.setLevel(logging.INFO)

def testLog():
        log.debug("this is debugging")

then I get the info printed out once but when I try to add the the formatting, log accumulates the info.       
 
Thanks
Yury

Jesse Kretschmer

unread,
Dec 6, 2011, 9:18:52 PM12/6/11
to python_in...@googlegroups.com
Yury,
First create a logging object.  If you are running this from the script editor, you will need to set propagate = False so maya does not catch and resend your message in the format you see above.  Also, I would recommend the log handler provided by PyMel as it is already wired up to the correct error/warning/info events.  It is already available in Maya 2011 or greater.

Try this:

import logging
import maya.utils

myLogger = logging.getLogger("MyLogger")
myLogger.propagate = False
handler = maya.utils.MayaGuiLogHandler()
handler.setLevel(logging.INFO)
formatter = logging.Formatter("%(asctime)s %(message)s")
handler.setFormatter(formatter)
myLogger.addHandler(handler)

myLogger.warning('is when this event was logged.')


Cheers,

-jesse
On Tue, Dec 6, 2011 at 5:48 PM, ynedelin <yned...@gmail.com> wrote:

Geordie Martinez

unread,
Dec 7, 2011, 3:12:11 PM12/7/11
to python_in...@googlegroups.com
It might be worth noting that Pymel has a great little utility to load a Logging Control menu into the maya GUI.

from pymel.tools import loggingControl

loggingControl.initMenu()


Just for anyone reading this in case they want to control the log level in the interactive session of any module that uses the logging module.

David Lantos

unread,
Mar 3, 2014, 1:40:07 PM3/3/14
to python_in...@googlegroups.com
I've got error, MayaGuiLogHandler wants something to eat:

# Error: unbound method __init__() must be called with Filterer instance as first argument (got MayaGuiLogHandler instance instead)
# Traceback (most recent call last):
#   File "<maya console>", line 1, in <module>
#   File "C:\Program Files\Autodesk\Maya2013\bin\python26.zip\logging\__init__.py", line 596, in __init__
#     Filterer.__init__(self)
# TypeError: unbound method __init__() must be called with Filterer instance as first argument (got MayaGuiLogHandler instance instead) #

David Lantos

unread,
Mar 3, 2014, 1:49:02 PM3/3/14
to python_in...@googlegroups.com

Asi Sudai

unread,
Mar 3, 2014, 3:01:46 PM3/3/14
to python_in...@googlegroups.com
I'm a big fan of the logging module too... 
On GitHub I made a custom Logging module, based on python logging, with support for Maya, Nuke and MotionBuilder unique messages.

features are:
- will use Maya or Nuke built-in warning messages
- will popup a msgbox for FATAL level, so the artists ( which rarely look at the script editor could see )
- will print to stdout or/and to the application script-editor.
- Email developer the traceback msg ( feature that isn't on GitHub, but you could add easily to FATAL if you wish )

hope it helps anyone, feel free to contribute or branch this git.




On Monday, March 3, 2014 10:49:02 AM UTC-8, David Lantos wrote:
Ok, I've found a solution. ->

David Lantos

unread,
Mar 3, 2014, 4:20:53 PM3/3/14
to python_in...@googlegroups.com
Ohh great!! Nice and clean code.
Thank you!
Reply all
Reply to author
Forward
0 new messages