Clarify application and library logger entries

This commit is contained in:
Nanaloveyuki
2026-05-15 11:42:46 +08:00
parent 1b56e1e20a
commit 78423384ea
6 changed files with 105 additions and 4 deletions
+33
View File
@@ -279,3 +279,36 @@ async test "library async logger can be built from config" {
inspect(logger.is_enabled(@bitlogger.Level::Info), content="false")
inspect(full.target, content="async.lib.config")
}
async test "async logger can project to library async logger" {
let logger = build_async_logger(
AsyncLoggerBuildConfig::new(
logger=@bitlogger.LoggerConfig::new(target="async.projected", min_level=@bitlogger.Level::Warn),
async_config=AsyncLoggerConfig::new(max_pending=2),
),
).to_library_async_logger()
inspect(logger.is_enabled(@bitlogger.Level::Error), content="true")
inspect(logger.is_enabled(@bitlogger.Level::Info), content="false")
inspect(logger.to_async_logger().target, content="async.projected")
}
test "application async logger aliases runtime async entry" {
let logger = build_application_async_logger(
AsyncLoggerBuildConfig::new(
logger=@bitlogger.LoggerConfig::new(target="async.app", min_level=@bitlogger.Level::Warn),
async_config=AsyncLoggerConfig::new(max_pending=2),
),
)
inspect(logger.is_enabled(@bitlogger.Level::Error), content="true")
inspect(logger.is_enabled(@bitlogger.Level::Info), content="false")
inspect(logger.target, content="async.app")
}
test "application async logger can be built from config text" {
let logger = parse_and_build_application_async_logger(
"{\"logger\":{\"min_level\":\"warn\",\"target\":\"async.app.json\",\"sink\":{\"kind\":\"console\"}},\"async_config\":{\"max_pending\":2,\"overflow\":\"DropNewest\",\"max_batch\":1,\"linger_ms\":0,\"flush\":\"Never\"}}",
)
inspect(logger.is_enabled(@bitlogger.Level::Error), content="true")
inspect(logger.is_enabled(@bitlogger.Level::Info), content="false")
inspect(logger.target, content="async.app.json")
}
+21
View File
@@ -0,0 +1,21 @@
pub type ApplicationAsyncLogger = AsyncLogger[@bitlogger.RuntimeSink]
pub type ApplicationTextAsyncLogger = AsyncLogger[@bitlogger.FormattedConsoleSink]
pub fn build_application_async_logger(
config : AsyncLoggerBuildConfig,
) -> ApplicationAsyncLogger {
build_async_logger(config)
}
pub fn build_application_text_async_logger(
config : AsyncLoggerBuildConfig,
) -> ApplicationTextAsyncLogger {
build_async_text_logger(config)
}
pub fn parse_and_build_application_async_logger(
input : String,
) -> ApplicationAsyncLogger raise {
build_application_async_logger(parse_async_logger_build_config_text(input))
}
+6 -2
View File
@@ -2,10 +2,14 @@ pub struct LibraryAsyncLogger[S] {
inner : AsyncLogger[S]
}
pub fn[S] library_async_logger(logger : AsyncLogger[S]) -> LibraryAsyncLogger[S] {
fn[S] library_async_logger(logger : AsyncLogger[S]) -> LibraryAsyncLogger[S] {
{ inner: logger }
}
pub fn[S] AsyncLogger::to_library_async_logger(self : AsyncLogger[S]) -> LibraryAsyncLogger[S] {
library_async_logger(self)
}
pub fn[S] LibraryAsyncLogger::new(
sink : S,
config~ : AsyncLoggerConfig = AsyncLoggerConfig::new(),
@@ -28,7 +32,7 @@ pub fn[S] LibraryAsyncLogger::to_async_logger(self : LibraryAsyncLogger[S]) -> A
self.inner
}
pub fn[S] configured_library_async_logger(
fn[S] configured_library_async_logger(
logger : AsyncLogger[S],
) -> LibraryAsyncLogger[S] {
library_async_logger(logger)
+28
View File
@@ -824,3 +824,31 @@ test "library logger can be parsed and built from config text" {
inspect(logger.is_enabled(Level::Info), content="false")
inspect(full.target, content="lib.json")
}
test "configured logger can project to library logger" {
let configured = build_logger(
LoggerConfig::new(min_level=Level::Warn, target="lib.projected"),
)
let logger = configured.to_library_logger()
inspect(logger.is_enabled(Level::Error), content="true")
inspect(logger.is_enabled(Level::Info), content="false")
inspect(logger.to_logger().target, content="lib.projected")
}
test "application logger aliases configured runtime entry" {
let logger = build_application_logger(
LoggerConfig::new(min_level=Level::Warn, target="app.runtime"),
)
inspect(logger.is_enabled(Level::Error), content="true")
inspect(logger.is_enabled(Level::Info), content="false")
inspect(logger.target, content="app.runtime")
}
test "application logger can be built from config text" {
let logger = parse_and_build_application_logger(
"{\"min_level\":\"warn\",\"target\":\"app.json\",\"sink\":{\"kind\":\"console\"}}",
)
inspect(logger.is_enabled(Level::Error), content="true")
inspect(logger.is_enabled(Level::Info), content="false")
inspect(logger.target, content="app.json")
}
+11
View File
@@ -0,0 +1,11 @@
pub type ApplicationLogger = ConfiguredLogger
pub fn build_application_logger(config : LoggerConfig) -> ApplicationLogger {
build_logger(config)
}
pub fn parse_and_build_application_logger(
input : String,
) -> ApplicationLogger raise ConfigError {
parse_and_build_logger(input)
}
+6 -2
View File
@@ -2,10 +2,14 @@ pub struct LibraryLogger[S] {
inner : Logger[S]
}
pub fn[S] library_logger(logger : Logger[S]) -> LibraryLogger[S] {
fn[S] library_logger(logger : Logger[S]) -> LibraryLogger[S] {
{ inner: logger }
}
pub fn[S] Logger::to_library_logger(self : Logger[S]) -> LibraryLogger[S] {
library_logger(self)
}
pub fn[S] LibraryLogger::new(
sink : S,
min_level~ : Level = Level::Info,
@@ -18,7 +22,7 @@ pub fn[S] LibraryLogger::to_logger(self : LibraryLogger[S]) -> Logger[S] {
self.inner
}
pub fn configured_library_logger(logger : ConfiguredLogger) -> LibraryLogger[RuntimeSink] {
fn configured_library_logger(logger : ConfiguredLogger) -> LibraryLogger[RuntimeSink] {
library_logger(logger)
}