mirror of
https://github.com/Nanaloveyuki/BitLogger.git
synced 2026-05-30 15:42:25 +00:00
✨ Add file sink reopen and failure counters
This commit is contained in:
@@ -90,6 +90,9 @@ test "native file support flag is queryable" {
|
||||
test "file sink availability reflects backend support" {
|
||||
let sink = file_sink("bitlogger-test.log")
|
||||
inspect(sink.is_available() == native_files_supported(), content="true")
|
||||
inspect(sink.open_failures(), content=if sink.is_available() { "0" } else { "1" })
|
||||
inspect(sink.write_failures(), content="0")
|
||||
inspect(sink.flush_failures(), content="0")
|
||||
if sink.is_available() {
|
||||
inspect(sink.flush(), content="true")
|
||||
inspect(sink.close(), content="true")
|
||||
@@ -110,9 +113,34 @@ test "file sink tracks rotation failures on unavailable backend" {
|
||||
sink.write(record(Level::Info, "a"))
|
||||
if sink.is_available() {
|
||||
inspect(sink.rotation_failures(), content="0")
|
||||
inspect(sink.write_failures(), content="0")
|
||||
ignore(sink.close())
|
||||
} else {
|
||||
inspect(sink.rotation_failures(), content="0")
|
||||
inspect(sink.write_failures(), content="1")
|
||||
}
|
||||
}
|
||||
|
||||
test "file sink reopen and failure counters reflect backend state" {
|
||||
let sink = file_sink("bitlogger-reopen.log")
|
||||
if sink.is_available() {
|
||||
inspect(sink.open_failures(), content="0")
|
||||
inspect(sink.close(), content="true")
|
||||
inspect(sink.reopen(), content="true")
|
||||
inspect(sink.is_available(), content="true")
|
||||
inspect(sink.open_failures(), content="0")
|
||||
sink.write(record(Level::Info, "reopened"))
|
||||
inspect(sink.write_failures(), content="0")
|
||||
inspect(sink.flush_failures(), content="0")
|
||||
inspect(sink.close(), content="true")
|
||||
ignore(remove_file_internal("bitlogger-reopen.log"))
|
||||
} else {
|
||||
inspect(sink.open_failures(), content="1")
|
||||
sink.write(record(Level::Info, "dropped"))
|
||||
inspect(sink.write_failures(), content="1")
|
||||
inspect(sink.reopen(), content="false")
|
||||
inspect(sink.open_failures(), content="2")
|
||||
inspect(sink.flush_failures(), content="0")
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -46,8 +46,8 @@ BitLogger 是一个基于 MoonBit 的结构化日志库。
|
||||
- 支持 `parse_logger_config_text(...)`、`stringify_logger_config(...)` 进行最小 JSON 配置读写
|
||||
- config-driven logger assembly via `build_logger(...)`
|
||||
- 支持 `build_logger(...)` 将配置组装为可直接使用的 logger
|
||||
- native-only file output via `file_sink(...)`, with basic size rotation and backup retention
|
||||
- 支持 `file_sink(...)`、基础 size rotation 与 backup retention,但当前仅保证 `native/llvm` backend 可用
|
||||
- native-only file output via `file_sink(...)`, with basic size rotation, backup retention, explicit `reopen()`, and failure counters
|
||||
- 支持 `file_sink(...)`、基础 size rotation / backup retention、显式 `reopen()` 与失败计数,但当前仅保证 `native/llvm` backend 可用
|
||||
|
||||
## Example / 示例
|
||||
|
||||
@@ -220,3 +220,6 @@ test {
|
||||
- basic rotation is size-based / 当前基础轮转为按文件大小触发
|
||||
- `file_rotation(max_bytes, max_backups=...)` controls threshold and retained backups / `file_rotation(max_bytes, max_backups=...)` 控制触发阈值和保留备份数
|
||||
- JSON config uses `sink.rotation.max_bytes` and `sink.rotation.max_backups` / JSON 配置使用 `sink.rotation.max_bytes` 与 `sink.rotation.max_backups`
|
||||
- `FileSink::reopen()` can explicitly reopen the current file handle / `FileSink::reopen()` 可显式重开当前文件句柄
|
||||
- `open_failures()`、`write_failures()`、`flush_failures()`、`rotation_failures()` expose basic sink health counters / `open_failures()`、`write_failures()`、`flush_failures()`、`rotation_failures()` 可用于观察基础 sink 健康状态
|
||||
- current scope is observability-first, not a full self-healing sink runtime / 当前定位仍以可观测性优先,不是完整自愈型 sink runtime
|
||||
|
||||
+64
-7
@@ -51,6 +51,9 @@ pub struct FileSink {
|
||||
formatter : RecordFormatter
|
||||
auto_flush : Bool
|
||||
rotation : FileRotation?
|
||||
open_failures : Ref[Int]
|
||||
write_failures : Ref[Int]
|
||||
flush_failures : Ref[Int]
|
||||
rotation_failures : Ref[Int]
|
||||
}
|
||||
|
||||
@@ -79,13 +82,17 @@ pub fn file_sink(
|
||||
format_text(rec)
|
||||
},
|
||||
) -> FileSink {
|
||||
let handle = open_file_handle_internal(path, append)
|
||||
{
|
||||
path,
|
||||
append,
|
||||
handle: Ref::new(open_file_handle_internal(path, append)),
|
||||
handle: Ref::new(handle),
|
||||
formatter,
|
||||
auto_flush,
|
||||
rotation,
|
||||
open_failures: Ref::new(if handle is Some(_) { 0 } else { 1 }),
|
||||
write_failures: Ref::new(0),
|
||||
flush_failures: Ref::new(0),
|
||||
rotation_failures: Ref::new(0),
|
||||
}
|
||||
}
|
||||
@@ -97,7 +104,13 @@ pub fn FileSink::is_available(self : FileSink) -> Bool {
|
||||
pub fn FileSink::flush(self : FileSink) -> Bool {
|
||||
match self.handle.val {
|
||||
None => false
|
||||
Some(handle) => flush_file_handle_internal(handle)
|
||||
Some(handle) => {
|
||||
let ok = flush_file_handle_internal(handle)
|
||||
if !ok {
|
||||
self.flush_failures.val += 1
|
||||
}
|
||||
ok
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -116,6 +129,37 @@ pub fn FileSink::rotation_failures(self : FileSink) -> Int {
|
||||
self.rotation_failures.val
|
||||
}
|
||||
|
||||
pub fn FileSink::open_failures(self : FileSink) -> Int {
|
||||
self.open_failures.val
|
||||
}
|
||||
|
||||
pub fn FileSink::write_failures(self : FileSink) -> Int {
|
||||
self.write_failures.val
|
||||
}
|
||||
|
||||
pub fn FileSink::flush_failures(self : FileSink) -> Int {
|
||||
self.flush_failures.val
|
||||
}
|
||||
|
||||
pub fn FileSink::reopen(self : FileSink, append~ : Bool? = None) -> Bool {
|
||||
let append_mode = append.unwrap_or(self.append)
|
||||
match self.handle.val {
|
||||
None => ()
|
||||
Some(handle) => {
|
||||
ignore(close_file_handle_internal(handle))
|
||||
self.handle.val = None
|
||||
}
|
||||
}
|
||||
let reopened = open_file_handle_internal(self.path, append_mode)
|
||||
self.handle.val = reopened
|
||||
if reopened is Some(_) {
|
||||
true
|
||||
} else {
|
||||
self.open_failures.val += 1
|
||||
false
|
||||
}
|
||||
}
|
||||
|
||||
fn rotated_file_path(path : String, index : Int) -> String {
|
||||
"\{path}.\{index}"
|
||||
}
|
||||
@@ -171,20 +215,33 @@ fn rotate_if_needed_internal(sink : FileSink, next_line_bytes : Int) -> Bool {
|
||||
|
||||
pub impl Sink for FileSink with write(self, rec) {
|
||||
match self.handle.val {
|
||||
None => ()
|
||||
None => {
|
||||
self.write_failures.val += 1
|
||||
}
|
||||
Some(_) => {
|
||||
let line = "\{(self.formatter)(rec)}\n"
|
||||
let can_write = rotate_if_needed_internal(self, string_byte_length_internal(line))
|
||||
if can_write {
|
||||
match self.handle.val {
|
||||
None => ()
|
||||
None => {
|
||||
self.write_failures.val += 1
|
||||
}
|
||||
Some(active) => {
|
||||
ignore(write_file_handle_internal(active, line))
|
||||
if self.auto_flush {
|
||||
ignore(flush_file_handle_internal(active))
|
||||
let wrote = write_file_handle_internal(active, line)
|
||||
if wrote {
|
||||
if self.auto_flush {
|
||||
let flushed = flush_file_handle_internal(active)
|
||||
if !flushed {
|
||||
self.flush_failures.val += 1
|
||||
}
|
||||
}
|
||||
} else {
|
||||
self.write_failures.val += 1
|
||||
}
|
||||
}
|
||||
}
|
||||
} else {
|
||||
self.write_failures.val += 1
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user