-
Notifications
You must be signed in to change notification settings - Fork 2
/
aws_lambda_logging.py
132 lines (102 loc) · 4.15 KB
/
aws_lambda_logging.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
"""Microlibrary to simplify logging in AWS Lambda."""
import json
import logging
import os
from functools import wraps
def json_formatter(obj):
"""Formatter for unserialisable values."""
return str(obj)
class JsonFormatter(logging.Formatter):
"""AWS Lambda Logging formatter.
Formats the log message as a JSON encoded string. If the message is a
dict it will be used directly. If the message can be parsed as JSON, then
the parse d value is used in the output record.
"""
def __init__(self, **kwargs):
"""Return a JsonFormatter instance.
The `json_default` kwarg is used to specify a formatter for otherwise
unserialisable values. It must not throw. Defaults to a function that
coerces the value to a string.
Other kwargs are used to specify log field format strings.
"""
datefmt = kwargs.pop('datefmt', None)
super(JsonFormatter, self).__init__(datefmt=datefmt)
self.format_dict = {
'timestamp': '%(asctime)s',
'level': '%(levelname)s',
'location': '%(filename)s.%(funcName)s:%(lineno)d',
# 'location': '%(name)s.%(funcName)s:%(lineno)d',
}
self.format_dict.update(kwargs)
self.default_json_formatter = kwargs.pop(
'json_default', json_formatter)
def format(self, record):
record_dict = record.__dict__.copy()
record_dict['asctime'] = self.formatTime(record, self.datefmt)
log_dict = {
k: v % record_dict
for k, v in self.format_dict.items()
if v
}
if isinstance(record_dict['msg'], dict):
log_dict['message'] = record_dict['msg']
else:
log_dict['message'] = record.getMessage()
# Attempt to decode the message as JSON, if so, merge it with the
# overall message for clarity.
try:
log_dict['message'] = json.loads(log_dict['message'])
except (TypeError, ValueError):
pass
if record.exc_info:
# Cache the traceback text to avoid converting it multiple times
# (it's constant anyway)
# from logging.Formatter:format
if not record.exc_text:
record.exc_text = self.formatException(record.exc_info)
if record.exc_text:
log_dict['exception'] = record.exc_text
json_record = json.dumps(log_dict, default=self.default_json_formatter)
if hasattr(json_record, 'decode'): # pragma: no cover
json_record = json_record.decode('utf-8')
return json_record
def setup(level='DEBUG', formatter_cls=JsonFormatter,
boto_level=None, **kwargs):
"""Overall Metadata Formatting."""
if formatter_cls:
for handler in logging.root.handlers:
handler.setFormatter(formatter_cls(**kwargs))
try:
logging.root.setLevel(level)
except ValueError:
logging.root.error('Invalid log level: %s', level)
level = 'INFO'
logging.root.setLevel(level)
if not boto_level:
boto_level = level
try:
logging.getLogger('boto').setLevel(boto_level)
logging.getLogger('boto3').setLevel(boto_level)
logging.getLogger('botocore').setLevel(boto_level)
except ValueError:
logging.root.error('Invalid log level: %s', boto_level)
def wrap(lambda_handler):
"""Lambda handler decorator that setup logging when handler is called.
Adds ``request=context.request_id`` on all log messages.
From environment variables:
- ``log_level`` set the global log level (default to ``DEBUG``);
- ``boto_level`` set boto log level (default to ``WARN``);
"""
@wraps(lambda_handler)
def wrapper(event, context):
try:
request_id = event['requestContext']['requestId']
except (TypeError, KeyError):
request_id = getattr(context, 'aws_request_id', None)
setup(
level=os.getenv('log_level', 'DEBUG'),
request_id=request_id,
boto_level=os.getenv('boto_level', 'WARN'),
)
return lambda_handler(event, context)
return wrapper