Using logging in an add-in

Using logging in an add-in

pludikar
Collaborator Collaborator
1,234 Views
4 Replies
Message 1 of 5

Using logging in an add-in

pludikar
Collaborator
Collaborator

Hi 

 

I'm trying to include logging into an add-in that I'm writing and it's not behaving as I would expect.  Does anyone have any experience including a logger into their app?

 

I'm finding that I can't create a file with type ".log" - it always ends up ".txt".  I can create other file types, just not ".log".  I'm also experiencing multiple copies of the log record blocks. 

 

I know that multiple handlers can write the same record multiple times - hence the  

        for handle in logger.handlers:
            logger.removeHandler(handle)
            handle.close()

(from code below) to ensure that I start from fresh.  I'm getting a block of records 1->N, then the same block again 1->N (sometimes multiple times).  If there were multiple handlers I'd get records 1,1,1,1; 2,2,2,2; 3,3,3,3 etc.

 

It almost feels like I'm conflicting with f360.  So maybe I'm missing something.

 

Anyone have any suggestions, I'd appreciate hearing them.

 

Regards

Peter

 

here's a code snippet to illustrate what I'm doing.

import logging, adsk.core, adsk.fusion, adsk.cam, traceback, os

def run(context):
    appPath = os.path.dirname(os.path.abspath(__file__))
    ui = None
    logger = logging.getLogger(__name__)
    logger.setLevel(logging.DEBUG)
    formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
    logHandler = logging.FileHandler(os.path.join(appPath, 'logTest.log'), mode='w')
    logHandler.setFormatter(formatter)
    logHandler.flush()
    logger.addHandler(logHandler)
    logger.info('hi there')


    try:
        app = adsk.core.Application.get()
        
        for handle in logger.handlers:
            logger.removeHandler(handle)
            handle.close()
    except:
        if ui:
            ui.messageBox('Failed:\n{}'.format(traceback.format_exc()))

 

 

I'm not an expert, but I know enough to be very, very dangerous.

Life long R&D Engineer (retired after 30+ years in Military Communications, Aerospace Robotics and Transport Automation).
1,235 Views
4 Replies
Replies (4)
Message 2 of 5

goyals
Autodesk
Autodesk

Hi,

 

Thank you for reporting this. We really appreciate the feedback provided by Fusion users. 

 

I just tried the code you provided in your post on Mac machine but not able to see both the issues. It creates the file with extension *.log correctly with the records only written once. It will be helpful if there is some extra information to share. I will also try once on Windows machine to see if it is a platform specific issue.

 

Regards,

Shyam Goyal



Shyam Goyal
Sr. Software Dev. Manager
Message 3 of 5

pludikar
Collaborator
Collaborator

@goyals

 

Hi,

 

I'm using a windows machine, and after some digging it appears that Windows declares a *.log file as a text file.  It doesn't seem to do it to any other type of file.  So, my first problem is resolved.

 

Attached is a copy of a log from my add-in - if you scroll down you can see it starts to repeat.  You can find a full copy of the add-in here.  If you need a model to run it on it's attached to - the resultant log file will be in the add-in directory.

 

Regards

Peter

I'm not an expert, but I know enough to be very, very dangerous.

Life long R&D Engineer (retired after 30+ years in Military Communications, Aerospace Robotics and Transport Automation).
0 Likes
Message 4 of 5

goyals
Autodesk
Autodesk

Hi @pludikar

 

Thank you for sharing your add-in. I cloned your git repo but I am not seeing any usage of logger in your code. I am just guessing here. Are you using logger in some callback handler which got called from Fusion based on some event? Is it possible that you are getting the same information again which might be fine in certain cases. Consider you are making the selection and filtering out the selection in your add-in so if you are logging the information in your callback function then same information will be entered again and again the moment you hover over the same entity in Fusion.

 

Regards,

Shyam Goyal



Shyam Goyal
Sr. Software Dev. Manager
0 Likes
Message 5 of 5

pludikar
Collaborator
Collaborator

@goyals

 

Not sure if you activated the logger - you need to do that from the add-in dialog box.  Select debug.

 

You also need to select a face (or multiple faces) that have acute 90 degree corners (ones going into the body).  Once the first face is selected, the add-in will only allow you to select faces that are parallel.  Then press ok.  The log will be in the add-in folder.

 

 

However, I think I've resolved the issue.  I had a 

logger = logging.getLogger(__name__)

 in a separate class that is in the same file as my main class.  I had assumed that the initiation of the logging function was class dependant and not file dependant.  For example - this is the cause of the problem:

 

class One:
      def __init__(self)
           logger = logging.getLogger(__name__)

class Two:
      def __init__(self)
           logger = logging.getLogger(__name__)

 But this does lead to another question:  What is the proper way to initiate a logger in a class?

 

Regards

Peter

 

I'm not an expert, but I know enough to be very, very dangerous.

Life long R&D Engineer (retired after 30+ years in Military Communications, Aerospace Robotics and Transport Automation).
0 Likes