Using logging in an add-in
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report
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()))
Life long R&D Engineer (retired after 30+ years in Military Communications, Aerospace Robotics and Transport Automation).
