# 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. Size-based rotation is available via the builder — once the file reaches the limit, it is rolled over to a timestamped sibling (e.g. `app.log.20260809-102530-1`). `max_files` bounds how many rotated files are kept (the oldest are deleted once the count is exceeded), and `.compressed()` gzips rotated files:
```rust
use log4rc::{ConsoleAppender, FileAppender, LogLevel, Logger, SimpleLayout};
Logger::builder(LogLevel::Info)
.appender(Box::new(ConsoleAppender::new(Box::new(SimpleLayout))))
.appender(Box::new(
FileAppender::builder("app.log", Box::new(SimpleLayout))
.max_size(10 * 1024 * 1024) // roll at 10 MiB
.max_files(5) // keep at most 5 rotated files
.compressed() // gzip rotated files as app.log.<timestamp>.gz
.build()
.unwrap(),
))
.init();
```
## License
This project is licensed under the MIT License - see the [LICENSE](LICENSE) file for details.