Package: fasal-logger
InstallationInstall the packagefasal-loggerusing the following commandpipinstall--upgradefasal-loggerConfigurationCreate a filelogger.yml. Copy the contents from the repository and make the necessary changes (If needed)version:1disable_existing_loggers:noformatters:simple:format:"%(asctime)s-%(name)s-%(levelname)s-%(module)s:%(lineno)d-%(message)s"datefmt:'%Y-%m-%d%H:%M:%S'fasalFormat:():fasal_logger.FasalStdlibFormatterdatefmt:'%Y-%m-%d%H:%M:%S'extra:{"author":"${USER}"}exclude_fields:[ecs,log.original]handlers:console:class:logging.StreamHandlerlevel:DEBUGformatter:fasalFormatstream:ext://sys.stdoutfile:class:logging.handlers.RotatingFileHandlerlevel:DEBUGformatter:fasalFormatfilename:'logging.example.log'mode:amaxBytes:1000000# 1 MBbackupCount:2encoding:utf8root:level:DEBUGhandlers:[console]propogate:nologgers:fasalLogger:level:DEBUGhandlers:[console]propagate:noThelogger.ymldefined here defaults to console. In order to log the contents into a file, make the required changes to thelogger.yml.root:handlers:[console,file]loggers:fasalLogger:handlers:[console,file]And change the filename and location.handler:file:filename:'logging.example.log'UsageIn order to use the logger in your code, add the following piece of code at the top of your .py fileNote:Get the webhook from the infra team to able to send messages to slack to any other channel. (By default using #fasal-ai-infra)importloggingimportlogging.configimportosfromfasal_loggerimportLoggerInitializer,SlackNotificationlogging.captureWarnings(True)logger=logging.getLogger(__name__)logger_init=LoggerInitializer()logger_init(logger=logger,config='path_to_logger.yml')slk=SlackNotification()# set parameter for webhook, DEV (if needed)# Use logger nowlogger.info("Logger set")# Send a message to slack channelslk.notify(message="testing")Variables taken from environment are:Set these variables as required in the file used to read environment variable.Note: You can keep env variables in an yaml filelocal.ymland usepython-dotenv-yamllibrary to read itSLACK_WEBHOOK: Channel webhook trigger, defaults to slack channel #fasal-ai-infraDEV: If True, no message is send to slackENV: logger environment (staging/production/development)Another example usage:importloggingimportlogging.configimportosfromfunctoolsimportreducefromfasal_loggerimportLoggerInitializer,SlackNotificationlogging.captureWarnings(True)logger=logging.getLogger(__name__)logger_init=LoggerInitializer()logger_init(logger=logger,config='logger.yml')slk=SlackNotification(DEV=True)high_level_stuff={'custom_tag':'tagged-at-line'}logger.info("Starting the function",extra={'tag':'xxx'})defmy_sum(a,b):ifisinstance(a,str):logger.warning("This is not int")logger.info(f'performing addition of{a},{b}')try:result=a+blogger.info(f'result:{result}')exceptExceptionaserr:logger.error(err)raiseerrreturnresultdefrun_request():importrequestslogger.info("request")ploads={'things':2,'total':25}r=requests.get('https://httpbin.org/get',params=ploads)param=[1,2,3]result=reduce(my_sum,param)logger.info(f"final:{result}",extra=high_level_stuff)run_request()slk.notify(message="this is test")Note:Use logger.info() with first argument as stringIn order to pass another set of key: value pairs to logger.info / logger.warnings / logger.debugPass the key: value pair to logger.yamlformatters:fasalFormat:():fasal_logger.FasalStdlibFormatterdatefmt:'%Y-%m-%d%H:%M:%S'extra:{"author":"${USER}","key-1":"value-1","key-2":"value-2"}Pass a dictionary to argumentextrainside logger.info(). However, doing so will only effect the logger's log where its added and adding inlogger.yamlwill effect it globallylogger.info("my message",extra={'key-1':'value-1'})Build and Published using (poetry)[https://python-poetry.org/docs/cli/#publish]