Metadata-Version: 2.4
Name: vcti-logging
Version: 1.3.1
Summary: Configurable logging with custom levels and color support for Python
Author: Visual Collaboration Technologies Inc.
License-Expression: LicenseRef-Proprietary
Project-URL: Repository, https://github.com/vcollab/vcti-python-logging
Project-URL: Changelog, https://github.com/vcollab/vcti-python-logging/blob/main/CHANGELOG.md
Classifier: Operating System :: OS Independent
Classifier: Programming Language :: Python :: 3.12
Classifier: Programming Language :: Python :: 3.13
Classifier: Programming Language :: Python :: 3.14
Requires-Python: <3.15,>=3.12
Description-Content-Type: text/markdown
License-File: LICENSE
Requires-Dist: colorlog
Provides-Extra: test
Requires-Dist: pytest; extra == "test"
Requires-Dist: pytest-cov; extra == "test"
Provides-Extra: lint
Requires-Dist: ruff; extra == "lint"
Provides-Extra: typecheck
Requires-Dist: mypy; extra == "typecheck"
Provides-Extra: bench
Requires-Dist: tabulate; extra == "bench"
Dynamic: license-file

# vcti-logging

Configurable logging with custom levels and color support for Python.

## Overview

`vcti-logging` gives every script and service the same logging setup:
the same levels, the same formats, the same behavior. It configures one
package logger, `vcti`, from a list of handler descriptions — console,
file, or both — each with its own level and message format, and replaces
that configuration wholesale whenever it is called again. It adds two
levels to Python's standard set, `TRACE` (5) and `VERBOSE` (15), with
matching `logger.trace()` and `logger.verbose()` methods, optional colored
console output, and named format presets so callers rarely write a
`%`-style format string.

---

## Installation

```bash
pip install vcti-logging
```

### In `requirements.txt`

```
vcti-logging>=1.3.1
```

### In `pyproject.toml` dependencies

```toml
dependencies = [
    "vcti-logging>=1.3.1",
]
```

---

## Quick Start

```python
from pathlib import Path

from vcti.logging import ConsoleLogConfig, FileLogConfig, logger, setup_logging

# Console logging with color
setup_logging([
    ConsoleLogConfig(log_level="trace", enable_color_log=True),
])

# Standard levels
logger.info("Application started")
logger.warning("Low disk space")

# Custom levels
logger.trace("Entering load() with path=%s", "model.h5")
logger.verbose("Processing item %d of %d", 3, 10)

# Reconfigure: console at INFO, everything from DEBUG up to a file
setup_logging([
    ConsoleLogConfig(log_level="info", enable_color_log=True),
    FileLogConfig(log_level="debug", file_path=Path("app.log")),
])
```

The same setup from keyword arguments, for callers that map command-line
options straight through:

```python
from vcti.logging import create_logging_config

setup_logging(create_logging_config(log_level="debug", log_file_path=Path("app.log")))
```

---

## Log Levels

| Name | Value | Purpose |
|------|-------|---------|
| `"notset"` | 0 | Pass every record |
| `"trace"` | 5 | Step-by-step execution detail |
| `"debug"` | 10 | Debug information |
| `"verbose"` | 15 | Detailed, but less noisy than DEBUG |
| `"info"` | 20 | General information (default) |
| `"warning"` | 30 | Warning conditions |
| `"error"` | 40 | Error conditions |
| `"fatal"` | 50 | Fatal errors |

Configuration takes the names, case-insensitively. `LogLevel` is the
matching `IntEnum`; `decode_log_level()` and `encode_log_level()` convert
between the two.

---

## Configuration

### Message format presets

| Preset | Shows | Use case |
|--------|-------|----------|
| `"minimal"` | message | CLI tools, clean output |
| `"standard"` | level + message | General use (default) |
| `"detailed"` | level + file + line + message | Debugging |

Each preset has a plain and a color variant; the right one is chosen from
`enable_color_log`. Any other string is used as a raw format:

```python
ConsoleLogConfig(log_level="debug", enable_color_log=True, message_format="detailed")
ConsoleLogConfig(message_format="%(asctime)s - %(name)s - %(levelname)s - %(message)s")
```

### File logging

Files are written as UTF-8 and opened in append mode; `mode="w"`
overwrites on each `setup_logging()` call. The parent directory must exist.

```python
FileLogConfig(file_path=Path("app.log"), log_level="info", mode="w")
```

Console and file handlers are independent — each has its own level and
preset, and file output never carries color codes:

