Skip to content

Commit d4a60fe

Browse files
committed
rich.logging
1 parent 505b45c commit d4a60fe

File tree

7 files changed

+62
-14
lines changed

7 files changed

+62
-14
lines changed

CHANGELOG.md

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,13 @@ All notable changes to this project will be documented in this file.
55
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
66
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
77

8-
## [0.3.1] - 1020-01-22
8+
## [0.3.2] - 2020-01-26
9+
10+
### Added
11+
12+
- Added rich.logging
13+
14+
## [0.3.1] - 2020-01-22
915

1016
### Added
1117

examples/log.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,14 +29,16 @@ class RequestHighlighter(RegexHighlighter):
2929

3030
time.sleep(1)
3131

32+
request_highlighter = RequestHighlighter()
33+
3234
console.log(
33-
"HTTP GET /foo/bar/baz/egg.html 200 [0.57, 127.0.0.1:59076]",
34-
highlighter=RequestHighlighter(),
35+
request_highlighter("HTTP GET /foo/bar/baz/egg.html 200 [0.57, 127.0.0.1:59076]"),
3536
)
3637

3738
console.log(
38-
"HTTP GET /foo/bar/baz/background.jpg 200 [0.57, 127.0.0.1:59076]",
39-
highlighter=RequestHighlighter(),
39+
request_highlighter(
40+
"HTTP GET /foo/bar/baz/background.jpg 200 [0.57, 127.0.0.1:59076]"
41+
),
4042
)
4143

4244

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
name = "rich"
33
homepage = "https://github.com/willmcgugan/rich"
44
documentation = "https://rich.readthedocs.io/en/latest/"
5-
version = "0.3.1"
5+
version = "0.3.2"
66
description = "Render rich text, tables, syntax highlighting, markdown and more to the terminal"
77
authors = ["Will McGugan <willmcgugan@gmail.com>"]
88
license = "MIT"

rich/_log_render.py

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
from datetime import datetime
2-
from typing import Any, Iterable, List, Optional, TYPE_CHECKING
2+
from typing import Any, Iterable, List, Optional, TYPE_CHECKING, Union
33

44
from .text import Text
55

