From 8b13a596f746a14994def8f9dd4b805d74eaff3b Mon Sep 17 00:00:00 2001 From: ge Date: Sun, 11 Jan 2026 06:04:13 +0300 Subject: [PATCH] LogConfig: deprecate timestamp_*, add Timestamp --- examples/unix_timestamp.v | 7 +++++-- structlog.v | 41 ++++++++++++++++++++++++++++++++++----- v.mod | 2 +- 3 files changed, 42 insertions(+), 8 deletions(-) diff --git a/examples/unix_timestamp.v b/examples/unix_timestamp.v index 1060b08..5a4fef2 100644 --- a/examples/unix_timestamp.v +++ b/examples/unix_timestamp.v @@ -4,8 +4,11 @@ import structlog fn main() { // Initialize logger with edited timestamp. log := structlog.new( - timestamp_format: .unix - handler: structlog.JSONHandler{ + // timestamp_format: .unix + timestamp: structlog.Timestamp{ + format: .unix + } + handler: structlog.JSONHandler{ writer: os.stdout() } ) diff --git a/structlog.v b/structlog.v index 6efd2c0..12c28a9 100644 --- a/structlog.v +++ b/structlog.v @@ -144,6 +144,25 @@ pub fn (r Record) send() { r.channel <- r } +pub struct Timestamp { +pub mut: + // format sets the format of datettime for logs. + // TimestampFormat values map 1-to-1 to the date formats provided by `time.Time`. + // If .custom format is selected the timestamp_custom field must be set. + format TimestampFormat = .rfc3339 + + // custom sets the custom datetime string format if format is set to .custom. + // See docs for Time.format_custom() fn from stadnard `time` module. + custom string + + // If local is true the local time will be used instead of UTC. + local bool +} + +fn (t Timestamp) as_value() Value { + return timestamp(t.format, t.custom, t.local) +} + pub enum TimestampFormat { default rfc3339 @@ -167,20 +186,23 @@ pub: // This value cannot be changed after logger initialization. level Level = .info + // timestamp holds the timestamp settings. + timestamp Timestamp + add_level bool = true // if true add `level` field to all log records. add_timestamp bool = true // if true add `timestamp` field to all log records. // timestamp_format sets the format of datettime for logs. // TimestampFormat values ​​map 1-to-1 to the date formats provided by `time.Time`. // If .custom format is selected the timestamp_custom field must be set. - timestamp_format TimestampFormat = .rfc3339 + timestamp_format TimestampFormat = .rfc3339 @[deprecated: 'use `timestamp` instead'] // timestamp_custom sets the custom datetime string format if timestapm_format is // set to .custom. See docs for Time.format_custom() fn from stadnard `time` module. - timestamp_custom string + timestamp_custom string @[deprecated: 'use `timestamp` instead'] // If timestamp_local is true the local time will be used instead of UTC. - timestamp_local bool + timestamp_local bool @[deprecated: 'use `timestamp` instead'] // handler holds a log record handler object which is used to process logs. handler RecordHandler = TextHandler{ @@ -239,11 +261,20 @@ pub fn new(config LogConfig) StructuredLog { mut extra_fields := []Field{} + mut timestamp := logger.timestamp + mut timestamp_old := Timestamp{ + format: logger.timestamp_format + custom: logger.timestamp_custom + local: logger.timestamp_local + } + if timestamp != timestamp_old { + timestamp = timestamp_old + } + if logger.add_timestamp { extra_fields << Field{ name: 'timestamp' - value: timestamp(logger.timestamp_format, logger.timestamp_custom, - logger.timestamp_local) + value: timestamp.as_value() } } diff --git a/v.mod b/v.mod index d8959c9..3e9d319 100644 --- a/v.mod +++ b/v.mod @@ -1,7 +1,7 @@ Module { name: 'structlog' description: 'Structured logs' - version: '0.1.0' + version: '0.2.0' license: 'MIT' dependencies: [] }