Package: logger-cloudwatch-structlog
Logger Cloudwatch StructlogLogger Cloudwatch Structlog is a Python package that contain that allows logging in anAWS CloudWatch Logcompatible way using a JSON format inserverless services in AWS(e.g., AWS Lambda). It is easily readable by humans and machines. This library is a wrapper for structlog, you can replicate all the functionalities just usingstructlog. We decided to make this library to make easier our developments.Installation and updatingUse the package managerpipto installLogger Cloudwatch Structloglike below.pipinstalllogger_cloudwatch_structlogUsageThis library has two structlog processors and functions that configure and returns a logger ready to use in your application. Also, we provided a one-fits-all solution that you can use without taking into account any change.ProcessorsPasswordCensorProcessor that censor words inevent_dict. If you log information has words that are needed to be censured, like passwords, etc., this processor replaces them with"*CENSORED*". You have to provide a list of string of the key to be censured as argumentwordlist.fromlogger_cloudwatch_structlogimportPasswordCensorimportstructlogprocessors=[...PasswordCensor(wordlist=["password","user"])...]structlog.configure(processors=processors,...)logger=structlog.get_logger()logger.info("msg",peer='127.0.0.1',password="password",user="alice")This would result among other the following lines to be printed:event='msg'peer='127.0.0.1'user='*CENSORED*'password='*CENSORED*'AWSCloudWatchLogsRender a log line compatible with AWS CloudWatch Logs.  This is a copy and modification ofstructlog.processors.JSONRenderer. Render theevent_dictusingserializer(event_dict, **json_kw).  This class is fromserverless-zoom-recordings.Arguments:callouts: List of strings, they are printed in clear-text on the front of the log line. Only the first two items of this list are called out. Defaults toNone.serializer: Ajson.dumps-compatible callable that will be used to format the string. Defaults tojson.dumps.**dumps_kw: Arbitrary keyword arguments. Are passed unmodified toserializer.fromlogger_cloudwatch_structlogimportAWSCloudWatchLogsimportstructlogimportjsonprocessors=[...AWSCloudWatchLogs(callouts=["status","event"],serializer=json.dumps,sort_keys=False)]structlog.configure(processors=processors,...)logger=structlog.get_logger()logger.info("msg",status="wut",peer='127.0.0.1',password="password",user="alice")This would result among other the following lines to be printed:[INFO]"wut""msg"{"event":"msg","status":"wut","peer":"'127.0.0.1'","password":"password","user":"alice"}Functionssetup_logging → Configure logging for the application.get_logger → Convenience function that returns a structlog logger.setup_and_get_logger →  Configure logging for the application and return the logger. This function is a one-fits-all solution with some possibilities to change the setup.setup_logging()This function configure the logging for the application. It has multiple arguments that allow you to have a flexible configuration.Arguments:wordlist_to_censor: List with words to be censored in the event_dict, if they are present. Defaults toNone.callouts: List of strings, they are printed in clear-text on the front of the log line. Only the first two items of this list are called out. Defaults toNone.serializer: Ajson.dumps-compatible callable that will be used to format the string. Defaults tojson.dumps.level: Sets the threshold for this logger to level. Logging messages which are less severe than level will be ignored. Defaults tologging.INFO.noisy_log_sources: Tuple of sources that output a lot of unnecessary messages. Defaults to("boto", "boto3", "botocore").**serializer_kw: Arbitrary keyword arguments. Are passed unmodified toserializer.get_logger()Convenience function that returns a structlog logger.Arguments:*args: Positional arguments that are passed unmodified to the logger factory. Therefore, it depends on the factory what they mean.**initial_values: Values that are used to pre-populate the context.setup_and_get_logger()Configure logging for the application and return the logger. This function is a one-fits-all solution with some possibilities to change the setup. But you cannot add keyword arguments for the logger factory, or even values that are used to pre-populate the context. If you need a more flexible solution, you can call setup_logging() and get_logger() separated.Example,fromlogger_cloudwatch_structlogimportsetup_and_get_logger,AWSCloudWatchLogsimportstructlogimportloggingprocessors=[structlog.stdlib.add_log_level,structlog.processors.TimeStamper(fmt="iso"),structlog.processors.UnicodeDecoder(),AWSCloudWatchLogs(callouts=["event","context"])]logger=setup_and_get_logger(level=logging.INFO,processors=processors)One-fits-all solutionIf you want to use this library without any change, we provide a one-fit-solution. If you callsetup_and_get_logger()without argument, you have a ready to use logger that works with AWS CloudWatch Log.fromlogger_cloudwatch_structlogimportsetup_and_get_loggerlogger=setup_and_get_logger()The one-fits-all solution has the next configuration:wordlist_to_censor=Nonecallouts=["status_code","event"]serializer=json.dumpslevel=logging.INFOnoisy_log_sources=("boto","boto3","botocore")processors=(structlog.stdlib.filter_by_level,structlog.stdlib.add_log_level,structlog.stdlib.PositionalArgumentsFormatter(),structlog.processors.TimeStamper(fmt="iso"),structlog.processors.StackInfoRenderer(),structlog.processors.format_exc_info,structlog.processors.UnicodeDecoder(),PasswordCensor(wordlist=wordlist_to_censor),structlog.threadlocal.merge_threadlocal,AWSCloudWatchLogs(callouts=["status_code","event"],serializer=json.dumps,sort_keys=False))If you want words to be censored, just add the list in the functionfromlogger_cloudwatch_structlogimportsetup_and_get_loggerlogger=setup_and_get_logger(wordlist_to_censor=["password"])LicenseApache 2.0