Giter Club home page Giter Club logo

local-rolling-file's Introduction

local-rolling-file

local-rolling-file on GitHub Actions local-rolling-file on crates.io rolling-file on docs.rs GitHub: WANG-lp/local-rolling-file license: MIT or Apache-2.0 minimum rustc: 1.42

NOTE: this repo is based on https://github.com/Axcient/rolling-file-rs

A rolling file appender with customizable rolling conditions. Includes built-in support for rolling conditions on date/time (daily, hourly, every minute) and/or size.

Log files structures(with log as folder and log.log as prefix):

  • log.log (a symbol link always points to the latest one log file)
  • log.log.yyyymmdd.hhmmss (e.g. log.log.20240520.010101)
  • ..

This is useful to combine with the tracing crate and tracing_appender::non_blocking::NonBlocking -- use it as an alternative to tracing_appender::rolling::RollingFileAppender.

Examples

use rolling_file::*;
let file_appender = BasicRollingFileAppender::new(
    "./log", // folder
    "log.log", // prefix
    RollingConditionBasic::new().daily(),
    9
).unwrap();
let file_appender = local_rolling_file::RollingFileAppender::new(
        folder,
        "log.log",
        local_rolling_file::RollingConditionBasic::new().daily(),
        3,
    )
    .unwrap();
    let (log_file_writer, guard) = tracing_appender::non_blocking(file_appender);
    let local_time = tracing_subscriber::fmt::time::OffsetTime::new(
        UtcOffset::from_hms(8, 0, 0).unwrap(),
        format_description!("[year]-[month]-[day] [hour]:[minute]:[second].[subsecond digits:6]"),
    );
    let subscriber = tracing_subscriber::Registry::default()
        .with(
            tracing_subscriber::fmt::layer()
                .compact()
                .with_target(false)
                .with_file(true)
                .with_line_number(true)
                .with_ansi(false)
                .with_timer(local_time.clone())
                .with_writer(log_file_writer)
                .with_filter(level),
        )
        .with(
            tracing_subscriber::fmt::layer()
                .compact()
                .with_target(false)
                .with_file(true)
                .with_line_number(true)
                .with_ansi(true)
                .with_timer(local_time)
                .with_filter(level),
        );

    // use that subscriber to process traces emitted after this point
    let _ = tracing::subscriber::set_global_default(subscriber);
    tracing::info!("Logger set up successfully");

Development

Must pass latest stable clippy, be formatted with nightly rustfmt, and pass unit tests:

cargo +nightly fmt
cargo clippy --all-targets
cargo test

License

Dual-licensed under the terms of either the MIT license or the Apache 2.0 license.

Changelog

See CHANGELOG.md

local-rolling-file's People

Contributors

wang-lp avatar ryoqun avatar kevinhoffman avatar

Stargazers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.