Skip to content

Instantly share code, notes, and snippets.

@sempervent
Last active September 7, 2022 14:24
Show Gist options
  • Save sempervent/9c68aafc8df6b3f4b99ce9e1e1c24d2e to your computer and use it in GitHub Desktop.
Save sempervent/9c68aafc8df6b3f4b99ce9e1e1c24d2e to your computer and use it in GitHub Desktop.
Easy creation of beautiful Rich logging in Python
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# standard python imports
import logging
from rich.logging import RichHandler
from rich.traceback import install
import os
install()
def create_logger():
"""Create a logger for use in all cases."""
LOGLEVEL = os.environ.get('LOGLEVEL', 'INFO').upper()
rich_handler = RichHandler(rich_tracebacks=True, markup=True)
logging.basicConfig(level=LOGLEVEL, format='%(message)s',
datefmt="[%Y/%m/%d %H:%M;%S]",
handlers=[rich_handler])
return logging.getLogger('rich')
@sempervent
Copy link
Author

sempervent commented Jul 22, 2021

Usage:

from logz import create_logger
logger = create_logger()
logger.info('Rich logging created')
raise RuntimeError('And it has beautiful tracebacks!')

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment