#logging #structured #console #configurable #level #log-level #file

custom_logger

A lightweight logger crate for structured and configurable logging to console and file

1 unstable release

new 0.1.0 Jan 10, 2025

#350 in Debugging

Download history 124/week @ 2025-01-08

124 downloads per month

MIT license

17KB
332 lines

Custom Logger

A lightweight, structured logger built from scratch with support for multiple output backends and configurable logging levels. Designed to log efficiently to the console, files, and other backends while maintaining a focus on structured logging with key-value pairs.

Features

  • Multiple Outputs: Supports logging to the console and files.
  • Configurable Levels: Easily set logging levels (e.g., trace, debug, info, warn, error).
  • Structured Logging: Accepts key-value pairs for clear and structured logs.
  • Minimal Dependencies: Only depends on the chrono crate for timestamps.
  • Expandable: Additional logging backends can be added as needed.

Installation

Add this crate to your Cargo.toml:

[dependencies]  
custom_logger = { git = "https://github.com/Shourya742/logger.git" }  

Example Usage

Setup

To initialize the logger:

use custom_logger::{init_logger, info, error, trace}; 
use sinks::console::ConsoleSink;
use std::str::FromStr; 

fn main() {  
    // Initialize logger with file and console output
    init_logger(log_level::LogLevel::Trace, Box::new(ConsoleSink));

    trace!("This is a trace log.");
    info!("This is an info log.");
    error!("This is an error log.");
}  

Output Example

2025-01-10T16:35:37.689459275+00:00 TRACE logger::test - This is a trace log.
2025-01-10T16:35:37.689475106+00:00 INFO logger::test - This is an info log.
2025-01-10T16:35:37.689478106+00:00 ERROR logger::test - This is an error log. 

Structured Logging

You can pass key-value pairs directly for structured logs:

info!(user_id = 42, action = "login", "User action recorded");  
debug!(config = ?config, status = "loading", "Loading configuration");  

Contributing

Contributions are welcome! Feel free to open issues or submit pull requests.

Dependencies

~1MB
~18K SLoC