# ๐ crypsol_logger
Structured and production-grade logger for your applications, with seamless AWS CloudWatch support and local fallback logging.
---
## ๐ฅ Features
- โ
Structured JSON logging
- โ
Batch and compress logs before sending
- โ
AWS CloudWatch integration
- โ
Automatic fallback to local stdout logging
- โ
Thread-safe, high-performance design
- โ
Minimal configuration required
---
## ๐ฆ Installation
Add the crate to your `Cargo.toml`:
```toml
[dependencies]
crypsol_logger = "0.2.2"
```
The `Level` enum is re-exported, so there's no need to add the `log` crate separately.
---
## ๐ Setup & Usage
You can use the log! macro to generate logs. This macro will automatically check the environment variable and accordingly send logs to CloudWatch or print them to the console.
```rust
log!(Level::Info, "This is an info message");
log!(Level::Error, "This is an error message");
log!(Level::Debug, "Debugging information");
```
You can also attach structured key-value fields to any log call. Just add a `;` after the message and pass your fields as `"key" => value` pairs:
```rust
log!(Level::Info, "User {} logged in", user_id; "ip" => ip_addr, "role" => role);
log!(Level::Error, "payment failed"; "order_id" => order_id, "amount" => amount);
```
This produces a JSON message like:
```json
{"message":"User 42 logged in","ip":"10.0.0.1","role":"admin"}
```
Plain string logs still work exactly the same โ the structured fields are fully optional and backward-compatible.
To log in a custom stream (other than info, error and debug) you can use log_custom macro
```rust
log_custom!(Level::Info, "Custom Stream Name", "This is the message and variable {variable}");
log_custom!(Level::Info, "Payments", "charge created"; "tx" => tx_hash, "total" => total);
```
โ
That's it! Logs are automatically captured and either sent to AWS CloudWatch or printed locally.
---
## ๐งช Environment Variables
| `CLOUDWATCH_AWS_ACCESS_KEY` | Your AWS Access Key |
| `CLOUDWATCH_AWS_SECRET_KEY` | Your AWS Secret Key |
| `CLOUDWATCH_AWS_REGION` | AWS Region (default: `us-east-1`) |
| `AWS_LOG_GROUP` | CloudWatch log group name |
| `LOG_TO_CLOUDWATCH` | Set this to false if you want to disable logging to CloudWatch (default is false) |
| `LOG_TO_FILE` | Set this to true to write logs to local files |
| `LOG_FILE_DIR` | Directory path for local logs (default: `logs`) |
| `LOG_BATCH_SIZE` | Max logs per batch (default: 10) |
| `BATCH_TIMEOUT` | Max time to wait for putting a log event |
| `LOG_RETENTION_DAYS` | Days to keep log files on disk (default: `30`) |
| `LOG_RETENTION_SIZE_MB` | Max total size of logs before old files are deleted (default: `512`) |
| `LOG_DELETE_BATCH_MB` | Amount of oldest logs removed when the size limit is hit (default: `100`) |
| `LOG_SHOW_LOCATION` | Include file path and line number in logs (default: `false`) |
---
## ๐ License
MIT ยฉ 2025 [Crypsol](https://crypsol.tech/)
---
## ๐ง Also Available in Python!
A **Python version** of this logger, which is also easily integratable with FastAPI, Flask, and other WSGI/ASGI frameworks:
๐ [cloudwatchpy โ Python Logger for AWS CloudWatch](https://github.com/Irfan-Ahmad-byte/cloudwatchpy)