Skip to content

Commit

Permalink
add flush and reopen methods to the logger
Browse files Browse the repository at this point in the history
  • Loading branch information
kimshrier committed Oct 28, 2023
1 parent 705eea8 commit 51695c5
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 0 deletions.
10 changes: 10 additions & 0 deletions vlib/log/log.v
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,16 @@ pub fn (mut l Log) close() {
l.ofile.close()
}

// reopen reopens the log file. Useful for log rotation.
// This does nothing if you are only writing to the console.
pub fn (mut l Log) reopen() {
if l.output_target == .file || l.output_target == .both {
l.ofile.reopen(l.output_file_name, 'ab') or {
panic('error while re-opening log file ${l.output_file_name} for appending')
}
}
}

// log_file writes log line `s` with `level` to the log file.
fn (mut l Log) log_file(s string, level Level) {
timestamp := time.now().format_ss_micro()
Expand Down
2 changes: 2 additions & 0 deletions vlib/log/logger_interface.v
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,7 @@ mut:
debug(s string)
// utility methods:
set_level(level Level)
flush()
reopen()
free()
}

0 comments on commit 51695c5

Please sign in to comment.