mirror of
https://github.com/gechandesu/structlog.git
synced 2026-04-15 05:43:19 +03:00
all: various improvements
* Add os.File.flush() call to fix writing log into files. * Add comma as TextHandler fields separator. * Add struct_adapter() * Add field() generic function to creating Field instances. * Add new write_to_file.v example. * Rename Record.field() to Record.add()
This commit is contained in:
@@ -19,8 +19,8 @@ fn main() {
|
||||
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()
|
||||
log.info().add('random_string', rand.string(100)).send()
|
||||
log.info().add('answer', 42).add('computed_by', 'Deep Thought').send()
|
||||
|
||||
// Errors can be passed to logger as is.
|
||||
log.error().message('this line contains error').error(error('oops')).send()
|
||||
|
||||
@@ -15,7 +15,7 @@ fn main() {
|
||||
}
|
||||
|
||||
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.info().add('random_string', rand.string(100)).send()
|
||||
log.info().add('answer', 42).add('computed_by', 'Deep Thought').send()
|
||||
log.error().message('this line contains error').error(error('oops')).send()
|
||||
}
|
||||
|
||||
26
examples/write_to_file.v
Normal file
26
examples/write_to_file.v
Normal file
@@ -0,0 +1,26 @@
|
||||
import os
|
||||
import structlog
|
||||
|
||||
fn main() {
|
||||
// Open a file in append mode. If file does not exists it will be created.
|
||||
log_path := os.join_path_single(os.temp_dir(), 'example_log')
|
||||
log_file := os.open_file(log_path, 'a+') or {
|
||||
eprintln('Error: cound not open log file ${log_path}: ${err}')
|
||||
exit(1)
|
||||
}
|
||||
|
||||
eprintln('Log file location: ${log_path}')
|
||||
|
||||
// Initialize logger with os.File as writer.
|
||||
log := structlog.new(
|
||||
handler: structlog.TextHandler{
|
||||
color: false
|
||||
writer: log_file
|
||||
}
|
||||
)
|
||||
defer {
|
||||
log.close()
|
||||
}
|
||||
|
||||
log.info().message('Hello, World!').send()
|
||||
}
|
||||
Reference in New Issue
Block a user