mirror of
https://github.com/Nanaloveyuki/BitLogger.git
synced 2026-05-30 15:42:25 +00:00
✨ Make file append mode observable and persistent
This commit is contained in:
@@ -222,7 +222,8 @@ if native_files_supported() {
|
||||
- 已支持字段:`min_level`、`target`、`timestamp`、`sink.kind`、`sink.path`、`sink.append`、`sink.auto_flush`、`sink.rotation`、`sink.text_formatter`、`queue`
|
||||
- `sink.rotation` 当前支持 `max_bytes` 与 `max_backups`,提供基础 size-based rotation 和 backup retention
|
||||
- `file_sink(...)` 还提供 `reopen()`、`open_failures()`、`write_failures()`、`flush_failures()`、`rotation_failures()`,用于基础可观测性
|
||||
- `build_logger(...)` 产出的 `ConfiguredLogger` 同样提供 `file_reopen()`、`file_flush()`、`file_close()` 与对应 file failure 计数访问器,便于配置式接入后继续运维控制
|
||||
- `file_sink(...)` 当前还提供 `append_mode()`;`reopen(append=...)` 在显式传值时会更新后续 reopen 使用的 append 策略
|
||||
- `build_logger(...)` 产出的 `ConfiguredLogger` 同样提供 `file_reopen()`、`file_flush()`、`file_close()`、`file_append_mode()` 与对应 file failure 计数访问器,便于配置式接入后继续运维控制
|
||||
- `sink.text_formatter.template` 当前支持固定 token:`{timestamp}`、`{timestamp_ms}`、`{level}`、`{target}`、`{message}`、`{fields}`
|
||||
- 当前可由配置直接组装的 sink 类型:`console`、`json_console`、`text_console`、`file`
|
||||
- `queue` 会作为显式包装层附着在最终 sink 外侧;这仍然是同步 drain 模型,不是 async runtime
|
||||
|
||||
@@ -202,6 +202,7 @@ test "configured logger exposes file sink observability helpers" {
|
||||
),
|
||||
)
|
||||
inspect(logger.file_available() == native_files_supported(), content="true")
|
||||
inspect(logger.file_append_mode(), content="true")
|
||||
inspect(logger.file_open_failures(), content=if logger.file_available() { "0" } else { "1" })
|
||||
inspect(logger.file_write_failures(), content="0")
|
||||
inspect(logger.file_flush_failures(), content="0")
|
||||
@@ -219,16 +220,24 @@ test "configured logger can reopen built file sink" {
|
||||
inspect(logger.close(), content="true")
|
||||
inspect(logger.file_reopen(), 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_append_mode(), content="false")
|
||||
inspect(logger.file_reopen(), content="true")
|
||||
inspect(logger.file_append_mode(), content="false")
|
||||
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_open_failures(), content="2")
|
||||
inspect(logger.file_reopen(append=Some(false)), content="false")
|
||||
inspect(logger.file_append_mode(), content="false")
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -90,6 +90,7 @@ 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.append_mode(), 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")
|
||||
@@ -124,23 +125,32 @@ test "file sink tracks rotation failures on unavailable backend" {
|
||||
test "file sink reopen and failure counters reflect backend state" {
|
||||
let sink = file_sink("bitlogger-reopen.log")
|
||||
if sink.is_available() {
|
||||
inspect(sink.append_mode(), content="true")
|
||||
inspect(sink.open_failures(), content="0")
|
||||
inspect(sink.close(), content="true")
|
||||
inspect(sink.reopen(), 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.append_mode(), content="false")
|
||||
inspect(sink.reopen(), content="true")
|
||||
inspect(sink.append_mode(), content="false")
|
||||
inspect(sink.close(), content="true")
|
||||
ignore(remove_file_internal("bitlogger-reopen.log"))
|
||||
} else {
|
||||
inspect(sink.append_mode(), content="true")
|
||||
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")
|
||||
inspect(sink.reopen(append=Some(false)), content="false")
|
||||
inspect(sink.append_mode(), content="false")
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -221,6 +221,7 @@ test {
|
||||
- `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()` 可显式重开当前文件句柄
|
||||
- `append_mode()` exposes the current append policy, and `reopen(append=...)` updates that policy for later reopen calls / `append_mode()` 可读取当前 append 策略,`reopen(append=...)` 会更新后续 reopen 复用的 append 策略
|
||||
- `open_failures()`、`write_failures()`、`flush_failures()`、`rotation_failures()` expose basic sink health counters / `open_failures()`、`write_failures()`、`flush_failures()`、`rotation_failures()` 可用于观察基础 sink 健康状态
|
||||
- `ConfiguredLogger` also forwards file reopen, flush, close, and failure-counter helpers for config-built file sinks / `ConfiguredLogger` 也会为配置生成的 file sink 转发 reopen、flush、close 与失败计数访问器
|
||||
- `ConfiguredLogger` also forwards file reopen, flush, close, append-mode, and failure-counter helpers for config-built file sinks / `ConfiguredLogger` 也会为配置生成的 file sink 转发 reopen、flush、close、append-mode 与失败计数访问器
|
||||
- current scope is observability-first, not a full self-healing sink runtime / 当前定位仍以可观测性优先,不是完整自愈型 sink runtime
|
||||
|
||||
@@ -231,6 +231,14 @@ pub fn RuntimeSink::file_reopen(self : RuntimeSink, append~ : Bool? = None) -> B
|
||||
}
|
||||
}
|
||||
|
||||
pub fn RuntimeSink::file_append_mode(self : RuntimeSink) -> Bool {
|
||||
match self {
|
||||
File(sink) => sink.append_mode()
|
||||
QueuedFile(sink) => sink.sink.append_mode()
|
||||
_ => false
|
||||
}
|
||||
}
|
||||
|
||||
pub fn RuntimeSink::file_flush(self : RuntimeSink) -> Bool {
|
||||
match self {
|
||||
File(sink) => sink.flush()
|
||||
@@ -315,6 +323,10 @@ pub fn ConfiguredLogger::file_reopen(self : ConfiguredLogger, append~ : Bool? =
|
||||
self.sink.file_reopen(append=append)
|
||||
}
|
||||
|
||||
pub fn ConfiguredLogger::file_append_mode(self : ConfiguredLogger) -> Bool {
|
||||
self.sink.file_append_mode()
|
||||
}
|
||||
|
||||
pub fn ConfiguredLogger::file_flush(self : ConfiguredLogger) -> Bool {
|
||||
self.sink.file_flush()
|
||||
}
|
||||
|
||||
+8
-3
@@ -46,7 +46,7 @@ pub impl Sink for JsonConsoleSink with write(self, rec) {
|
||||
|
||||
pub struct FileSink {
|
||||
path : String
|
||||
append : Bool
|
||||
append : Ref[Bool]
|
||||
handle : Ref[FileHandle?]
|
||||
formatter : RecordFormatter
|
||||
auto_flush : Bool
|
||||
@@ -85,7 +85,7 @@ pub fn file_sink(
|
||||
let handle = open_file_handle_internal(path, append)
|
||||
{
|
||||
path,
|
||||
append,
|
||||
append: Ref::new(append),
|
||||
handle: Ref::new(handle),
|
||||
formatter,
|
||||
auto_flush,
|
||||
@@ -114,6 +114,10 @@ pub fn FileSink::flush(self : FileSink) -> Bool {
|
||||
}
|
||||
}
|
||||
|
||||
pub fn FileSink::append_mode(self : FileSink) -> Bool {
|
||||
self.append.val
|
||||
}
|
||||
|
||||
pub fn FileSink::close(self : FileSink) -> Bool {
|
||||
match self.handle.val {
|
||||
None => false
|
||||
@@ -142,7 +146,8 @@ pub fn FileSink::flush_failures(self : FileSink) -> Int {
|
||||
}
|
||||
|
||||
pub fn FileSink::reopen(self : FileSink, append~ : Bool? = None) -> Bool {
|
||||
let append_mode = append.unwrap_or(self.append)
|
||||
let append_mode = append.unwrap_or(self.append.val)
|
||||
self.append.val = append_mode
|
||||
match self.handle.val {
|
||||
None => ()
|
||||
Some(handle) => {
|
||||
|
||||
+2
-1
@@ -207,7 +207,8 @@ if native_files_supported() {
|
||||
- Supported keys include `min_level`, `target`, `timestamp`, `sink.kind`, `sink.path`, `sink.append`, `sink.auto_flush`, `sink.rotation`, `sink.text_formatter`, and `queue`.
|
||||
- `sink.rotation` currently supports `max_bytes` and `max_backups` for basic size-based rotation and backup retention.
|
||||
- `file_sink(...)` also exposes `reopen()`, `open_failures()`, `write_failures()`, `flush_failures()`, and `rotation_failures()` for basic observability.
|
||||
- `ConfiguredLogger` built through `build_logger(...)` also exposes `file_reopen()`, `file_flush()`, `file_close()`, and the corresponding file failure counters, so config-driven file logging keeps a usable control surface.
|
||||
- `file_sink(...)` also exposes `append_mode()`. Passing `append=...` to `reopen(...)` updates the current append policy used by later reopen calls.
|
||||
- `ConfiguredLogger` built through `build_logger(...)` also exposes `file_reopen()`, `file_flush()`, `file_close()`, `file_append_mode()`, and the corresponding file failure counters, so config-driven file logging keeps a usable control surface.
|
||||
- `sink.text_formatter.template` currently supports fixed tokens: `{timestamp}`, `{timestamp_ms}`, `{level}`, `{target}`, `{message}`, and `{fields}`.
|
||||
- Config-driven sink assembly currently supports `console`, `json_console`, `text_console`, and `file`.
|
||||
- `queue` remains a synchronous bounded wrapper around the final sink, not an async runtime.
|
||||
|
||||
@@ -32,6 +32,7 @@ version 0.3.0
|
||||
- feat: add `FileSink::reopen()` and basic file sink failure counters via `open_failures()`, `write_failures()`, and `flush_failures()`
|
||||
- feat: forward file sink reopen and failure-counter helpers through `RuntimeSink` and `ConfiguredLogger`
|
||||
- feat: add explicit `file_flush()` and `file_close()` helpers for config-built file sinks, including queued file sink drain-before-close behavior
|
||||
- feat: make file append mode queryable via `append_mode()` / `file_append_mode()` and persist explicit `reopen(append=...)` mode updates for later reopen calls
|
||||
- feat: add `SplitSink`, `split_sink(...)`, and `split_by_level(...)` for routing records into different sinks by predicate or level
|
||||
- feat: add `Logger::bind(...)` as an ergonomic context-binding alias and `fields(...)` helper for tuple-based field construction
|
||||
|
||||
@@ -48,6 +49,7 @@ version 0.3.0
|
||||
- test: cover file sink reopen behavior and backend-dependent failure counter paths
|
||||
- test: cover config-built file logger reopen and failure-counter helper access
|
||||
- test: cover config-built file logger flush/close helpers and queued-file drain semantics
|
||||
- test: cover append-mode observability and reopen-mode persistence for direct and config-built file sinks
|
||||
- test: cover split sink predicate routing and level-based routing behavior
|
||||
- test: cover `bind(...)` context composition and `fields(...)` helper behavior
|
||||
- test: add async logger lifecycle, config roundtrip, and batching/flush policy test seeds
|
||||
@@ -65,6 +67,7 @@ version 0.3.0
|
||||
- docs: document file sink reopen and observability counters in README variants
|
||||
- docs: clarify that config-built file loggers keep file control and observability helpers
|
||||
- docs: document explicit file flush/close helpers for config-built file loggers
|
||||
- docs: clarify append-mode observability and reopen append-policy semantics
|
||||
- docs: add split sink examples for level-based routing
|
||||
- docs: add `bind(...)` examples for reusable context binding
|
||||
- docs: update root README and English README with async adapter notes and current scope
|
||||
|
||||
Reference in New Issue
Block a user