Skip to content

Commit 00a07d4

Browse files
committed
chore: comments, flag rename to be more explicit
1 parent cd93938 commit 00a07d4

File tree

1 file changed

+17
-11
lines changed

1 file changed

+17
-11
lines changed

aws_lambda_powertools/logging/formatter.py

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ def __init__(
6262
json_deserializer: Optional[Callable[[Union[Dict, str, bool, int, float]], str]] = None,
6363
json_default: Optional[Callable[[Any], Any]] = None,
6464
datefmt: Optional[str] = None,
65-
use_datetime: bool = False,
65+
use_datetime_directive: bool = False,
6666
log_record_order: Optional[List[str]] = None,
6767
utc: bool = False,
6868
**kwargs,
@@ -91,9 +91,9 @@ def __init__(
9191
String directives (strftime) to format log timestamp.
9292
9393
See https://docs.python.org/3/library/time.html#time.strftime or
94-
use_datetime: str, optional
94+
use_datetime_directive: str, optional
9595
Interpret `datefmt` as a format string for `datetime.datetime.strftime`, rather than
96-
`time.strftime`.
96+
`time.strftime` - Only useful when used alongside `datefmt`.
9797
9898
See https://docs.python.org/3/library/datetime.html#strftime-strptime-behavior . This
9999
also supports a custom %F directive for milliseconds.
@@ -110,7 +110,7 @@ def __init__(
110110
self.json_serializer = json_serializer or partial(json.dumps, default=self.json_default, separators=(",", ":"))
111111

112112
self.datefmt = datefmt
113-
self.use_datetime = use_datetime
113+
self.use_datetime = use_datetime_directive
114114

115115
self.utc = utc
116116
self.log_record_order = log_record_order or ["level", "location", "message", "timestamp"]
@@ -150,19 +150,25 @@ def formatTime(self, record: logging.LogRecord, datefmt: Optional[str] = None) -
150150
# Reason 2 is that std logging doesn't support msec after TZ
151151
msecs = "%03d" % record.msecs
152152

153-
if self.use_datetime:
153+
# Datetime format codes might be optionally used
154+
# however it only makes a difference if `datefmt` is passed
155+
# since format codes are the same except %f
156+
if self.use_datetime and datefmt:
157+
# record.msecs are microseconds, divide by 1000 and we get milliseconds
154158
timestamp = record.created + record.msecs / 1000
155159

156-
dt = datetime.fromtimestamp(timestamp, tz=timezone.utc)
157-
if not self.utc:
158-
# convert back to local time
159-
dt = dt.astimezone()
160+
if self.utc:
161+
dt = datetime.fromtimestamp(timestamp, tz=timezone.utc)
162+
else:
163+
# make sure local timezone is included
164+
dt = datetime.fromtimestamp(timestamp).astimezone()
160165

161-
custom_fmt = (datefmt or self.default_time_format).replace(self.custom_ms_time_directive, msecs)
166+
custom_fmt = datefmt.replace(self.custom_ms_time_directive, msecs)
162167
return dt.strftime(custom_fmt)
163168

164169
elif datefmt:
165-
return time.strftime(datefmt, record_ts)
170+
custom_fmt = datefmt.replace(self.custom_ms_time_directive, msecs)
171+
return time.strftime(custom_fmt, record_ts)
166172

167173
custom_fmt = self.default_time_format.replace(self.custom_ms_time_directive, msecs)
168174
return time.strftime(custom_fmt, record_ts)

0 commit comments

Comments
 (0)