How to use create custom logs by using the output texts from Synapse notebook?

Liang Chen 1 Reputation point Microsoft Vendor
2022-04-27T00:28:44.247+00:00

Hi,

I am developing a Synapse notebook, and I want to use an output from Synapse notebook to create custom logs in Azure Log Analytics.

For example:

In a Synapse notebook, I define a string variable s = 'Cannot get the file status', and I want to output this text message into custom logs in Azure Log Analytics every time when I run this Synapse notebook.

How can I realize this?

Thanks,
Liang

Azure Monitor
Azure Monitor
An Azure service that is used to collect, analyze, and act on telemetry data from Azure and on-premises environments.
3,408 questions
Azure Synapse Analytics
Azure Synapse Analytics
An Azure analytics service that brings together data integration, enterprise data warehousing, and big data analytics. Previously known as Azure SQL Data Warehouse.
5,117 questions
{count} votes

1 answer

Sort by: Most helpful
  1. PRADEEPCHEEKATLA 90,496 Reputation points
    2022-04-27T11:33:58.467+00:00

    Hello @Liang Chen ,

    Thanks for the question and using MS Q&A platform.

    You may try with below sample code for python logging with different format and customized log level.

     import logging  
          
     # Customize the logging format for all loggers  
     FORMAT = "%(asctime)s - %(name)s - %(levelname)s - %(message)s"  
     formatter = logging.Formatter(fmt=FORMAT)  
     for handler in logging.getLogger().handlers:  
         handler.setFormatter(formatter)  
          
     # Customize log level for all loggers  
     logging.getLogger().setLevel(logging.INFO)  
          
     # Customize the log level for a specific logger  
     customizedLogger = logging.getLogger('customized')  
     customizedLogger.setLevel(logging.WARNING)  
          
     # logger that use the default global log level  
     defaultLogger = logging.getLogger('default')  
          
     defaultLogger.debug("default debug message")  
     defaultLogger.info("default info message")  
     defaultLogger.warning("default warning message")  
     defaultLogger.error("default error message")  
     defaultLogger.critical("default critical message")  
          
     # logger that use the customized log level  
     customizedLogger.debug("customized debug message")  
     customizedLogger.info("customized info message")  
     customizedLogger.warning("customized warning message")  
     customizedLogger.error("customized error message")  
     customizedLogger.critical("customized critical message")  
    

    Here is the expected output for the above sample code for python logging with different format and customized log level.

    197011-image.png

    Hope this will help. Please let us know if any further queries.

    ------------------------------

    • Please don't forget to click on 130616-image.png or upvote 130671-image.png button whenever the information provided helps you. Original posters help the community find answers faster by identifying the correct answer. Here is how
    • Want a reminder to come back and check responses? Here is how to subscribe to a notification
    • If you are interested in joining the VM program and help shape the future of Q&A: Here is how you can be part of Q&A Volunteer Moderators

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.