@@ -12,10 +12,12 @@ class LogRender:
1212
def __init__(
1313
self,
1414
show_time: bool = True,
15+
show_level: bool = False,
1516
show_path: bool = True,
1617
time_format: str = "[%x %X] ",
1718
) -> None:
1819
self.show_time = show_time
20+
self.show_level = show_level
1921
self.show_path = show_path
2022
self.time_format = time_format
2123
self._last_time: Optional[str] = None
@@ -25,6 +27,8 @@ def __call__(
2527
console: "Console",
2628
renderables: Iterable["ConsoleRenderable"],
2729
log_time: datetime = None,
30+
time_format: str = None,
31+
level: Union[str, Text] = "",
2832
path: str = None,
2933
line_no: int = None,
3034
) -> "Table":
@@ -34,19 +38,23 @@ def __call__(
3438
output = Table(show_header=False, expand=True, box=None, padding=0)
3539
if self.show_time:
3640
output.add_column(style="log.time")
41+
if self.show_level:
42+
output.add_column(style="log.level", width=9)
3743
output.add_column(ratio=1, style="log.message")
3844
if self.show_path and path:
3945
output.add_column(style="log.path")
4046
row: List["RenderableType"] = []
4147
if self.show_time:
4248
if log_time is None:
4349
log_time = datetime.now()
44-
log_time_display = log_time.strftime(self.time_format)
50+
log_time_display = log_time.strftime(time_format or self.time_format)
4551
if log_time_display == self._last_time:
4652
row.append(Text(" " * len(log_time_display)))
4753
else:
4854
row.append(Text(log_time_display))
4955
self._last_time = log_time_display
56+
if self.show_level:
57+
row.append(level)
5058
row.append(Renderables(renderables))
5159
if self.show_path and path:
5260
if line_no is None:

rich/default_styles.py

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -40,14 +40,22 @@
4040
"magenta": Style(color="magenta"),
4141
"cyan": Style(color="cyan"),
4242
"white": Style(color="white"),
43+
"logging.keyword": Style(bold=True, color="yellow"),
44+
"logging.level.notset": Style(dim=True),
45+
"logging.level.debug": Style(color="green"),
46+
"logging.level.info": Style(color="blue"),
47+
"logging.level.warning": Style(color="red"),
48+
"logging.level.error": Style(color="red", bold=True),
49+
"logging.level.critical": Style(color="red", bold=True, reverse=True),
50+
"log.level": Style(),
4351
"log.time": Style(color="cyan", dim=True),
4452
"log.message": Style(),
4553
"log.path": Style(dim=True),
46-
"repr.str": Style(color="green", italic=False),
54+
"repr.str": Style(color="green", italic=False, bold=False),
4755
"repr.brace": Style(bold=True),
4856
"repr.tag_start": Style(bold=True),
49-
"repr.tag_name": Style(color="bright_magenta"),
50-
"repr.tag_contents": Style(color="default", italic=True),
57+
"repr.tag_name": Style(color="bright_magenta", bold=True),
58+
"repr.tag_contents": Style(color="default"),
5159
"repr.tag_end": Style(bold=True),
5260
"repr.attrib_name": Style(color="yellow", italic=True),
5361
"repr.attrib_equal": Style(bold=True),
@@ -56,9 +64,11 @@
5664
"repr.bool_true": Style(color="bright_green", italic=True),
5765
"repr.bool_false": Style(color="bright_red", italic=True),
5866
"repr.none": Style(color="magenta", italic=True),
59-
"repr.url": Style(underline=True, color="default"),
67+
"repr.url": Style(underline=True, color="bright_blue", bold=False),
6068
"rule.line": Style(color="green"),
6169
"rule.text": Style(),
70+
"repr.path": Style(color="magenta"),
71+
"repr.filename": Style(color="bright_magenta", bold=True),
6272
"table.header": Style(bold=True),
6373
"table.footer": Style(bold=True),
6474
"table.cell": Style(),

rich/highlighter.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,8 @@ class ReprHighlighter(RegexHighlighter):
8484
r"(?P<bool_true>True)|(?P<bool_false>False)|(?P<none>None)",
8585
r"(?P<number>\-?[0-9]+\.?[0-9]*)",
8686
r"(?P<number>0x[0-9a-f]*)",
87+
r"(?P<path>(\/\w+)+\/)",
88+
r"(?P<filename>\/\w*\..{3,4})\s",
8789
r"(?P<str>b?\'\'\'.*?\'\'\'|b?\'.*?\'|b?\"\"\".*?\"\"\"|b?\".*?\")",
8890
r"(?P<url>https?:\/\/\S*)",
8991
]

rich/text.py

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
from operator import itemgetter
2+
import re
23
from typing import (
34
Any,
45
Dict,
@@ -189,9 +190,28 @@ def stylize(self, start: int, end: int, style: Union[str, Style]) -> None:
189190
return
190191
self._spans.append(Span(max(0, start), min(length, end), style))
191192

192-
def rstrip(self) -> None:
193-
"""Trip whitespace from end of text
193+
def highlight_words(self, words: Iterable[str], style: Union[str, Style]) -> int:
194+
"""Highlight words with a style.
195+
196+
Args:
197+
words (Iterable[str]): Worlds to highlight.
198+
style (Union[str, Style]): Style to apply.
199+
200+
Returns:
201+
int: Number of words highlighted.
194202
"""
203+
re_words = "|".join(re.escape(word) for word in words)
204+
add_span = self._spans.append
205+
count = 0
206+
_Span = Span
207+
for match in re.finditer(re_words, self.text):
208+
start, end = match.span(0)
209+
add_span(_Span(start, end, style))
210+
count += 1
211+
return count
212+
213+
def rstrip(self) -> None:
214+
"""Trip whitespace from end of text."""
195215
self.text = self.text.rstrip()
196216

197217
def set_length(self, new_length: int) -> None:

0 commit comments

Comments
 (0)