mirror of
https://github.com/Nanaloveyuki/BitLogger.git
synced 2026-07-28 02:42:21 +00:00
📝 add Chinese API guides and locale detection
This commit is contained in:
@@ -0,0 +1,50 @@
|
||||
# 异步生命周期 API
|
||||
|
||||
异步 API 从 combined config 构建 Logger,再以单 worker 生命周期处理待写入记录。可执行 `async fn main` 仍应按目标平台边界使用。
|
||||
|
||||
## 解析与构建
|
||||
|
||||
```moonbit
|
||||
pub fn parse_async_logger_build_config_text(
|
||||
input : String,
|
||||
) -> AsyncLoggerBuildConfig raise
|
||||
|
||||
pub fn build_async_logger(
|
||||
config : AsyncLoggerBuildConfig,
|
||||
) -> AsyncLogger[RuntimeSink]
|
||||
```
|
||||
|
||||
解析函数同时验证 `logger` 与 `async_config` 两部分;缺少任一部分时使用对应默认配置。构建函数先复用同步 `build_logger(config.logger)` 路径,再包裹异步层,因此保留同步 sink、文件与可选同步队列的运行时语义。
|
||||
|
||||
```moonbit
|
||||
let config = @async_log.parse_async_logger_build_config_text(raw) catch {
|
||||
err => { ignore(err); return }
|
||||
}
|
||||
let logger = @async_log.build_async_logger(config)
|
||||
```
|
||||
|
||||
## `run()`
|
||||
|
||||
```moonbit
|
||||
pub async fn AsyncLogger::run(self : AsyncLogger[S]) -> Unit
|
||||
```
|
||||
|
||||
启动 drain worker。单个 Logger 只能同时拥有一个 worker;已经运行时再次调用会失败,关闭后的 Logger 也不能重新启动。worker 正常在队列关闭时退出;worker 失败时会记录状态并将错误向上传递。
|
||||
|
||||
## `shutdown()`
|
||||
|
||||
```moonbit
|
||||
pub async fn AsyncLogger::shutdown(
|
||||
self : AsyncLogger[S],
|
||||
clear? : Bool = false,
|
||||
) -> Unit
|
||||
```
|
||||
|
||||
`clear=false` 先等待 idle 再关闭;`clear=true` 立即关闭并放弃待处理记录。两条路径都会等待活动 worker 退出。没有运行 worker 且仍有待处理记录时,`clear=false` 可能无法推进,因此应用应保证先启动 `run()`。
|
||||
|
||||
## 英文原始 API
|
||||
|
||||
- [`parse_async_logger_build_config_text(...)`](../../api/parse-async-logger-build-config-text.md)
|
||||
- [`build_async_logger(...)`](../../api/build-async-logger.md)
|
||||
- [`run()`](../../api/async-logger-run.md)
|
||||
- [`shutdown()`](../../api/async-logger-shutdown.md)
|
||||
@@ -0,0 +1,55 @@
|
||||
# 基础记录 API
|
||||
|
||||
控制台示例依赖以下四个入口:先用 preset 描述输出,再由 `build_logger(...)` 构建运行时 Logger,最后用 `field(...)` 附加结构化上下文。
|
||||
|
||||
## `console(...)` 与 `json_console(...)`
|
||||
|
||||
```moonbit
|
||||
pub fn console(
|
||||
min_level~ : Level = Level::Info,
|
||||
target~ : String = "",
|
||||
timestamp~ : Bool = false,
|
||||
) -> LoggerConfig
|
||||
|
||||
pub fn json_console(
|
||||
min_level~ : Level = Level::Info,
|
||||
target~ : String = "",
|
||||
timestamp~ : Bool = false,
|
||||
) -> LoggerConfig
|
||||
```
|
||||
|
||||
两者都只生成 `LoggerConfig`,不会直接创建运行时 Logger。`console(...)` 适合人读输出,`json_console(...)` 适合按行采集的机器读取输出;默认都不带队列。
|
||||
|
||||
## `field(...)`
|
||||
|
||||
```moonbit
|
||||
pub fn field(key : String, value : String) -> Field
|
||||
```
|
||||
|
||||
创建一个结构化字段。它保留原始 key/value,不做去重、归一化或校验;应使用稳定字段名,便于下游筛选。
|
||||
|
||||
```moonbit
|
||||
logger.info("accepted", fields=[@log.field("user", "alice")])
|
||||
```
|
||||
|
||||
## `build_logger(...)`
|
||||
|
||||
```moonbit
|
||||
pub fn build_logger(config : LoggerConfig) -> ConfiguredLogger
|
||||
```
|
||||
|
||||
这是同步 config 到运行时的桥梁:它先按 `config.sink` 构建 `RuntimeSink`,再应用可选的 `config.queue`。返回值仍保留普通日志方法,以及适用时的队列和文件控制方法。
|
||||
|
||||
```moonbit
|
||||
let logger = @log.build_logger(
|
||||
@log.console(min_level=@log.Level::Info, target="service"),
|
||||
)
|
||||
logger.info("ready", fields=[@log.field("port", "8080")])
|
||||
```
|
||||
|
||||
## 英文原始 API
|
||||
|
||||
- [`console(...)`](../../api/console.md)
|
||||
- [`json_console(...)`](../../api/json-console.md)
|
||||
- [`field(...)`](../../api/field.md)
|
||||
- [`build_logger(...)`](../../api/build-logger.md)
|
||||
@@ -0,0 +1,52 @@
|
||||
# Sink 组合 API
|
||||
|
||||
当预设输出不足以表达路由规则时,使用 typed `Logger::new(...)` 和 sink 组合函数。它们接收完整 `Record`,因此仍保留 level、target、message 与 fields。
|
||||
|
||||
## `Logger::new(...)`
|
||||
|
||||
```moonbit
|
||||
pub fn Logger::new(
|
||||
sink : S,
|
||||
min_level~ : Level = Level::Info,
|
||||
target~ : String = "",
|
||||
) -> Logger[S]
|
||||
```
|
||||
|
||||
从任意 `Sink` 实现创建 typed Logger。`min_level` 在任何 sink 写入前生效;`target` 是默认值,之后可由 `with_target(...)`、`child(...)` 或单次 `log(..., target=...)` 覆盖。
|
||||
|
||||
## `fanout_sink(...)`
|
||||
|
||||
```moonbit
|
||||
pub fn fanout_sink(left : A, right : B) -> FanoutSink[A, B]
|
||||
```
|
||||
|
||||
每条记录写入两个目的地,适合控制台加 JSON,或控制台加 callback。右侧获得独立记录副本,两个写入在记录值层面互不影响。
|
||||
|
||||
## `split_by_level(...)`
|
||||
|
||||
```moonbit
|
||||
pub fn split_by_level(
|
||||
left : A,
|
||||
right : B,
|
||||
min_level~ : Level = Level::Warn,
|
||||
) -> SplitSink[A, B]
|
||||
```
|
||||
|
||||
达到阈值的记录进入 `left`,低于阈值的记录进入 `right`。典型用法是把 warning/error 单独输出,而 info/debug 留在普通控制台。
|
||||
|
||||
## `callback_sink(...)`
|
||||
|
||||
```moonbit
|
||||
pub fn callback_sink(
|
||||
callback : (Record) -> Unit,
|
||||
) -> CallbackSink
|
||||
```
|
||||
|
||||
将完整结构化 `Record` 交给应用回调。适合测试、自定义桥接与集成;它不自动格式化记录。
|
||||
|
||||
## 英文原始 API
|
||||
|
||||
- [`Logger::new(...)`](../../api/logger-new.md)
|
||||
- [`fanout_sink(...)`](../../api/fanout-sink.md)
|
||||
- [`split_by_level(...)`](../../api/split-by-level.md)
|
||||
- [`callback_sink(...)`](../../api/callback-sink.md)
|
||||
@@ -0,0 +1,53 @@
|
||||
# 配置与队列 API
|
||||
|
||||
这一组 API 将外部 JSON 或 typed config 变成同步 Logger,并明确规定满队列和关闭时的处理方式。
|
||||
|
||||
## `parse_logger_config_text(...)`
|
||||
|
||||
```moonbit
|
||||
pub fn parse_logger_config_text(input : String) -> LoggerConfig raise ConfigError
|
||||
```
|
||||
|
||||
解析并验证 JSON,但不构建运行时 Logger。缺省字段使用构造器默认值;无效 JSON、错误类型、未知枚举值或空文件路径都会抛出 `ConfigError`。
|
||||
|
||||
```moonbit
|
||||
let config = @log.parse_logger_config_text(raw) catch {
|
||||
err => { ignore(err); return }
|
||||
}
|
||||
let logger = @log.build_logger(config)
|
||||
```
|
||||
|
||||
## `with_queue(...)`、`QueueConfig` 与溢出策略
|
||||
|
||||
```moonbit
|
||||
pub fn with_queue(
|
||||
config : LoggerConfig,
|
||||
max_pending~ : Int = 0,
|
||||
overflow~ : QueueOverflowPolicy = QueueOverflowPolicy::DropNewest,
|
||||
) -> LoggerConfig
|
||||
|
||||
pub fn QueueConfig::new(
|
||||
max_pending : Int,
|
||||
overflow~ : QueueOverflowPolicy = QueueOverflowPolicy::DropNewest,
|
||||
) -> QueueConfig
|
||||
```
|
||||
|
||||
`with_queue(...)` 是 preset 组合入口;`QueueConfig::new(...)` 用于手写 `LoggerConfig`。两者配置同步 `QueuedSink`,不是异步包的 worker 队列。
|
||||
|
||||
`DropNewest` 丢弃新到记录,`DropOldest` 丢弃最早待处理记录。选择取决于业务更需要保留早期还是最新状态。
|
||||
|
||||
## `flush()`
|
||||
|
||||
```moonbit
|
||||
pub fn ConfiguredLogger::flush(self : ConfiguredLogger) -> Bool
|
||||
```
|
||||
|
||||
在退出或需要确认输出边界时调用。对于队列和文件 sink,它会走对应运行时 flush 路径;返回值表示该路径是否成功。
|
||||
|
||||
## 英文原始 API
|
||||
|
||||
- [`parse_logger_config_text(...)`](../../api/parse-logger-config-text.md)
|
||||
- [`with_queue(...)`](../../api/with-queue.md)
|
||||
- [`QueueConfig`](../../api/queue-config.md)
|
||||
- [`QueueOverflowPolicy`](../../api/queue-overflow-policy.md)
|
||||
- [`flush()`](../../api/configured-logger-flush.md)
|
||||
@@ -0,0 +1,49 @@
|
||||
# 文件输出 API
|
||||
|
||||
文件输出是 native 能力。先检查能力,再创建文件配置,最后在有序退出时 flush 并关闭。
|
||||
|
||||
## `native_files_supported()`
|
||||
|
||||
```moonbit
|
||||
pub fn native_files_supported() -> Bool
|
||||
```
|
||||
|
||||
native 后端当前返回 `true`,非文件能力后端返回 `false`。这是运行时能力检查,不保证后续文件操作一定成功,路径、权限和文件系统状态仍可能导致失败。
|
||||
|
||||
## `file(...)` 与 `with_file_rotation(...)`
|
||||
|
||||
```moonbit
|
||||
pub fn file(
|
||||
path : String,
|
||||
min_level~ : Level = Level::Info,
|
||||
target~ : String = "",
|
||||
timestamp~ : Bool = false,
|
||||
append~ : Bool = true,
|
||||
auto_flush~ : Bool = true,
|
||||
rotation~ : FileRotation? = None,
|
||||
text_formatter~ : TextFormatterConfig = default_text_formatter_config(),
|
||||
) -> LoggerConfig raise ConfigError
|
||||
|
||||
pub fn with_file_rotation(
|
||||
config : LoggerConfig,
|
||||
max_bytes : Int,
|
||||
max_backups~ : Int = 1,
|
||||
) -> LoggerConfig
|
||||
```
|
||||
|
||||
`file(...)` 要求非空路径。`with_file_rotation(...)` 只对 file config 生效;输入不是文件 sink 时原样返回。`max_bytes` 是活动文件上限,`max_backups` 是保留的轮转备份数。
|
||||
|
||||
## `file_close()`
|
||||
|
||||
```moonbit
|
||||
pub fn ConfiguredLogger::file_close(self : ConfiguredLogger) -> Bool
|
||||
```
|
||||
|
||||
关闭 config 构建的文件 sink。队列文件 sink 会依次 drain、flush、close;只有整条路径成功且期间没有新的写入、flush 或轮转失败时才返回 `true`。非文件 sink 返回 `false`。
|
||||
|
||||
## 英文原始 API
|
||||
|
||||
- [`native_files_supported()`](../../api/native-files-supported.md)
|
||||
- [`file(...)`](../../api/file.md)
|
||||
- [`with_file_rotation(...)`](../../api/with-file-rotation.md)
|
||||
- [`file_close()`](../../api/configured-logger-file-close.md)
|
||||
@@ -0,0 +1,40 @@
|
||||
# 文本格式 API
|
||||
|
||||
文本格式只改变呈现,不改变 `Record` 的结构。需要稳定机器消费时,优先使用 `json_console(...)`。
|
||||
|
||||
## `text_console(...)`
|
||||
|
||||
```moonbit
|
||||
pub fn text_console(
|
||||
min_level~ : Level = Level::Info,
|
||||
target~ : String = "",
|
||||
timestamp~ : Bool = false,
|
||||
text_formatter~ : TextFormatterConfig = default_text_formatter_config(),
|
||||
) -> LoggerConfig
|
||||
```
|
||||
|
||||
生成 `SinkKind::TextConsole` 配置,并复制 formatter 配置。和其他 preset 一样,它不直接构建 Logger,也默认不带队列。
|
||||
|
||||
## `TextFormatterConfig` 与 `text_formatter(...)`
|
||||
|
||||
`TextFormatterConfig::new(...)` 用于 config-first 路径,控制 timestamp、level、target、fields、分隔符与 template。`text_formatter(...)` 构建直接传给 `text_console_sink(...)` 的运行时 formatter。
|
||||
|
||||
```moonbit
|
||||
let config = @log.text_console(
|
||||
text_formatter=@log.TextFormatterConfig::new(
|
||||
show_timestamp=false,
|
||||
template="[{level}] {target} {message} :: {fields}",
|
||||
),
|
||||
)
|
||||
```
|
||||
|
||||
## `ColorMode`
|
||||
|
||||
`ColorMode` 控制文本 formatter 的颜色策略。终端样式只服务可读性;业务语义仍应放在 level、target 和 fields 中。
|
||||
|
||||
## 英文原始 API
|
||||
|
||||
- [`text_console(...)`](../../api/text-console.md)
|
||||
- [`TextFormatterConfig`](../../api/text-formatter-config.md)
|
||||
- [`text_formatter(...)`](../../api/text-formatter.md)
|
||||
- [`ColorMode`](../../api/color-mode.md)
|
||||
@@ -0,0 +1,14 @@
|
||||
# 高频 API
|
||||
|
||||
这一层只覆盖 Examples 和 Extend 中直接使用、或应用启动时高频需要的 API。它按使用主题归纳,避免把阅读路径重新拆回一函数一页。
|
||||
|
||||
| 场景 | 中文说明 | 英文精确参考 |
|
||||
| --- | --- | --- |
|
||||
| 第一条结构化日志 | [基础记录](./basics.md) | [完整 API 索引](../../api/index.md) |
|
||||
| 配置、队列与 flush | [配置与队列](./configuration.md) | [config 分类](../../api/index.md) |
|
||||
| native 文件输出 | [文件输出](./file-output.md) | [file/sink 分类](../../api/index.md) |
|
||||
| 终端可读性 | [文本格式](./formatting.md) | [formatter 分类](../../api/index.md) |
|
||||
| 后台异步处理 | [异步生命周期](./async.md) | [async 分类](../../api/index.md) |
|
||||
| 多目的地与按 level 路由 | [Sink 组合](./composition.md) | [sink 分类](../../api/index.md) |
|
||||
|
||||
每个中文页面保留高频签名与行为边界;需要全部参数、错误条件或较少使用的公开符号时,沿页面内链接进入英文原始 API。
|
||||
Reference in New Issue
Block a user