Giter VIP home page Giter VIP logo

async_logger_log's Introduction

async_logger_log

Rust

Asynchronous logger is a performant implementation of log facade. The implementation is based on async_logger crate, and allows non-blocking writes of log records in memory buffer, which in turn then processed in separate thread by writer (see more details in async_logger documentation).

Default log record format includes date, time, timezone, log level, target, and log message itself. Log record example:

[2020-03-15 11:47:32.339865887+0100 WARN thread]: log message.

The log record format, and other parameters are customizable with LoggerBuilder.

Examples

use async_logger_log::Logger;
use log::{info, warn};

let logger = Logger::new("/tmp", 256, 10*1024*1024).expect("Failed to create Logger instance");

log::set_boxed_logger(Box::new(logger)).expect("Failed to set logger");
log::set_max_level(log::LevelFilter::Info);

info!("{}", "test msg");
warn!("{}", "warning msg");

log::logger().flush();

Custom writer and formatter:

use async_logger_log::Logger;
use async_logger::Writer;
use log::{debug, Record};

// Custom formatting of `log::Record`
fn custom_formatter(record: &Record) -> String {
    format!("log record: {}\n", record.args())
}

struct StdoutWriter {}

// Writer simply prints log messages to stdout
impl Writer<Box<String>> for StdoutWriter {

    fn process_slice(&mut self, slice: &[Box<String>]) {
        for item in slice {
            println!("{}", **item);
        }
    }

    fn flush(&mut self) { }
}

let logger = Logger::builder()
    .buf_size(256)
    .formatter(custom_formatter)
    .writer(Box::new(StdoutWriter {}))
    .build()
    .unwrap();

log::set_boxed_logger(Box::new(logger)).expect("Failed to set logger");
log::set_max_level(log::LevelFilter::Trace);

debug!("{}", "Hello, Wrold!");

log::logger().flush();

Notes

  1. Dependency on time crate is optional and can be excluded by adding in Cargo.toml:
[dependencies.async_logger_log]
default-features = false
  1. This crate was tested on Linux x86_64. Rust version 1.42.

async_logger_log's People

Contributors

stencillogic avatar

Stargazers

 avatar

Watchers

 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.