Skip to content

Commit 8b13a59

Browse files
committed
LogConfig: deprecate timestamp_*, add Timestamp
1 parent 98738e7 commit 8b13a59

3 files changed

Lines changed: 42 additions & 8 deletions

File tree

examples/unix_timestamp.v

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,11 @@ import structlog
44
fn main() {
55
// Initialize logger with edited timestamp.
66
log := structlog.new(
7-
timestamp_format: .unix
8-
handler: structlog.JSONHandler{
7+
// timestamp_format: .unix
8+
timestamp: structlog.Timestamp{
9+
format: .unix
10+
}
11+
handler: structlog.JSONHandler{
912
writer: os.stdout()
1013
}
1114
)

structlog.v

Lines changed: 36 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -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+
147166
pub 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

v.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
Module {
22
name: 'structlog'
33
description: 'Structured logs'
4-
version: '0.1.0'
4+
version: '0.2.0'
55
license: 'MIT'
66
dependencies: []
77
}

0 commit comments

Comments
 (0)