mirror of
https://github.com/Nanaloveyuki/BitLogger.git
synced 2026-05-30 15:42:25 +00:00
✨ Add explicit append and truncate reopen helpers
This commit is contained in:
@@ -275,30 +275,35 @@ test "configured logger can reopen built file sink" {
|
||||
)
|
||||
if logger.file_available() {
|
||||
inspect(logger.close(), content="true")
|
||||
inspect(logger.file_reopen(), content="true")
|
||||
inspect(logger.file_reopen_append(), content="true")
|
||||
inspect(logger.file_available(), content="true")
|
||||
inspect(logger.file_append_mode(), content="true")
|
||||
inspect(logger.file_open_failures(), content="0")
|
||||
logger.info("reopened from config")
|
||||
inspect(logger.file_write_failures(), content="0")
|
||||
inspect(logger.file_reopen(append=Some(false)), content="true")
|
||||
inspect(logger.file_reopen_truncate(), content="true")
|
||||
inspect(logger.file_append_mode(), content="false")
|
||||
inspect(logger.file_reopen(), content="true")
|
||||
inspect(logger.file_append_mode(), content="false")
|
||||
inspect(logger.file_reopen_with_current_policy(), content="true")
|
||||
inspect(logger.file_append_mode(), content="false")
|
||||
inspect(logger.file_reopen_append(), content="true")
|
||||
inspect(logger.file_append_mode(), content="true")
|
||||
inspect(logger.close(), content="true")
|
||||
} else {
|
||||
inspect(logger.file_append_mode(), content="true")
|
||||
inspect(logger.file_open_failures(), content="1")
|
||||
logger.info("dropped")
|
||||
inspect(logger.file_write_failures(), content="1")
|
||||
inspect(logger.file_reopen(), content="false")
|
||||
inspect(logger.file_reopen_append(), content="false")
|
||||
inspect(logger.file_open_failures(), content="2")
|
||||
inspect(logger.file_reopen(append=Some(false)), content="false")
|
||||
inspect(logger.file_reopen_truncate(), content="false")
|
||||
inspect(logger.file_append_mode(), content="false")
|
||||
inspect(logger.file_reopen_with_current_policy(), content="false")
|
||||
inspect(logger.file_open_failures(), content="3")
|
||||
inspect(logger.file_reopen_append(), content="false")
|
||||
inspect(logger.file_append_mode(), content="true")
|
||||
inspect(logger.file_open_failures(), content="4")
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -166,19 +166,21 @@ test "file sink reopen and failure counters reflect backend state" {
|
||||
inspect(sink.append_mode(), content="true")
|
||||
inspect(sink.open_failures(), content="0")
|
||||
inspect(sink.close(), content="true")
|
||||
inspect(sink.reopen(), content="true")
|
||||
inspect(sink.reopen_append(), content="true")
|
||||
inspect(sink.is_available(), content="true")
|
||||
inspect(sink.append_mode(), 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.reopen(append=Some(false)), content="true")
|
||||
inspect(sink.reopen_truncate(), content="true")
|
||||
inspect(sink.append_mode(), content="false")
|
||||
inspect(sink.reopen(), content="true")
|
||||
inspect(sink.append_mode(), content="false")
|
||||
inspect(sink.reopen_with_current_policy(), content="true")
|
||||
inspect(sink.append_mode(), content="false")
|
||||
inspect(sink.reopen_append(), content="true")
|
||||
inspect(sink.append_mode(), content="true")
|
||||
inspect(sink.close(), content="true")
|
||||
ignore(remove_file_internal("bitlogger-reopen.log"))
|
||||
} else {
|
||||
@@ -186,13 +188,16 @@ test "file sink reopen and failure counters reflect backend state" {
|
||||
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.reopen_append(), content="false")
|
||||
inspect(sink.open_failures(), content="2")
|
||||
inspect(sink.flush_failures(), content="0")
|
||||
inspect(sink.reopen(append=Some(false)), content="false")
|
||||
inspect(sink.reopen_truncate(), content="false")
|
||||
inspect(sink.append_mode(), content="false")
|
||||
inspect(sink.reopen_with_current_policy(), content="false")
|
||||
inspect(sink.open_failures(), content="3")
|
||||
inspect(sink.reopen_append(), content="false")
|
||||
inspect(sink.append_mode(), content="true")
|
||||
inspect(sink.open_failures(), content="4")
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -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, backup retention, explicit `reopen()` / `reopen_with_current_policy()`, and failure counters
|
||||
- 支持 `file_sink(...)`、基础 size rotation / backup retention、显式 `reopen()` / `reopen_with_current_policy()` 与失败计数,但当前仅保证 `native/llvm` backend 可用
|
||||
- native-only file output via `file_sink(...)`, with basic size rotation, backup retention, explicit `reopen()` / `reopen_with_current_policy()` / `reopen_append()` / `reopen_truncate()`, and failure counters
|
||||
- 支持 `file_sink(...)`、基础 size rotation / backup retention、显式 `reopen()` / `reopen_with_current_policy()` / `reopen_append()` / `reopen_truncate()` 与失败计数,但当前仅保证 `native/llvm` backend 可用
|
||||
|
||||
## Example / 示例
|
||||
|
||||
@@ -220,7 +220,7 @@ 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, and `FileSink::reopen_with_current_policy()` makes the stored-policy reopen path explicit / `FileSink::reopen()` 可显式重开当前文件句柄,`FileSink::reopen_with_current_policy()` 则把“按当前保存策略重开”变成显式动作
|
||||
- `FileSink::reopen()` can explicitly reopen the current file handle, `FileSink::reopen_with_current_policy()` makes the stored-policy reopen path explicit, and `FileSink::reopen_append()` / `FileSink::reopen_truncate()` cover the two common reopen modes directly / `FileSink::reopen()` 可显式重开当前文件句柄,`FileSink::reopen_with_current_policy()` 把“按当前保存策略重开”变成显式动作,`FileSink::reopen_append()` / `FileSink::reopen_truncate()` 则直接覆盖常见 append 与 truncate 模式
|
||||
- `append_mode()` exposes the current append policy, and `reopen(append=...)` updates that policy for later reopen calls / `append_mode()` 可读取当前 append 策略,`reopen(append=...)` 会更新后续 reopen 复用的 append 策略
|
||||
- `set_append_mode(...)` updates the stored append policy without forcing an immediate reopen / `set_append_mode(...)` 可直接更新保存的 append 策略,但不会强制立即 reopen
|
||||
- `path()` and `auto_flush_enabled()` expose core file sink policy state / `path()` 与 `auto_flush_enabled()` 可读取 file sink 的基础策略状态
|
||||
|
||||
@@ -239,6 +239,22 @@ pub fn RuntimeSink::file_reopen_with_current_policy(self : RuntimeSink) -> Bool
|
||||
}
|
||||
}
|
||||
|
||||
pub fn RuntimeSink::file_reopen_append(self : RuntimeSink) -> Bool {
|
||||
match self {
|
||||
File(sink) => sink.reopen_append()
|
||||
QueuedFile(sink) => sink.sink.reopen_append()
|
||||
_ => false
|
||||
}
|
||||
}
|
||||
|
||||
pub fn RuntimeSink::file_reopen_truncate(self : RuntimeSink) -> Bool {
|
||||
match self {
|
||||
File(sink) => sink.reopen_truncate()
|
||||
QueuedFile(sink) => sink.sink.reopen_truncate()
|
||||
_ => false
|
||||
}
|
||||
}
|
||||
|
||||
pub fn RuntimeSink::file_append_mode(self : RuntimeSink) -> Bool {
|
||||
match self {
|
||||
File(sink) => sink.append_mode()
|
||||
@@ -423,6 +439,14 @@ pub fn ConfiguredLogger::file_reopen_with_current_policy(self : ConfiguredLogger
|
||||
self.sink.file_reopen_with_current_policy()
|
||||
}
|
||||
|
||||
pub fn ConfiguredLogger::file_reopen_append(self : ConfiguredLogger) -> Bool {
|
||||
self.sink.file_reopen_append()
|
||||
}
|
||||
|
||||
pub fn ConfiguredLogger::file_reopen_truncate(self : ConfiguredLogger) -> Bool {
|
||||
self.sink.file_reopen_truncate()
|
||||
}
|
||||
|
||||
pub fn ConfiguredLogger::file_append_mode(self : ConfiguredLogger) -> Bool {
|
||||
self.sink.file_append_mode()
|
||||
}
|
||||
|
||||
@@ -201,6 +201,14 @@ pub fn FileSink::reopen_with_current_policy(self : FileSink) -> Bool {
|
||||
self.reopen()
|
||||
}
|
||||
|
||||
pub fn FileSink::reopen_append(self : FileSink) -> Bool {
|
||||
self.reopen(append=Some(true))
|
||||
}
|
||||
|
||||
pub fn FileSink::reopen_truncate(self : FileSink) -> Bool {
|
||||
self.reopen(append=Some(false))
|
||||
}
|
||||
|
||||
fn rotated_file_path(path : String, index : Int) -> String {
|
||||
"\{path}.\{index}"
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user