@@ -62,7 +62,7 @@ def __init__(
62
62
json_deserializer : Optional [Callable [[Union [Dict , str , bool , int , float ]], str ]] = None ,
63
63
json_default : Optional [Callable [[Any ], Any ]] = None ,
64
64
datefmt : Optional [str ] = None ,
65
- use_datetime : bool = False ,
65
+ use_datetime_directive : bool = False ,
66
66
log_record_order : Optional [List [str ]] = None ,
67
67
utc : bool = False ,
68
68
** kwargs ,
@@ -91,9 +91,9 @@ def __init__(
91
91
String directives (strftime) to format log timestamp.
92
92
93
93
See https://docs.python.org/3/library/time.html#time.strftime or
94
- use_datetime : str, optional
94
+ use_datetime_directive : str, optional
95
95
Interpret `datefmt` as a format string for `datetime.datetime.strftime`, rather than
96
- `time.strftime`.
96
+ `time.strftime` - Only useful when used alongside `datefmt` .
97
97
98
98
See https://docs.python.org/3/library/datetime.html#strftime-strptime-behavior . This
99
99
also supports a custom %F directive for milliseconds.
@@ -110,7 +110,7 @@ def __init__(
110
110
self .json_serializer = json_serializer or partial (json .dumps , default = self .json_default , separators = ("," , ":" ))
111
111
112
112
self .datefmt = datefmt
113
- self .use_datetime = use_datetime
113
+ self .use_datetime = use_datetime_directive
114
114
115
115
self .utc = utc
116
116
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) -
150
150
# Reason 2 is that std logging doesn't support msec after TZ
151
151
msecs = "%03d" % record .msecs
152
152
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
154
158
timestamp = record .created + record .msecs / 1000
155
159
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 ()
160
165
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 )
162
167
return dt .strftime (custom_fmt )
163
168
164
169
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 )
166
172
167
173
custom_fmt = self .default_time_format .replace (self .custom_ms_time_directive , msecs )
168
174
return time .strftime (custom_fmt , record_ts )
0 commit comments