From 4b123ddf9881f37d257a385fbbae0821deb6dba6 Mon Sep 17 00:00:00 2001 From: Will McGugan Date: Sun, 14 Jun 2020 16:35:56 +0100 Subject: [PATCH] rich handler tweak --- CHANGELOG.md | 4 ++-- pyproject.toml | 2 +- rich/logging.py | 21 ++++++++++++++++----- 3 files changed, 19 insertions(+), 8 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 351f40d6..a6033ef3 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,11 +5,11 @@ All notable changes to this project will be documented in this file. The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). -## [2.2.2] - Unreleased +## [2.2.2] - 2020-06-14 ### Changed -- Extra paramters added to RichLogging +- Exposed RichHandler highlighter as a class var ## [2.2.1] - 2020-06-14 diff --git a/pyproject.toml b/pyproject.toml index 0c5e3436..e251bfe6 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -2,7 +2,7 @@ name = "rich" homepage = "https://github.com/willmcgugan/rich" documentation = "https://rich.readthedocs.io/en/latest/" -version = "2.2.1" +version = "2.2.2" description = "Render rich text, tables, progress bars, syntax highlighting, markdown and more to the terminal" authors = ["Will McGugan "] license = "MIT" diff --git a/rich/logging.py b/rich/logging.py index cc5c52fb..60b2b19e 100644 --- a/rich/logging.py +++ b/rich/logging.py @@ -2,12 +2,12 @@ import logging from datetime import datetime from logging import Handler, LogRecord from pathlib import Path -from typing import Optional +from typing import ClassVar, List, Optional, Type from . import get_console from rich._log_render import LogRender from rich.console import Console -from rich.highlighter import Highlighter, ReprHighlighter +from rich.highlighter import Highlighter, RegexHighlighter, ReprHighlighter from rich.markup import render from rich.text import Text @@ -20,11 +20,21 @@ class RichHandler(Handler): level (int, optional): Log level. Defaults to logging.NOTSET. console (:class:`~rich.console.Console`, optional): Optional console instance to write logs. Default will create a new console writing to stderr. + enable_link_path (bool, optional): Enable terminal link of path column to file. Defaults to True. """ - KEYWORDS = ["GET", "POST", "HEAD", "PUT", "DELETE", "OPTIONS", "TRACE", "PATCH"] - HIGHLIGHTER_CLASS = ReprHighlighter + KEYWORDS: ClassVar[Optional[List[str]]] = [ + "GET", + "POST", + "HEAD", + "PUT", + "DELETE", + "OPTIONS", + "TRACE", + "PATCH", + ] + HIGHLIGHTER_CLASS: ClassVar[Type[Highlighter]] = ReprHighlighter def __init__( self, @@ -50,9 +60,10 @@ class RichHandler(Handler): level = Text() level.append(record.levelname, log_style) message_text = Text(message) - message_text.highlight_words(self.KEYWORDS, "logging.keyword") if self.highlighter: message_text = self.highlighter(message_text) + if self.KEYWORDS: + message_text.highlight_words(self.KEYWORDS, "logging.keyword") self.console.print( self._log_render(