Metadata-Version: 2.4
Name: loglane
Version: 0.1.0
Summary: Readable, coloured, timestamped logs for Python's standard logging - one call, no dependencies.
Keywords: logging,logger,color,colour,formatter,json,uvicorn,nestjs
Author: AppMeSoft Private Limited
License-Expression: MIT
License-File: LICENSE
Classifier: Development Status :: 4 - Beta
Classifier: Intended Audience :: Developers
Classifier: Operating System :: OS Independent
Classifier: Programming Language :: Python :: 3
Classifier: Programming Language :: Python :: 3 :: Only
Classifier: Programming Language :: Python :: 3.11
Classifier: Programming Language :: Python :: 3.12
Classifier: Programming Language :: Python :: 3.13
Classifier: Programming Language :: Python :: 3.14
Classifier: Topic :: System :: Logging
Classifier: Typing :: Typed
Requires-Python: >=3.11
Project-URL: Homepage, https://github.com/ashishpatel546/loglane
Project-URL: Source, https://github.com/ashishpatel546/loglane
Project-URL: Issues, https://github.com/ashishpatel546/loglane/issues
Project-URL: Changelog, https://github.com/ashishpatel546/loglane/blob/main/CHANGELOG.md
Description-Content-Type: text/markdown

# loglane

[![PyPI](https://img.shields.io/pypi/v/loglane.svg)](https://pypi.org/project/loglane/)
[![Python](https://img.shields.io/pypi/pyversions/loglane.svg)](https://pypi.org/project/loglane/)
[![License: MIT](https://img.shields.io/badge/license-MIT-blue.svg)](https://opensource.org/licenses/MIT)

**Readable, coloured, timestamped logs for Python, in one line of setup.**

loglane gives Python's built-in `logging` the clean, colour-coded look you know from
NestJS. It also masks passwords and tokens before they reach your logs, and can switch
to JSON for log collectors. It has **zero dependencies** and works with FastAPI,
uvicorn, Django, Flask, Celery, scripts, or anything else that uses the standard
`logging` module.

```
[orders-api] 41237  - 2026-11-16 11:34:01.154 IST     LOG [uvicorn] Uvicorn running on http://0.0.0.0:8000
[orders-api] 41237  - 2026-11-16 11:34:02.410 IST     LOG [payments] charged order 42
[orders-api] 41237  - 2026-11-16 11:34:02.411 IST     LOG [http] 10.0.0.7:51122 - "POST /orders HTTP/1.1" 201
[orders-api] 41237  - 2026-11-16 11:34:05.010 IST    WARN [stock] only 2 left of SKU-9
[orders-api] 41237  - 2026-11-16 11:34:07.511 IST   ERROR [db] connect failed for postgresql://app:***@db:5432/shop
```

On a terminal, each level has its own colour: green `LOG`, yellow `WARN`, red `ERROR`,
bold red `FATAL` and magenta `DEBUG`. The source name is shown in yellow. Written to a
file, a pipe or `docker logs`, the same lines come out without colour codes.

## Features

- **One call.** `setup_logging("my-app")` and you're done. No classes to subclass, no
  config files.
- **NestJS-style lines.** Each line shows the app name, process id, a millisecond
  timestamp **with its timezone**, the level and a short source name.
- **Smart colour.** Colour appears only on a real terminal. `NO_COLOR` and `FORCE_COLOR`
  are respected.
- **Secrets masked.** Passwords, tokens, API keys, `Bearer …` headers and `user:pass@`
  in URLs are masked in messages *and* tracebacks. You can add your own patterns.
- **JSON mode.** One JSON object per line for Loki, ELK, CloudWatch or Datadog. Fields
  passed with `extra={...}` are included.
- **Framework logs included.** uvicorn's server, access and error logs use the same
  format.
- **Configure from the environment.** `LOG_LEVEL`, `LOG_FORMAT`, `LOG_COLOR`,
  `LOG_TIMEZONE`.
- **Zero dependencies.** Standard library only, fully typed (`py.typed`), Python 3.11+.

## Install

```bash
pip install loglane
# or
uv add loglane
# or
poetry add loglane
```

## Quick start

```python
import logging
from loglane import setup_logging

setup_logging("orders-api", timezone="Asia/Kolkata")

log = logging.getLogger("orders.payments")
log.info("charged order %s", 42)
log.warning("card expires soon")

try:
    1 / 0
except ZeroDivisionError:
    log.exception("refund failed")   # the traceback is kept, dimmed under the line
```

## FastAPI

Call `setup_logging` once, at the top of the module that creates the app.

```python
# main.py
import logging
from fastapi import FastAPI
from loglane import setup_logging

setup_logging("shop-api")
log = logging.getLogger("shop.orders")
app = FastAPI()

@app.get("/orders/{n}")
def get_order(n: int):
    log.info("order %s read", n)
    return {"n": n}
```

```bash
uvicorn main:app
```

uvicorn's startup lines, the access log (shown as `[http]`) and unhandled errors all
come out in the loglane format. If you start uvicorn **from code**, pass
`log_config=None` so uvicorn doesn't replace your setup:

```python
uvicorn.run(app, host="0.0.0.0", port=8000, log_config=None)
```

## Configure from environment variables

```python
from loglane import setup_from_env

setup_from_env("orders-api")
```

| Variable | Values | Default |
|---|---|---|
| `LOG_LEVEL` | `DEBUG`, `INFO`, `WARNING`, `ERROR`, `CRITICAL` | `INFO` |
| `LOG_FORMAT` | `pretty`, `plain`, `json` | `pretty` |
| `LOG_COLOR` | `auto`, `always`, `never` | `auto` |
| `LOG_TIMEZONE` | `UTC`, `Asia/Kolkata`, `Europe/London`, ... or `local` | `local` |
| `LOG_REDACT` | `true` / `false` | `true` |

Pass `prefix="MYAPP_LOG_"` to read different variable names.

## JSON output

```python
setup_logging("orders-api", fmt="json", timezone="UTC")
logging.getLogger("orders.db").error("query slow", extra={"ms": 812, "table": "orders"})
```

```json
{"time": "2026-11-16T06:04:01.154+00:00", "level": "error", "app": "orders-api", "pid": 41237, "logger": "orders.db", "message": "query slow", "ms": 812, "table": "orders"}
```

## Secret masking

These are masked by default, in the message and in any traceback:

| Input | Logged as |
|---|---|
| `password=hunter2` | `password=***` |
| `{"api_key": "abc123"}` | `{"api_key": "***"}` |
| `Authorization: Bearer eyJhbGciOi...` | `Authorization: Bearer ***` |
| `postgresql://app:s3cret@db/shop` | `postgresql://app:***@db/shop` |
| `sk-ABCDEFGHIJKLMNOPQRST` | `sk-***` |

Add your own patterns. Group 1 is masked, or the whole match if there is no group:

```python
setup_logging("shop-api", redact_patterns=[r"card (\d{12,19})"])
```

Masking is a safety net. Avoid logging secrets in the first place.

## All options

```python
setup_logging(
    app,                      # name shown at the start of every line
    level="INFO",             # name or number
    fmt="pretty",             # "pretty" | "plain" (never coloured) | "json"
    color="auto",             # "auto" | "always" | "never"
    timezone=None,            # IANA name, a tzinfo, or None / "local"
    stream=None,              # defaults to sys.stderr
    redact=True,              # mask secrets
    redact_patterns=(),       # extra regexes to mask
    short_names=True,         # "orders.payments" shown as [payments]
    capture=("uvicorn", "uvicorn.error", "uvicorn.access"),  # loggers routed through loglane
    levels=None,              # per-logger levels, e.g. {"httpx": "WARNING"}
)
```

`setup_logging` replaces the root logger's handlers and returns the new handler.
Calling it again is safe. The formatters (`PrettyFormatter`, `JsonFormatter`) and
`Redactor` are exported too, if you want to attach them to your own handlers.

## Level names

| Python | loglane | Colour |
|---|---|---|
| `DEBUG` | `DEBUG` | magenta |
| `INFO` | `LOG` | green |
| `WARNING` | `WARN` | yellow |
| `ERROR` | `ERROR` | red |
| `CRITICAL` | `FATAL` | bold red |

## License

MIT © AppMeSoft Private Limited
