Jump to content

How to run ftrack so I can utilize logging?


Mike

Recommended Posts

I've never utilized a debugger or logging system, so I'm not sure how to go about this.  I've seen the logging imports all throughout the boilerplate code and my usual method of printing variables doesn't return any values when running ftrack from my visual studio project... So I think its time to start taking advantage of the logging system.  So how would I got about initializing the logging system when running from a python project?  Here's an example for how I'm launching ftrack. Sorry for the formatting, for some reason I don't have access to any of the text editing options: 

def main():
	
	subprocess.Popen([r"{0}/ftrack_connect_package.exe".format(connect_path)])

if __name__ == "__main__":
	main()

 

Link to comment
Share on other sites

Below works and outputs info into an existing .txt file.  In the future I'd like to get this to a place where my script creates a .txt file every time the program is run and pumps logging information into that. 

import logging
import os

import ftrack


class MessageAction(object):
	'''Launch Message action.'''

	# Unique action identifier.
	identifier = 'my-message-launch-action'

	def __init__(self):
		'''Initialise action.'''
		super(MessageAction, self).__init__()
		

		if self.identifier is None:
			raise ValueError('The action must be given an identifier.')

	def register(self):
		'''Register action.'''
		ftrack.EVENT_HUB.subscribe(
			'topic=ftrack.action.discover',
			self.discover
		)

		ftrack.EVENT_HUB.subscribe(
			'topic=ftrack.action.launch and data.actionIdentifier={0}'.format(
				self.identifier
			),
			self.launch
		)

	def discover(self, event):
		'''Return action based on *event*.'''
		return {
			'items': [{
				'label': 'Message',
				'actionIdentifier': self.identifier
			}]
		}

	def launch(self, event):
		'''Callback method for Message action.'''

		log_handler = logging.FileHandler(os.getenv('LOG_ROOT_DIR') + '/log_file.txt')
		log_handler.setFormatter(logging.Formatter( '%(asctime)s %(levelname)s: %(message)s ' '[in %(pathname)s:%(lineno)d]'))
		applogger = logging.getLogger('mb')
		applogger.setLevel(logging.DEBUG)
		applogger.addHandler(log_handler)
		applogger.info('Action executed for {0}'.format(event))

		return {
			'success': True,
			'message': 'Hello Mike!'
		}


def register(registry, **kw):
	'''Register action in Connect.'''

	# Validate that registry is the correct ftrack.Registry. If not,
	# assume that register is being called with another purpose or from a
	# new or incompatible API and return without doing anything.
	if registry is not ftrack.EVENT_HANDLERS:
		# Exit to avoid registering this plugin again.
		return

	action = MessageAction()
	action.register()

 

Link to comment
Share on other sites

UPDATE- almost there, but need a bit more help:

I found this to be a great resource for learning the ins and outs of logging: https://docs.python.org/3/howto/logging.html#configuring-logging

After doing those tutorials, I've set up a custom logger module within my package.  However I'm unable to get the logger to successfully import into my ftrack hook modules.  When I insert the import statement into the action, it just doesn't show up when connect start.  I don't have any logs to see what the error is. Could anyone let me know if the problem is due to an error I'm making with python or is this related to ftrack? 

My project is set up like so:
 

+Root/
	+bin/
		-launcher.py
	+modules/
		+ftrack/
			+ftrack-connect-plugins/
				+message_action/
					+hook/
						+action/
							-message_action.py
        +tools/
            +my-logger/
                +source/
                    +my_logger/
                        -__init__.py
                        -my_logging.py

For example, I'm adding the modules/, tools/, and my-logger/source/ to the pythonpath like this:

INIT_MODULES_DIR = os.path.join(_ROOT_DIR, 'modules')
if not INIT_MODULES_DIR in sys.path:
    sys.path.append( INIT_MODULES_DIR )

I can easily get my_logger to import into the launcher.py module just by running this :

from my_logger import my_logging
logger = my_logging.getLogger(__name__)

I've spent a several days trying to get my_logging.py to import into message_action.py, but nothing is working! I can't tell if this is just a simple python issue, or if ftrack needs more information from me. Thanks!

Link to comment
Share on other sites

Hi Mike, I cannot see any immediate problems with your solution.  If the _ROOT_DIR is correct and your adding tools/my-logger/source/ to the sys.path it should get picked up :-/

We've merged the logging feature that will log warnings and errors to file from Connect, but we haven't built and released it yet.

Link to comment
Share on other sites

  • 4 weeks later...

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...