moonlog-file

    Rotating file sink for moonbitstack/moonlog.

    moonlog
    log
    file
    log-rotation
    gzip
    moonbit
    Download zip
    Version
    0.1.0
    License
    Apache-2.0
    Last updated
    37 minutes ago
    Downloads
    4

    #moonlog-file

    Rotating file sink for moonbitstack/moonlog.

    File

    pub struct File {
    path : String
    append : Bool
    perms : Int
    mkdir : Bool
    rotation : Rotation
    limits : Limits
    compress : Bool
    buffer : Int
    clock : () -> Int64
    handle : Int?
    written : Int64
    opened : Int64
    held :
    Buffer

    }

    A sink that appends each entry to a file, rotating it when it grows too large or too old.

    The file is opened on the first write, so a sink that cannot open its file is a failing slot rather than a failure at startup: the health machine puts the sink down, the alarm says why, and a probe retries. reopen picks up a file that an outside tool moved away.

    Segments are named <file>.<20261101T090000>, or .gz too when compress is on, and are pruned by Limits after every cut. Names are UTC, so a directory listing reads as a history.
    impl Sink for File

    File::close

    fn File::close(self : File) -> Unit

    Writes what the buffer holds, then gives the file back to the system. A sink that is buffered should be closed, or its last lines are lost.

    File::flush

    fn File::flush(self : File) -> Result[Unit, String]

    Writes what the buffer holds.

    File::new

    fn File::new(path~ : String, append? : Bool, perms? : Int, mkdir? : Bool, rotation? : Rotation, limits? : Limits, compress? : Bool, buffer? : Int, clock? : () -> Int64) -> File

    A file sink on path.

    append keeps what is there (the default) and false starts the file empty. perms is the mode a new file gets, in the usual octal form. mkdir creates the parent directories. buffer gathers that many bytes before writing, which trades a lost tail on a crash for fewer calls; 0 writes every line as it arrives.

    clock answers nanoseconds since the epoch. A file sink needs one for segment names and time-based rotation, so it reads the system clock unless it is given another.

    File::path

    fn File::path(self : File) -> String

    The file this sink writes.

    File::reopen

    fn File::reopen(self : File) -> Result[Unit, String]

    Closes the file and opens it again, which is what an outside rotate wants: the old descriptor keeps writing to a file that was moved, this one writes to the path again.

    File::rotate

    fn File::rotate(self : File) -> Result[Unit, String]

    Cuts the file now, whether or not a limit was reached: the current file becomes a segment and a fresh one is started.

    File::segments

    fn File::segments(self : File) -> Array[String]

    The segments of this file, oldest first.

    A segment is a file this sink cut and left behind: what a status page lists, and what keep, max_age and max_total count.

    File::slot

    fn File::slot(self : File, name? : String) ->
    Slot

    A slot around this sink.

    File::write

    fn File::write(self : File, entry :
    Entry
    ) -> Result[Unit, String]

    Appends the line to the file, through rotation, compression and the buffer.

    File::written

    fn File::written(self : File) -> Int64

    How many bytes the current file holds.

    Limits

    pub(all) struct Limits {
    keep : Int
    max_age : Int64
    max_total : Int64
    }

    What becomes of the files rotation leaves behind.

    All three are ceilings: zero keeps everything. max_age and max_total are only applied to segments, never to the file being written.

    Limits::keep

    fn Limits::keep(self : Limits, keep : Int) -> Limits

    The same limits, keeping at most keep segments.

    Limits::max_age

    fn Limits::max_age(self : Limits, max_age : Int64) -> Limits

    The same limits, dropping segments older than max_age nanoseconds.

    Limits::max_total

    fn Limits::max_total(self : Limits, max_total : Int64) -> Limits

    The same limits, dropping the oldest segments until the rest fit in max_total bytes.

    Limits::new

    fn Limits::new(keep? : Int, max_age? : Int64, max_total? : Int64) -> Limits

    Keep every segment, for ever.

    Rotation

    pub(all) struct Rotation {
    size : Int64
    every : Int64
    }

    When a file is rotated.

    size is a limit in bytes and every a span in nanoseconds; zero turns either one off. A file that has been written to rotates before the line that would cross the limit, so a line never straddles two files.

    Rotation::every

    fn Rotation::every(self : Rotation, every : Int64) -> Rotation

    The same rotation, with a time limit.

    Rotation::new

    fn Rotation::new(size? : Int64, every? : Int64) -> Rotation

    No rotation: the file grows until something else deals with it.

    Rotation::size

    fn Rotation::size(self : Rotation, size : Int64) -> Rotation

    The same rotation, with a size limit.