fast_log
A log implementation for extreme speed, using Crossbeam/channel ,once Batch write logs,fast log date, Appender architecture, appender per thread
- High performance,Low overhead, logs auto merge, Full APPEND mode file writing
- Built-in
ZIP,LZ4compression - Support use
log::logger().flush()method wait to flush disk - Support custom file(impl Trait)
- Support rolling log(
ByDate,BySize,ByDuration) - Support Keep log(
All,KeepTime,KeepNum) Delete old logs,Prevent logs from occupying the disk - uses
#![forbid(unsafe_code)]100% Safe Rust.
Architecture
log data
│
▼
┌────────────────────┐
│ main channel │◀──── user threads call log!() macro
│ (crossbeam) │
└─────────┬──────────┘
│
│ (1 dispatcher thread)
▼
┌────────────────────┐
│ dispatcher thread │─────┬─────┬─────┬──────┬
│ - recv all records │ │ │ │ │
│ - batch merge │ │ │ │ │
│ - broadcast (Arc) │ │ │ │ │
└────────────────────┘ │ │ │ │
│ │ │ │ │
▼ ▼ ▼ ▼ ▼
┌─────────┐ ┌─────────┬─────────┬─────────┐
│channel 1│ │channel 2│ │channel N│
└────┬────┘ └────┬────┘ └────┬────┘
│ │ │
▼ ▼ ▼
┌────────────┐ ┌────────────┐ ┌────────────┐
│ background │ │ background │ │ background │
│ thread 1 │ │ thread 2 │ │ thread N │
│ do_logs( │ │ do_logs( │ │ do_logs( │
│ batch) │ │ batch) │ │ batch) │
└─────┬──────┘ └─────┬──────┘ └─────┬──────┘
│ │ │
▼ ▼ ▼
┌────────────┐ ┌────────────┐ ┌────────────┐
│ appender1 │ │ appender2 │ │ appenderN │
└────────────┘ └────────────┘ └────────────┘
-
How fast is?
-
no flush(chan_len=1000000) benches/log.rs
//MACOS(Apple M1MAX-32GB)
test bench_log ... bench: 85 ns/iter (+/- 1,800)
- all log flush into file(chan_len=1000000) example/bench_test_file.rs
//MACOS(Apple M1MAX-32GB)
test bench_log ... bench: 323 ns/iter (+/- 0)
- how to use?
= "0.4"
= { = "1.7" }
or enable zip/lz4/gzip Compression library
= "0.4"
# "lz4","zip","gzip"
= { = "1.7", = ["lz4", "zip", "gzip"] }
Performance optimization(important)
- use
chan_len(Some(100000))Preallocating channel memory reduces the overhead of memory allocation,for example:
use ;
Use Log(Console)
use ;
Use Log(Console Print)
use ;
Use Log(File)
use ;
use ;
Split Log(ByLogDate)
use Config;
use ;
use sleep;
use Duration;
use LogPacker;
Split Log(ByLogSize)
use Config;
use LogSize;
use ;
use LogPacker;
Custom Log(impl do_log method)
use ;
use Config;
use DateTime;
use Level;