```python
setup_logging([
    ConsoleLogConfig(log_level="info", enable_color_log=True, message_format="standard"),
    FileLogConfig(log_level="debug", file_path=Path("app.log"), message_format="detailed"),
])
```

### Child loggers

Modules log through children of `vcti`; their records reach the handlers
configured above, with no setup of their own. `get_logger()` returns one,
typed so that `trace()` and `verbose()` type-check:

```python
from vcti.logging import get_logger

app_logger = get_logger("myapp")  # the "vcti.myapp" logger
app_logger.verbose("Application-specific message")
```

A name already under `vcti` is kept as is, so `get_logger(__name__)`
works inside a vcti package.

### Temporary configuration and tests

`setup_logging()` is for an application's entry point: it changes the
process-wide `vcti` logger until the next call. Where a configuration must
be undone — a test, a long-running host — use `logging_configured()`,
which restores the previous handlers, level and propagation when the block
exits, even if it raises:

```python
from vcti.logging import logging_configured

with logging_configured([FileLogConfig(file_path=Path("run.log"), log_level="debug")]):
    logger.debug("written to run.log")
# the previous configuration is back, and run.log is closed
```

To log somewhere *extra* for a while — say, a log file for one job in a
host that keeps its own console — use `logging_added()`. It attaches the
handlers beside whatever is configured, replaces nothing, leaves
propagation alone, and removes and closes them when the block exits:

```python
from vcti.logging import logging_added

with logging_added([FileLogConfig(file_path=Path("job.log"), log_level="debug")]):
    logger.debug("in job.log only")
    logger.info("in job.log and wherever the host already logs")
```

If the file asks for records below what the logger lets through, the
level is lowered for the block, and the handlers already in place are
filtered so they still receive only what they did before. A
`setup_logging()` or `logging_configured()` call inside the block leaves
the added handlers working.

`reset_logging()` returns the logger to its import-time state: no
handlers from this package, level `NOTSET`, propagation on. A test suite
can call it after every test:

```python
import pytest
from vcti.logging import reset_logging

@pytest.fixture(autouse=True)
def reset_vcti_logging():
    yield
    reset_logging()
```

All four manage only the handlers this package created; a handler
attached by other code, such as pytest's log capture, is left in place.

### Pydantic models

`LogLevelCoder` bundles the level type, default and valid names for a
model field (requires `pydantic`, which this package does not depend on):

```py
from pydantic import BaseModel, Field
from vcti.logging import LogLevelCoder

class AppConfig(BaseModel):
    log_level: LogLevelCoder.Literal = Field(
        default=LogLevelCoder.default,
        description=f"Supported: {'/'.join(LogLevelCoder.list)}",
    )
```

---

## Public API

| Symbol | Purpose |
|--------|---------|
| `setup_logging(configs, propagate=False)` | Replace the `vcti` logger's handlers |
| `logging_configured(configs, propagate=False)` | Context manager: configure for a block, then restore |
| `logging_added(configs)` | Context manager: add handlers beside the existing ones for a block |
| `reset_logging()` | Return the `vcti` logger to its import-time state |
| `ConsoleLogConfig` / `FileLogConfig` | Describe one console or file handler |
| `create_logging_config(...)` | Build the config list from keyword arguments |
| `logger` / `get_logger(name=None)` | The package logger, named `"vcti"` / it or a child, typed |
| `CustomLevelLogger` | Logger class with `trace()` and `verbose()` |
| `LogLevel` / `LogLevelStr` | Level enum / `Literal` of level names |
| `decode_log_level(name)` / `encode_log_level(level)` | Name ↔ `LogLevel` |
| `DEFAULT_LOG_LEVEL_STR` / `LOG_LEVEL_VALUES` | `"info"` / every level name |
| `LogLevelCoder` | Level type, default, names and conversion in one object |
| `MESSAGE_FORMAT_PRESETS` / `MessageFormatPreset` | Preset formats / preset names |
| `resolve_message_format(fmt, color=False)` | Preset name or raw format → format string |

---

## Dependencies

- [`colorlog`](https://pypi.org/project/colorlog/) — colored console
  output.

---

## Documentation

| If you want to… | Read |
|---|---|
| Get started using the package | Quick Start above |
| Understand the design decisions and their trade-offs | [docs/design.md](docs/design.md) |
| Navigate and understand the source | [docs/source-guide.md](docs/source-guide.md) |
| Contribute, run the checks or the benchmarks | [CONTRIBUTING.md](CONTRIBUTING.md) |
