Thursday, March 31, 2022

Logging_Example_Explanation --- import logging

 import logging

# This sets the root logger to write to stdout (your console).
# Your script/app needs to call this somewhere at least once.
#logging.basicConfig()

# By default the root logger is set to WARNING and all loggers you define
# inherit that value. Here we set the root logger to NOTSET. This logging
# level is automatically inherited by all existing and new sub-loggers
# that do not set a less verbose level.
logging.root.setLevel(logging.NOTSET)

# The following line sets the root logger level as well.
# It's equivalent to both previous statements combined:
#logging.basicConfig(level=logging.NOTSET)


logger = logging.getLogger("my-app")
f_handler = logging.FileHandler('main-file.log')
f_handler.setLevel(logging.INFO)
f_format = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
f_handler.setFormatter(f_format)
logger.addHandler(f_handler)


# Convenient methods in order of verbosity from highest to lowest
logger.debug("this will get printed")
logger.info("this will get printed")
logger.warning("this will get printed")
logger.error("this will get printed")
logger.critical("this will get printed")
name = 'John'
logger.info('%s raised an error', name)
logger.info('%s raised an information',name)

a = 5
b = 0

try:
c = a / b
except Exception as e:
logger.error("Exception occurred", exc_info=True)

a = 5
b = 0
try:
c = a / b
except Exception as e:
logger.exception("Exception occurred")

# In large applications where you would like more control over the logging,
# create sub-loggers from your main application logger.
component_a_logger = logger.getChild("component-a")
f_component_a_handler = logging.FileHandler('compoment_a-file.log')
f_component_a_handler.setLevel(logging.ERROR)
f_component_a_handler.setFormatter(f_format)
component_a_logger.addHandler(f_component_a_handler)
component_a_logger.info("INFO: this will get printed with the prefix `my-app.component-a`")
component_a_logger.error("ERROR: this will get printed with the prefix `my-app.component-a`")

# If you wish to control the logging levels, you can set the level anywhere
# in the hierarchy:
#
# - root
# - my-app
# - component-a
#


# In large applications where you would like more control over the logging,
# create sub-loggers from your main application logger.
component_b_logger = logger.getChild("component-b")
f_component_b_handler = logging.FileHandler('compoment_b-file.log')
f_component_b_handler.setLevel(logging.ERROR)
f_component_b_handler.setFormatter(f_format)
component_b_logger.addHandler(f_component_b_handler)
component_b_logger.info(" INFO: this will get printed with the prefix `my-app.component-b`")
component_b_logger.error("ERROR: this will get printed with the prefix `my-app.component-b`")

# If you wish to control the logging levels, you can set the level anywhere
# in the hierarchy:
#
# - root
# - my-app
# - component-b
#


'''
# Example for development:
logger.setLevel(logging.DEBUG)

# If that prints too much, enable debug printing only for your component:
component_logger.setLevel(logging.DEBUG)

# For production you rather want:
logger.setLevel(logging.WARNING)
'''


No comments:

Post a Comment

Recent Post

Databricks Delta table merge Example

here's some sample code that demonstrates a merge operation on a Delta table using PySpark:   from pyspark.sql import SparkSession # cre...