# log4rc
A log4j-like logging framework for Rust.
## Overview
`log4rc` is a simple, extensible logging framework inspired by log4j. It provides different log levels and an easy-to-use API for Rust applications.
## Features
- Multiple log levels (Trace, Debug, Info, Warn, Error)
- Simple API similar to log4j
- Thread-safe logging
- Timestamped log entries
- Configurable log levels
- File appender (append logs to disk)
- Cheap to disable: filtered-out levels skip formatting entirely
## Installation
Add this to your `Cargo.toml`:
```toml
[dependencies]
log4rc = "0.1"
```
## Example
```rust
use log4rc::{LogLevel, Logger};
fn main() {
// Initialize the logger with Debug level
Logger::init(LogLevel::Debug);
// Test all log levels
log4rc::trace!("This is a trace message - won't be printed");
log4rc::debug!("This is a debug message - will be printed");
log4rc::info!("This is an info message - will be printed");
log4rc::warn!("This is a warning message - will be printed");
log4rc::error!("This is an error message - will be printed");
}
```
## Custom appenders
`log4rc` is extensible: implement the `Appender` trait (output target) and the `Layout` trait (message formatting), then build a logger with `Logger::builder`.
```rust
use log4rc::{Appender, LogEvent, LogLevel, Logger, SimpleLayout};
struct MyAppender;
impl Appender for MyAppender {
fn append(&self, event: &LogEvent) {
// Write `event.message` somewhere, e.g. a file or a network socket
}
}
fn main() {
Logger::builder(LogLevel::Debug)
.appender(Box::new(MyAppender))
.init();
}
```
You can add multiple appenders, and `Logger::init` remains a shortcut that installs a console appender with the default layout.
### File appender
`FileAppender` appends formatted events to a file (creating it if needed) and flushes after every event:
```rust
use log4rc::{ConsoleAppender, FileAppender, LogLevel, Logger, SimpleLayout};
Logger::builder(LogLevel::Info)
.appender(Box::new(ConsoleAppender::new(Box::new(SimpleLayout))))
.appender(Box::new(FileAppender::open("app.log", Box::new(SimpleLayout)).unwrap()))
.init();
```
## License
This project is licensed under the MIT License - see the [LICENSE](LICENSE) file for details.