Metadata-Version: 2.1
Name: logfy
Version: 1.0.1
Summary: Pretty printer
Author: lynariksh
Author-email: lynariksh@atomicmail.io
License: MIT
Description-Content-Type: text/markdown
Requires-Dist: colorlog>=3.0.0
Requires-Dist: pycryptodome>=3.0.0
Requires-Dist: colorama>=0.4.0

Perfect for production services, fintech, compliance-heavy apps, or anywhere you want readable logs in the terminal but encrypted storage on disk.

```python
from logfy import log, enable_secure_logging

# Turn on secure logging (creates key automatically if missing)
enable_secure_logging("myapp_secure.log")

log.info("Application started")
log.success("User authenticated")
log.warning("Rate limit approaching")
log.error("Payment gateway timeout")
log.fail("Transaction rejected – insufficient funds")
```

## Installation

```bash
pip install logfy
```

Requirements:
- `colorlog`
- `pycryptodome`
- `colorama`

## Quick Start

```python
from logfy import log, enable_secure_logging, decrypt_secure_log

# 1. Enable secure logging (only once, usually at app startup)
enable_secure_logging(
    filename="myapp_secure.log",
    key_file=".myapp_key",          # optional – defaults to .logfy_key
    correlation_id="myapp-prod"
)

# 2. Use the global logger (or create your own)
log.debug("Debugging session started")
log.info("Processing order #12345")
log.success("Payment confirmed")
log.fail("Card declined")

# 3. Later – decrypt the log file
for line in decrypt_secure_log("myapp_secure.log"):
    print(line)
```

Output in console:
```
14:28:15 myapp    INFO     │ Processing order #12345
14:28:16 myapp SUCCESS   │ Payment confirmed
14:28:17 myapp    FAIL     │ Card declined
```

The file `myapp_secure.log` contains only encrypted + signed entries.

## Features

| Feature                          | Description                                                                 |
|----------------------------------|-----------------------------------------------------------------------------|
| Gorgeous colored console         | Using `colorlog` with custom SUCCESS/FAIL levels                            |
| Encrypted file logs              | RC4 encryption with SHA-256 checksum verification                           |
| Automatic key generation         | First run creates a secure key in `.logfy_key` (or custom path)             |
| Smart key auto-detection         | `decrypt_secure_log()` finds the key automatically                          |
| Global `log` auto-upgrade        | After `enable_secure_logging()`, `logfy.log` writes encrypted entries       |
| `.success()` & `.fail()` methods | Clean semantic logging without extra level numbers                          |
| Async file writer                | Zero performance impact on your main thread                                 |
| Tamper detection                 | Any modified line fails checksum verification on decryption                 |
| Context manager support          | `with enable_secure_logging(...) as log:` auto-closes handlers on exit      |
| CLI decrypt tool                 | `python -m logfy decrypt app.log` with filter flags                         |
| Decrypt filtering                | Filter decrypted output by level, correlation ID, or time range             |
| Automatic context injection      | *(coming soon)* Attach request/user/session IDs via `contextvars` automatically |
| Environment-aware defaults       | *(coming soon)* Auto-detect log level from `LOG_LEVEL` env var              |

## Decrypting Logs

```python
from logfy import decrypt_secure_log

# Works even without passing the key explicitly
for line in decrypt_secure_log("myapp_secure.log"):
    print(line)

# Filter by level, correlation ID, or time range
for line in decrypt_secure_log(
    "myapp_secure.log",
    level="ERROR",
    correlation_id="myapp-prod",
    start_time="2026-03-26 09:00:00",
    end_time="2026-03-26 18:00:00",
):
    print(line)
```

## CLI Tool

logfy ships with a built-in command-line tool for decrypting and inspecting encrypted log files — no Python code required.

**Basic usage:**

```bash
python -m logfy decrypt myapp_secure.log
```

**With an explicit key file:**

```bash
python -m logfy decrypt myapp_secure.log --key-file .myapp_key
```

**Filter the output:**

```bash
# Only show ERROR lines
python -m logfy decrypt myapp_secure.log --level ERROR

# Only show lines from a specific service/request
python -m logfy decrypt myapp_secure.log --correlation-id myapp-prod

# Show lines within a time window
python -m logfy decrypt myapp_secure.log --start "2026-03-26 09:00:00" --end "2026-03-26 18:00:00"

# Combine filters
python -m logfy decrypt myapp_secure.log --level WARNING --correlation-id myapp-prod
```

**All available flags:**

| Flag                     | Short | Description                                      |
|--------------------------|-------|--------------------------------------------------|
| `--key-file PATH`        | `-k`  | Path to the encryption key file                  |
| `--key BASE64`           |       | Raw base64-encoded encryption key                |
| `--level LEVEL`          | `-l`  | Filter by log level (e.g. `ERROR`, `WARNING`)    |
| `--correlation-id ID`    | `-c`  | Filter by correlation ID                         |
| `--start DATETIME`       |       | Show lines at or after `YYYY-MM-DD HH:MM:SS`     |
| `--end DATETIME`         |       | Show lines at or before `YYYY-MM-DD HH:MM:SS`    |

## Context Manager

Use `enable_secure_logging()` as a context manager to ensure the file handler is always flushed and closed cleanly:

```python
from logfy import enable_secure_logging

with enable_secure_logging("myapp_secure.log") as log:
    log.info("Processing started")
    log.success("Done")
# handler flushed and closed automatically here
```

## Customizing

```python
from logfy import get_logger, enable_secure_logging

# Custom logger name
logger = get_logger("worker-07")
logger.info("Starting background job")

# Custom secure logger (won't touch the global `log`)
secure = enable_secure_logging(
    filename="worker07_secure.log",
    key_file=".worker07_key",
    log_level="INFO"
)
secure.critical("Disk almost full!")
```

## Security Notes

- The encryption key is never logged or exposed.
- Keys are stored as raw base64 in a hidden file (`.logfy_key` by default).
- Keep the key file safe! Anyone with the key can read the logs.
- For extra safety, store the key outside the repository (e.g. secrets manager) and pass it explicitly.

## Coming Soon

- **Automatic context injection** — use `contextvars` to automatically attach request, user, or session IDs to every log entry without passing them manually on each call.
- **Environment-aware defaults** — automatically detect the log level from a `LOG_LEVEL` environment variable, making it easy to switch between `DEBUG` in development and `INFO` in production without code changes.


## Contributing

Bug reports, feature requests, and pull requests are very welcome!

## License

MIT © 2025
