Global Maya Logger


As a TD, you are often asked to trace a bug or a crash, and it is way more easy when you have all the actions and logs done by the user/software. To do so, you usually have to modify the userSetup.mel of the user when you can and begin the logging… Then remove it when you are done.

The Maya command cmdFileOutput is quite handy for that.

What a hassle to do that for every single user each time you want to trace and debug something. It can be very cumbersome especially if you have a lot of users to manage. So what about a global and automatic solution that activate only for specific users at a time? All you need to do is to put the script in the global startup of Maya, and activate it only if the username is in a specific file that you can access and modify very easily.

import os
import time
import json
import errno
import getpass
import maya.cmds as cmds


def createDir(path):
    try:
        os.makedirs(path)
    except OSError as exception:
        if exception.errno != errno.EEXIST:
            raise

def mayaLogger():
    mainFolder = '/your/path/logs/'
    userLogin  = getpass.getuser()
    with open(mainFolder + 'users.json') as f:
        userDictionary = json.load(f)
    if userLogin in userDictionary:
        project = 'UberMovieTitle'
        mayaVersion = cmds.about(version=True)
        folderYearMonth = time.strftime("%Y-%m")
        timeNow = time.strftime("%Y-%m-%d_%H:%M:%S")  # Windows doesn't accept ':' in filenames so you need to change it
        folderPath = os.path.join(mainFolder, project, userLogin, folderYearMonth)
        fileName = timeNow + '__maya' + mayaVersion.replace(" ", "-") + '.log'
        filePath = os.path.join(folderPath, fileName).replace('\\', '/')  # This won't work on windows if you have some "\" character
        createDir(folderPath)
        cmds.cmdFileOutput(open=filePath)

Now if you put a username in the Json file, it will log his session the next time he launch Maya.

You don’t really want to activate it for every users as it could be a strain on the network if you save logs there, and it may bring unknown issues (even though I didn’t see one yet, even though I log every one of my Maya sessions since a year).

users.json:

{
    "regnareb": "",
    "captain3d": ""
}

You can improve it by getting the pid of the application, information about the computer, adding a strace, activate different kinds of strace with the Json file, etc..