Crate rlg

source ·
Expand description

§RustLogs (RLG)

RustLogs (RLG) is a library that implements application-level logging in a simple, readable output format. The library provides logging APIs and various helper macros that simplify many common logging tasks.

Rust

§Overview

RustLogs (RLG) is a library that implements application-level logging in a simple, readable output format. The library provides logging APIs and various helper macros that simplify many common logging tasks.

§Features

  • Supports many log levels: ALL, DEBUG, DISABLED, ERROR, FATAL, INFO, NONE, TRACE, VERBOSE, and WARNING.
  • Provides structured log formats that are easy to parse and filter.
  • Compatible with multiple output formats including:
    • Common Event Format (CEF)
    • Extended Log Format (ELF)
    • Graylog Extended Log Format (GELF)
    • JavaScript Object Notation (JSON)
    • NCSA Common Log Format (CLF)
    • W3C Extended Log File Format (W3C)
    • Syslog Format
    • Apache Access Log Format
    • Logstash Format
    • Log4j XML Format
    • NDJSON (Newline Delimited JSON)
    • and many more.

§Usage

Add this to your Cargo.toml:

[dependencies]
rlg = "0.0.4"

§Configuration

By default, RustLogs (RLG) logs to a file named “RLG.log” in the current directory. You can customize the log file path by setting the LOG_FILE_PATH environment variable.

§Examples

§Basic Logging

use rlg::log::Log;
use rlg::log_format::LogFormat;
use rlg::log_level::LogLevel;

// Create a new log entry
let log_entry = Log::new(
    "12345",
    "2023-01-01T12:00:00Z",
    &LogLevel::INFO,
    "MyComponent",
    "This is a sample log message",
    &LogFormat::JSON, // Choose from various formats like JSON, Syslog, NDJSON, etc.
);

// Log the entry asynchronously
tokio::runtime::Runtime::new().unwrap().block_on(async {
    log_entry.log().await.unwrap();
});

§Custom Log Configuration

use rlg::config::Config;
use rlg::log::Log;
use rlg::log_format::LogFormat;
use rlg::log_level::LogLevel;

// Customize log file path
std::env::set_var("LOG_FILE_PATH", "/path/to/log/file.log");

// Load custom configuration
let config = Config::load();

// Create a new log entry with custom configuration
let log_entry = Log::new(
    "12345",
    "2023-01-01T12:00:00Z",
    &LogLevel::INFO,
    "MyComponent",
    "This is a sample log message",
    &LogFormat::ApacheAccessLog
);

// Log the entry asynchronously
tokio::runtime::Runtime::new().unwrap().block_on(async {
    log_entry.log().await.unwrap();
});

§Error Handling

Errors can occur during logging operations, such as file I/O errors or formatting errors. The log() method returns a Result<(), io::Error> that indicates the outcome of the logging operation. You should handle potential errors appropriately in your code.

use rlg::log::Log;
use rlg::log_format::LogFormat;
use rlg::log_level::LogLevel;

// Create a new log entry
let log_entry = Log::new(
    "12345",
    "2023-01-01T12:00:00Z",
    &LogLevel::INFO,
    "MyComponent",
    "This is a sample log message",
    &LogFormat::NDJSON, // Using NDJSON format for this example
);

// Log the entry asynchronously and handle potential errors
tokio::runtime::Runtime::new().unwrap().block_on(async {
    match log_entry.log().await {
        Ok(_) => println!("Log entry successfully written"),
        Err(err) => eprintln!("Error logging entry: {}", err),
    }
});

Modules§

  • The config module contains the configuration struct for the logging system.
  • The log module contains the log struct and its implementation.
  • The log_format module contains the log format enumeration and its implementation.
  • The log_level module contains the log level enumeration and its implementation.
  • The macros module contains functions for generating macros.

Macros§

  • Conditional debug logging Logs if debug_enabled feature flag set Conditional debug logging
  • Macro for error log with default format Macro for error log with default format
  • Macro for fatal log Macro for fatal log
  • Macro for info log with default session id and format Usage: let log = macro_info_log!(time, component, description); Macro for info log with default session id and format
  • Macro to create a new log easily Usage: let log = macro_log!(session_id, time, level, component, description, format); Macro to create a new log easily
  • Conditional logging based on a predicate Usage: macro_log_if!(predicate, log); Conditional logging based on a predicate
  • Async log message to file Usage: let result = macro_log_to_file!(log); Async log message to file
  • Macro for logging with metadata Usage: let log = macro_log_with_metadata!(session_id, time, level, component, description, format); println!(“{log} | Metadata: {metadata}”); Macro for logging with metadata
  • Print log to stdout Usage: macro_print_log!(log); Print log to stdout
  • Set log format if not already defined Usage: macro_set_log_format_clf!(log); Set log format if not already defined
  • Macro for trace log Macro for trace log
  • Macro for warn log Macro for warn log