Package: bb-logger
BB LOGGERDescriptionRemove all logger handlers and reformat log record ( can be extended )Set noti status, controlable by argumentsOverride lambda raise error to critical and notice if noti status = True, optionalHowcall setup_logging() one time at begin of programusing decorator setup_logging_dec() with each lambdaParamsdefault_level(int, optional): min log level. Defaults to logging.WARNING.extend_format(str, optional): custom extend format. Defaults to None.lambda_exec_error_log(bool, optional): log with critical level for lambda raise exception. Defaults to True. only with decoratorsetup_logging_dec()default_noti_level(int, optional): all log from this level will be noticed if no arguments are provided. Defaults to logging.ERRORforce_noti_level(int, optional): force all log with this level to be noticed. Defaults to logging.ERROR.Notes(*) extend_format logiclogging format details can behereBASE_FORMAT = '[%(levelname)s]'DEFAULT_FORMAT = BASE_FORMAT + ' %(message)s'if extend_format is specified, FORMAT will be : BASE_FORMAT + '\t'+ extend_formatelse DEFAULT_FORMAT wil be usedEXAMPLES1. Remove all logger handlers and reformat log recordsample codeimportloggingfrombb_loggerimportsetup_logginglogging.error('before setup, using old handler format')# setup log with defaultsetup_logging()logging.error('after setup, using new default format')# setup logger with extend formatsetup_logging(extend_format="%(asctime)s-%(message)s")logging.error('after setup with custom format, using extended format')outputERROR:root:beforesetup,usingoldhandlerformat[ERROR]aftersetup,usingnewdefaultformat[ERROR]2021-01-1018:18:46,202-aftersetupwithcustomformat,usingextendedformat2. Set noti statusADD NOTI STATUS TO LOG BASE ON CONDITIONS(*)force_noti_level: default is logging.ERROREXAMPLEimportloggingfrombb_loggerimportsetup_logginglogging.error('before setup')# setup log with defaultsetup_logging()logging.error('after setup, noti status was added')# specify if this log will be noticed or notlogging.warning('warning with noti True',{'noti':True})logging.warning('warning with noti False',{'noti':False})# set force noti levelsetup_logging(default_level=logging.INFO,force_noti_level=logging.INFO)logging.info('info with noti True',{'noti':True})logging.info('info with noti False stil have NOTI status',{'noti':False})output[ERROR]2021-02-06T08:01:56.558Zc4c3fad9-eacf-41f3-bba1-15b119bfd980beforesetup[ERROR][NOTI]aftersetup,notistatuswasadded[WARNING][NOTI]warningwithnotiTrue[WARNING][NOT_NOTI]warningwithnotiFalse[INFO][NOTI]infowithnotiTrue[INFO][NOTI]infowithnotiFalsestilhaveNOTIstatusOverride lambda raise error to criticalEXAMPLEfrombb_loggerimportsetup_logging,setup_logging_dec@setup_logging_dec(default_level=logging.INFO,lambda_exec_error_log=True,force_noti_level=logging.ERROR)deflambda_handler(event,context):a=1/0output[CRITICAL][NOTI]divisionbyzero
Traceback(mostrecentcalllast):File"/var/task/bb_logger.py",line98,inwrapperfunc(*args,**kwargs)File"/var/task/lambda_function.py",line12,inlambda_handlera=1/0exception raise by lambda will be set at Critical level and noticedNOTECloudWatch now accept following pattern:[LEVEL] [NOTI]Example:"[ERROR] [NOTI] error log" will be matched"some thing before [ERROR] [NOTI] error log" will be matched