mirror of
https://github.com/gechandesu/structlog.git
synced 2026-03-05 18:41:49 +03:00
Compare commits
4 Commits
98738e773d
...
v0.3.0
| Author | SHA1 | Date | |
|---|---|---|---|
| c6d53758b3 | |||
| a9a0e22d3a | |||
| f688b3cad5 | |||
| 8b13a596f7 |
@@ -4,7 +4,9 @@ import structlog
|
|||||||
fn main() {
|
fn main() {
|
||||||
// Initialize logger with edited timestamp.
|
// Initialize logger with edited timestamp.
|
||||||
log := structlog.new(
|
log := structlog.new(
|
||||||
timestamp_format: .unix
|
timestamp: structlog.Timestamp{
|
||||||
|
format: .unix
|
||||||
|
}
|
||||||
handler: structlog.JSONHandler{
|
handler: structlog.JSONHandler{
|
||||||
writer: os.stdout()
|
writer: os.stdout()
|
||||||
}
|
}
|
||||||
|
|||||||
43
structlog.v
43
structlog.v
@@ -96,8 +96,7 @@ pub fn (r Record) append(field ...Field) Record {
|
|||||||
mut fields_orig := unsafe { r.fields }
|
mut fields_orig := unsafe { r.fields }
|
||||||
fields_orig << field
|
fields_orig << field
|
||||||
return Record{
|
return Record{
|
||||||
channel: r.channel
|
...r
|
||||||
level: r.level
|
|
||||||
fields: &fields_orig
|
fields: &fields_orig
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -110,8 +109,7 @@ pub fn (r Record) prepend(field ...Field) Record {
|
|||||||
mut new_fields := unsafe { field }
|
mut new_fields := unsafe { field }
|
||||||
new_fields << r.fields
|
new_fields << r.fields
|
||||||
return Record{
|
return Record{
|
||||||
channel: r.channel
|
...r
|
||||||
level: r.level
|
|
||||||
fields: new_fields
|
fields: new_fields
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -144,6 +142,25 @@ pub fn (r Record) send() {
|
|||||||
r.channel <- r
|
r.channel <- r
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub struct Timestamp {
|
||||||
|
pub mut:
|
||||||
|
// format sets the format of datetime in logs. TimestampFormat values
|
||||||
|
// map 1-to-1 to the date formats provided by `time.Time`.
|
||||||
|
// If .custom format is selected the `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 {
|
pub enum TimestampFormat {
|
||||||
default
|
default
|
||||||
rfc3339
|
rfc3339
|
||||||
@@ -167,21 +184,12 @@ pub:
|
|||||||
// This value cannot be changed after logger initialization.
|
// This value cannot be changed after logger initialization.
|
||||||
level Level = .info
|
level Level = .info
|
||||||
|
|
||||||
|
// timestamp holds the timestamp settings.
|
||||||
|
timestamp Timestamp
|
||||||
|
|
||||||
add_level bool = true // if true add `level` field to all log records.
|
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.
|
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_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
|
|
||||||
|
|
||||||
// If timestamp_local is true the local time will be used instead of UTC.
|
|
||||||
timestamp_local bool
|
|
||||||
|
|
||||||
// handler holds a log record handler object which is used to process logs.
|
// handler holds a log record handler object which is used to process logs.
|
||||||
handler RecordHandler = TextHandler{
|
handler RecordHandler = TextHandler{
|
||||||
writer: os.stdout()
|
writer: os.stdout()
|
||||||
@@ -242,8 +250,7 @@ pub fn new(config LogConfig) StructuredLog {
|
|||||||
if logger.add_timestamp {
|
if logger.add_timestamp {
|
||||||
extra_fields << Field{
|
extra_fields << Field{
|
||||||
name: 'timestamp'
|
name: 'timestamp'
|
||||||
value: timestamp(logger.timestamp_format, logger.timestamp_custom,
|
value: logger.timestamp.as_value()
|
||||||
logger.timestamp_local)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user