Skip to content

Commit

Permalink
Document usage of library
Browse files Browse the repository at this point in the history
- Add a better summary
- Add example usage
- Add example output
  • Loading branch information
felddy committed Oct 21, 2024
1 parent d35a23e commit 7b51164
Showing 1 changed file with 64 additions and 3 deletions.
67 changes: 64 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,72 @@
[![Known Vulnerabilities](https://snyk.io/test/github/cisagov/cyhy-logging/develop/badge.svg)](https://snyk.io/test/github/cisagov/cyhy-logging)

This is a Python package that provides a standard logging configuration which is
used by the Cyber Hygiene system.
used by the Cyber Hygiene (CyHy) system.

## Usage ##
The logging setup uses the
[`RichHandler`](https://rich.readthedocs.io/en/stable/logging.html) to provide
visually appealing and informative log outputs, including enhanced tracebacks
when debugging. Additionally, default filters like the
[`RedactPasswordFilter`](src/cyhy_logging/log_filters.py) are applied to all log
handlers to automatically filter and redact sensitive information.

<!-- TODO: add usage -->
To ensure that all logs across different parts of the CyHy system are properly
grouped under a single root, the `CYHY_ROOT_LOGGER` constant is used. By using
this constant when configuring loggers (`CYHY_ROOT_LOGGER` as the root logger),
all log messages are consistently organized under the same logging namespace,
which simplifies log management and makes it easier to locate all related logs.
Additionally, using `CYHY_ROOT_LOGGER` allows the verbosity of CyHy logs to be
increased independently without affecting the verbosity of other packages that
use the logging system.

## Example Usage ##

```python
from cyhy_logging import CYHY_ROOT_LOGGER, setup_logging
import logging

# Set up logging
logger = logging.getLogger(f"{CYHY_ROOT_LOGGER}.{__name__}")
setup_logging("DEBUG")

# Use the logger
logger.debug("This is a debug message")
logger.info("This is an info message")
logger.warning("This is a warning message")
logger.error("This is an error message")
logger.critical("This is a critical message")

# The logger will redact sensitive information by default
logger.info("Logging into database at https://admin:[email protected]/secret")

# Raise an exception to demonstrate the enhanced traceback
raise Exception("This is an exception")

# Shutdown logging
logging.shutdown()
```

## Example Output ##

```console
[13:37:28] DEBUG This is a debug message <python-input-0>:9
INFO This is an info message <python-input-0>:10
WARNING This is a warning message <python-input-0>:11
ERROR This is an error message <python-input-0>:12
CRITICAL This is a critical message <python-input-0>:13
INFO Logging into database at <python-input-0>:16
https://admin:****@example.com/secret
╭───────────────────── Traceback (most recent call last) ──────────────────────╮
│ in <module>:18 │
│ ╭───────────────────────────────── locals ─────────────────────────────────╮ │
│ │ CYHY_ROOT_LOGGER = 'cyhy' │ │
│ │ logger = <Logger cyhy.__main__ (DEBUG)> │ │
│ │ logging = <module 'logging' from │ │
│ │ '/Users/lemmy/.pyenv/versions/3.13.0/lib/python3.13/… │ │
│ ╰──────────────────────────────────────────────────────────────────────────╯ │
╰──────────────────────────────────────────────────────────────────────────────╯
Exception: This is an exception
```

## Contributing ##

Expand Down

0 comments on commit 7b51164

Please sign in to comment.