@@ -144,6 +144,25 @@ pub fn (r Record) send() {
144144 r.channel < - r
145145}
146146
147+ pub struct Timestamp {
148+ pub mut :
149+ // format sets the format of datettime for logs.
150+ // TimestampFormat values map 1-to-1 to the date formats provided by `time.Time`.
151+ // If .custom format is selected the timestamp_custom field must be set.
152+ format TimestampFormat = .rfc3339
153+
154+ // custom sets the custom datetime string format if format is set to .custom.
155+ // See docs for Time.format_custom() fn from stadnard `time` module.
156+ custom string
157+
158+ // If local is true the local time will be used instead of UTC.
159+ local bool
160+ }
161+
162+ fn (t Timestamp) as_value () Value {
163+ return timestamp (t.format, t.custom, t.local)
164+ }
165+
147166pub enum TimestampFormat {
148167 default
149168 rfc3339
@@ -167,20 +186,23 @@ pub:
167186 // This value cannot be changed after logger initialization.
168187 level Level = .info
169188
189+ // timestamp holds the timestamp settings.
190+ timestamp Timestamp
191+
170192 add_level bool = true // if true add `level` field to all log records.
171193 add_timestamp bool = true // if true add `timestamp` field to all log records.
172194
173195 // timestamp_format sets the format of datettime for logs.
174196 // TimestampFormat values map 1-to-1 to the date formats provided by `time.Time`.
175197 // If .custom format is selected the timestamp_custom field must be set.
176- timestamp_format TimestampFormat = .rfc3339
198+ timestamp_format TimestampFormat = .rfc3339 @[deprecated: 'use `timestamp` instead' ]
177199
178200 // timestamp_custom sets the custom datetime string format if timestapm_format is
179201 // set to .custom. See docs for Time.format_custom() fn from stadnard `time` module.
180- timestamp_custom string
202+ timestamp_custom string @[deprecated: 'use `timestamp` instead' ]
181203
182204 // If timestamp_local is true the local time will be used instead of UTC.
183- timestamp_local bool
205+ timestamp_local bool @[deprecated: 'use `timestamp` instead' ]
184206
185207 // handler holds a log record handler object which is used to process logs.
186208 handler RecordHandler = TextHandler{
@@ -239,11 +261,20 @@ pub fn new(config LogConfig) StructuredLog {
239261
240262 mut extra_fields := []Field{}
241263
264+ mut timestamp := logger.timestamp
265+ mut timestamp_old := Timestamp{
266+ format: logger.timestamp_format
267+ custom: logger.timestamp_custom
268+ local: logger.timestamp_local
269+ }
270+ if timestamp != timestamp_old {
271+ timestamp = timestamp_old
272+ }
273+
242274 if logger.add_timestamp {
243275 extra_fields << Field{
244276 name: 'timestamp'
245- value: timestamp (logger.timestamp_format, logger.timestamp_custom,
246- logger.timestamp_local)
277+ value: timestamp.as_value ()
247278 }
248279 }
249280
0 commit comments