Statistical log anomaly detection in pure Rust, with zero dependencies. Part of the AI Security Lab collection.
anomalyd learns what "normal" looks like in a log file, then flags lines that
deviate from that baseline. It extracts six cheap features per line — length,
digit ratio, uppercase ratio, symbol ratio, token count and byte entropy — fits
a mean and standard deviation, and scores each line by its average absolute
z-score.
No crates, no model download, no network. One static binary.
- Unsupervised — no labels needed; train on the log itself.
- Explainable-ish — features are named, and the score is a mean z-score you can reason about.
- Fast — one pass to fit, one pass to score, O(lines × 6).
- Safe — blank lines are ignored; feature deviations have a floor so constant features cannot explode.
- CI-ready — JSON output and exit status 1 when anomalies are found.
- Tested — unit tests in the library plus integration tests for scoring and line numbers.
cargo install --path .Or:
git clone https://github.com/Sakibpedia/anomalyd.git
cd anomalyd
cargo build --release# Detect anomalies in a log file (train on the same file)
anomalyd application.log
# Read from stdin
journalctl -u sshd --no-pager | anomalyd --threshold 3.0
# Top 10 most unusual lines as JSON
anomalyd --json --top 10 auth.log > anomalies.jsonExit status is 1 when at least one anomaly is reported, 0 when clean.
| Option | Default | Meaning |
|---|---|---|
--threshold N |
2.5 |
Score cutoff (higher = stricter) |
--top N |
all | Keep only the N highest-scoring lines |
--json |
off | Emit JSON objects with line, score, text |
--help |
— | Show help |
use anomalyd::{AnomalyModel, Detector};
let baseline = ["INFO request ok", "INFO health check ok"];
let model = AnomalyModel::fit(&baseline)?;
let detector = Detector::new(model, 2.5)?;
for anomaly in detector.detect(["INFO request ok", "!!!QQQ999$$$888"]) {
println!("line {}: {:.2}", anomaly.line, anomaly.score);
}features(line) -> [f64; 6]
z_i = |feature_i - mean_i| / max(stddev_i, 0.05)
score = mean(z_0 ... z_5)
A score of 2.5 means the line is, on average, 2.5 standard deviations from the
baseline across the six features. The 0.05 floor keeps constant features from
producing infinite scores.
Training on the file you scan means anomalies that dominate the file will raise the baseline and hide themselves. For real deployments, train on a known-clean window and score a different window. This is a triage aid, not a detection guarantee.
cargo testThe suite covers feature extraction, model fitting, scoring and line numbering.
anomalyd/
├── src/features.rs feature extraction + unit tests
├── src/model.rs fitting, scoring, detection + unit tests
├── src/main.rs CLI
└── tests/integration.rs end-to-end scoring tests
MIT © S M Nazmuz Sakib (2026).