logfly

    Logging for MoonBit: plain text and JSONL appenders with timestamps, and size-triggered log rotation

    log
    logging
    jsonl
    rotate
    append
    Download zip
    Author
    Version
    0.3.0
    License
    MIT
    Last updated
    7 hours ago
    Downloads
    6

    #chensuiyi/logfly

    Logging for MoonBit: plain text / JSONL line formats with automatic ISO timestamps, plus size-triggered rotation in rename or copytruncate mode.

    #Install

    moon add chensuiyi/logfly

    #Features

    • plain: one timestamp-prefixed text line — crash records, human-read logs
    • event: a JSONL management event; values arrive as pre-encoded fragments, so fields the caller omits never appear on the wire
    • maybe_rotate: rotate past a size limit in rename or copytruncate mode; a missing file is not an error
    • Depends on chensuiyi/fsx + chensuiyi/dateku + chensuiyi/fndash

    #Scenarios

    • Daemon event logs: JSONL, machine-parseable, dedupe-friendly
    • Crash records: plain text, human-read
    • Long-running services: size-triggered rotation keeps disk usage bounded

    #API

    FunctionDescription
    plain(path, text)Timestamp-prefixed text line
    event(path, event, fields)JSONL event line
    maybe_rotate(path, max_bytes, copytruncate?)Rotate past limit, returns rotated

    // Management event: omitted optional fields never appear on the wire
    @logfly.event("/var/lib/app/events.jsonl", "spawn_failed", [
    ("app", @fndash.jstr("api")),
    ("pid", "42"),
    ])

    // Crash record: plain text with automatic timestamp
    @logfly.plain("/var/log/app/crash.log", "app=api reason=exit_1")

    // Rotate past 10 MB
    @logfly.maybe_rotate("/var/log/app/events.jsonl", 10485760)

    #Author

    #License

    MIT

    #chensuiyi/logfly

    MoonBit 日志库:纯文本 / JSONL 两种行格式,自动 ISO 时间戳,按大小触发轮转(rename 与 copytruncate 双模式)。

    #安装

    moon add chensuiyi/logfly

    #功能

    • plain:一行时间戳前缀文本——崩溃记录、人读日志
    • event:JSONL 管理事件,值传已编码片段——调用方省略的字段就不会出现在线上
    • maybe_rotate:超限轮转;文件不存在不算错
    • 依赖:chensuiyi/fsx + chensuiyi/dateku + chensuiyi/fndash

    #场景

    • 守护进程事件日志(JSONL,机器可解析)
    • 崩溃记录与人读日志(纯文本)
    • 长驻服务:按大小轮转控制磁盘占用

    #API

    函数说明
    plain(path, text)时间戳前缀文本行
    event(path, event, fields)JSONL 事件行
    maybe_rotate(path, max_bytes, copytruncate?)超限轮转,返回是否轮转

    #用法示例

    // 管理事件:省略的可选字段不会以 null 出现在线上
    @logfly.event("/var/lib/app/events.jsonl", "spawn_failed", [
    ("app", @fndash.jstr("api")),
    ("pid", "42"),
    ])

    // 崩溃记录:纯文本,自动时间戳
    @logfly.plain("/var/log/app/crash.log", "app=api reason=exit_1")

    // 超过 10 MB 轮转
    @logfly.maybe_rotate("/var/log/app/events.jsonl", 10485760)

    #作者

    陈随易 (@chenbimo)

    #协议

    MIT

    event

    fn event(path : String, event : String, fields : Array[(String, String)]) -> Unit raise
    FileError

    Append one JSONL management event: {"ts":"...","event":"...","<key>":<value>...}. Keys are quoted by the library; values must arrive as already-encoded JSON fragments — pass @fndash.jstr(text) for strings and raw digits for numbers, so a field the caller omits simply does not appear on the wire.

    maybe_rotate

    fn maybe_rotate(path : String, max_bytes : Int64, copytruncate? : Bool) -> Bool raise
    FileError

    Rotate path once it exceeds max_bytes: copytruncate for logs held open by app processes (they keep writing to the old inode), rename for logs written by the caller itself. Returns true when a rotation ran. Raises FileError when a needed rotation failed; a missing file is not an error (nothing to rotate).

    plain

    fn plain(path : String, text : String) -> Unit raise
    FileError

    Append one plain text line, prefixed with the current ISO timestamp: 2026-10-03T08:15:30.123Z <text>. Failures raise so the caller stays in charge of best-effort handling.

    Source Files