This commit is contained in:
ge
2026-01-03 16:38:28 +03:00
commit 1fa21a9c74
12 changed files with 623 additions and 0 deletions

27
examples/basic.v Normal file
View File

@@ -0,0 +1,27 @@
import rand
import structlog
fn main() {
// Initialize logger with default configuratuion.
log := structlog.new()
defer {
// Since processing and writing the log is done in a separate thread,
// we need to wait for it to complete before exiting the program.
log.close()
}
// Write some logs.
//
// Note the call chain. First, the info() call creates a empty `structlog.Record`
// object with `info` log level. The next message() call adds a message field with
// the specified text to the record. The final send() call sends the record to the
// record handler (TextHandler by default) which writes log to stardard output.
log.info().message('Hello, World!').send()
// You can set your own named fields.
log.info().field('random_string', rand.string(5)).send()
log.info().field('answer', 42).field('computed_by', 'Deep Thought').send()
// Errors can be passed to logger as is.
log.error().message('this line contains error').error(error('oops')).send()
}

21
examples/json_log.v Normal file
View File

@@ -0,0 +1,21 @@
import os
import rand
import structlog
fn main() {
// Initialize logger with JSONHandler.
log := structlog.new(
level: .trace
handler: structlog.JSONHandler{
writer: os.stdout()
}
)
defer {
log.close()
}
log.info().message('Hello, World!').send()
log.info().field('random_string', rand.string(5)).send()
log.info().field('answer', 42).field('computed_by', 'Deep Thought').send()
log.error().message('this line contains error').error(error('oops')).send()
}

16
examples/logging_levels.v Normal file
View File

@@ -0,0 +1,16 @@
import structlog
fn main() {
// Initialize logger with non-default logging level.
log := structlog.new(level: .trace) // try to change logging level
defer {
log.close()
}
log.trace().message('hello trace').send()
log.debug().message('hello debug').send()
log.info().message('hello info').send()
log.warn().message('hello warn').send()
log.error().message('hello error').send()
log.fatal().message('hello fatal').send() // on fatal program exits immediately with exit code 1
}

17
examples/unix_timestamp.v Normal file
View File

@@ -0,0 +1,17 @@
import os
import structlog
fn main() {
// Initialize logger with edited timestamp.
log := structlog.new(
timestamp_format: .unix
handler: structlog.JSONHandler{
writer: os.stdout()
}
)
defer {
log.close()
}
log.info().message('Hello, World!').send()
}