--- name: global-warn group: api category: global update-time: 20260512 description: Emit a warn-level record through the shared default logger shortcut. key-word: - global - warn - default - public --- ## Global-warn Emit a warn-level record through the shared default logger. This is the global convenience wrapper for `log(Level::Warn, ...)`. ### Interface ```moonbit pub fn warn(message : String, fields~ : Array[Field] = []) -> Unit {} ``` #### input - `message : String` - Warning message text. - `fields : Array[Field]` - Optional structured fields attached to the record. #### output - `Unit` - No return value. The record is handled through the shared default logger. ### Explanation Detailed rules explaining key parameters and behaviors - This helper delegates to `default_logger().warn(...)`. - Each call therefore reads the current shared default threshold and target at write time instead of holding one long-lived logger value internally. - It uses the shared console sink and current default target. - Warning logs are suited to degraded or suspicious states that do not necessarily stop execution. - This helper offers convenience for a single shared logging path. ### How to Use Here are some specific examples provided. #### When Need A Quick Global Warning When a recoverable issue should be surfaced without creating a logger variable: ```moonbit set_default_target("cache") warn("cache miss ratio increased") ``` In this example, the shared default logger emits a warning-severity record under the `cache` target. If `set_default_target(...)` or `set_default_min_level(...)` changes later, future `warn(...)` calls will observe those updated shared defaults automatically. #### When Attach Structured Warning Context When the warning should include machine-readable detail: ```moonbit warn("retry scheduled", fields=[field("attempt", "3")]) ``` In this example, the global helper still supports structured metadata. ### Error Case e.g.: - If the shared minimum level is above `Warn`, the record is skipped. - If subsystem-specific routing is needed, a shared global warning path may be too limited. ### Notes 1. Global warnings are convenient for simple apps and scripts. 2. Future calls pick up later shared-default changes because the helper forwards through a fresh `default_logger()` each time. 3. Warning-level global events are often the first candidate for separate monitoring or routing later.