mirror of
https://github.com/vlang/v.git
synced 2025-08-05 10:47:11 -04:00
log: add local time / utc time selection support (#24268)
This commit is contained in:
parent
324edc6eab
commit
13645e2456
@ -38,6 +38,7 @@ mut:
|
|||||||
time_format TimeFormat = .tf_rfc3339_micro
|
time_format TimeFormat = .tf_rfc3339_micro
|
||||||
custom_time_format string = 'MMMM Do YY N kk:mm:ss A' // timestamp with custom format
|
custom_time_format string = 'MMMM Do YY N kk:mm:ss A' // timestamp with custom format
|
||||||
short_tag bool
|
short_tag bool
|
||||||
|
local_time bool // use local time or utc time in log
|
||||||
always_flush bool // flush after every single .fatal(), .error(), .warn(), .info(), .debug() call
|
always_flush bool // flush after every single .fatal(), .error(), .warn(), .info(), .debug() call
|
||||||
output_stream io.Writer = stderr
|
output_stream io.Writer = stderr
|
||||||
pub mut:
|
pub mut:
|
||||||
@ -119,7 +120,7 @@ pub fn (mut l Log) reopen() ! {
|
|||||||
|
|
||||||
// log_file writes log line `s` with `level` to the log file.
|
// log_file writes log line `s` with `level` to the log file.
|
||||||
fn (mut l Log) log_file(s string, level Level) {
|
fn (mut l Log) log_file(s string, level Level) {
|
||||||
timestamp := l.time_format(time.utc())
|
timestamp := l.time_format(if l.local_time { time.now() } else { time.utc() })
|
||||||
e := tag_to_file(level, l.short_tag)
|
e := tag_to_file(level, l.short_tag)
|
||||||
|
|
||||||
unsafe {
|
unsafe {
|
||||||
@ -142,7 +143,7 @@ fn (mut l Log) log_file(s string, level Level) {
|
|||||||
|
|
||||||
// log_stream writes log line `s` with `level` to stderr or stderr depending on set output stream.
|
// log_stream writes log line `s` with `level` to stderr or stderr depending on set output stream.
|
||||||
fn (mut l Log) log_stream(s string, level Level) {
|
fn (mut l Log) log_stream(s string, level Level) {
|
||||||
timestamp := l.time_format(time.utc())
|
timestamp := l.time_format(if l.local_time { time.now() } else { time.utc() })
|
||||||
tag := tag_to_console(level, l.short_tag)
|
tag := tag_to_console(level, l.short_tag)
|
||||||
msg := '${timestamp} [${tag}] ${s}\n'
|
msg := '${timestamp} [${tag}] ${s}\n'
|
||||||
arr := msg.bytes()
|
arr := msg.bytes()
|
||||||
@ -313,6 +314,16 @@ pub fn (l Log) get_short_tag() bool {
|
|||||||
return l.short_tag
|
return l.short_tag
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// set_local_time will set the log using local time
|
||||||
|
pub fn (mut l Log) set_local_time(enabled bool) {
|
||||||
|
l.local_time = enabled
|
||||||
|
}
|
||||||
|
|
||||||
|
// get_local_time will get the log using local time state
|
||||||
|
pub fn (l Log) get_local_time() bool {
|
||||||
|
return l.local_time
|
||||||
|
}
|
||||||
|
|
||||||
// use_stdout will restore the old behaviour of logging to stdout, instead of stderr.
|
// use_stdout will restore the old behaviour of logging to stdout, instead of stderr.
|
||||||
// It will also silence the deprecation note in the transition period.
|
// It will also silence the deprecation note in the transition period.
|
||||||
pub fn use_stdout() {
|
pub fn use_stdout() {
|
||||||
|
@ -134,6 +134,13 @@ fn test_log_time_format() {
|
|||||||
assert TimeFormat.tf_custom_format == l.get_time_format()
|
assert TimeFormat.tf_custom_format == l.get_time_format()
|
||||||
l.info('${@FN} custom like January 1st 22 AD 13:45:33 PM')
|
l.info('${@FN} custom like January 1st 22 AD 13:45:33 PM')
|
||||||
assert true
|
assert true
|
||||||
|
l.set_time_format(.tf_default)
|
||||||
|
l.set_local_time(true)
|
||||||
|
l.info('${@FN} time log in local time')
|
||||||
|
assert l.get_local_time()
|
||||||
|
l.set_local_time(false)
|
||||||
|
l.info('${@FN} time log in utc time')
|
||||||
|
assert !l.get_local_time()
|
||||||
println(@FN + ' end')
|
println(@FN + ' end')
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user