Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save alisson276/5b7fdff0c5663fee4c4023c1bbbb3554 to your computer and use it in GitHub Desktop.
Save alisson276/5b7fdff0c5663fee4c4023c1bbbb3554 to your computer and use it in GitHub Desktop.

Revisions

  1. @gene1wood gene1wood revised this gist Aug 12, 2019. 1 changed file with 4 additions and 4 deletions.
    8 changes: 4 additions & 4 deletions log_aws_lambda_event_and_context.py
    Original file line number Diff line number Diff line change
    @@ -1,3 +1,4 @@
    from __future__ import unicode_literals
    import logging
    import json

    @@ -17,17 +18,16 @@ class PythonObjectEncoder(json.JSONEncoder):
    """

    def default(self, obj):
    if isinstance(obj,
    (list, dict, str, unicode,
    int, float, bool, type(None))):
    if isinstance(obj, (list, dict, str, int, float, bool, type(None))):
    return json.JSONEncoder.default(self, obj)
    elif hasattr(obj, '__repr__'):
    return obj.__repr__()
    else:
    return json.JSONEncoder.default(self, obj.__repr__())


    def lambda_handler(event, context):
    logger.info('Event: %s' % json.dumps(event))
    logger.info('Context: %s' %
    json.dumps(vars(context), cls=PythonObjectEncoder))
    return True
    return True
  2. @gene1wood gene1wood created this gist Mar 4, 2016.
    33 changes: 33 additions & 0 deletions log_aws_lambda_event_and_context.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,33 @@
    import logging
    import json

    logger = logging.getLogger(__name__)
    logging.getLogger().setLevel(logging.INFO)


    class PythonObjectEncoder(json.JSONEncoder):
    """Custom JSON Encoder that allows encoding of un-serializable objects
    For object types which the json module cannot natively serialize, if the
    object type has a __repr__ method, serialize that string instead.
    Usage:
    >>> example_unserializable_object = {'example': set([1,2,3])}
    >>> print(json.dumps(example_unserializable_object,
    cls=PythonObjectEncoder))
    {"example": "set([1, 2, 3])"}
    """

    def default(self, obj):
    if isinstance(obj,
    (list, dict, str, unicode,
    int, float, bool, type(None))):
    return json.JSONEncoder.default(self, obj)
    elif hasattr(obj, '__repr__'):
    return obj.__repr__()
    else:
    return json.JSONEncoder.default(self, obj.__repr__())

    def lambda_handler(event, context):
    logger.info('Event: %s' % json.dumps(event))
    logger.info('Context: %s' %
    json.dumps(vars(context), cls=PythonObjectEncoder))
    return True