forked from pycontribs/enrich
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.qmd
297 lines (220 loc) · 11.5 KB
/
index.qmd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
---
# sidebar: false
title: "`enrich` 💸"
date: last-modified
callout-appearance: simple
title-block-banner: false
lightbox: true
# citeproc: true
citation:
author: Sam Foreman
type: webpage
title: "`enrich`"
url: https://saforem2.github.io/enrich
editor:
render-on-save: true
execute:
freeze: auto
# filters:
# - lightbox
twitter-card:
image: "./assets/thumbnail.png"
creator: "@saforem2"
site: "@saforem2"
open-graph:
image: "./assets/thumbnail.png"
format:
# html:
html: default
gfm:
author: Sam Foreman
output-file: "index.md"
revealjs:
author: Sam Foreman
output-file: "index-revealjs.html"
---
## `rich` 🤝 `logging`
`enrich` provides a generalized
[`RichHandler`](https://github.com/saforem2/enrich/blob/main/src/enrich/handler.py#L28) that allows
for [`rich`](https://github.com/Textualize/rich) style highlights **without line breaks**
::: {.callout-tip icon=false title='[💡 Tip]{style="color: var(--ansi-green);"}' collapse="false" style="text-align: left!important; width:100%; border: none!important; border-color: var(--dim-text)!important; background-color: var(--bg-transparent); opacity:100%; border-left: 2px solid var(--ansi-green)!important; border-radius: 0pt!important;"}
This document is best viewed online at [`enrich`](https://saforem2.github.io/enrich)
:::
<!-- ::: {layout="[[35, -5, 35]]" layout-valign="bottom" style="display: flex; text-align:center; align-items: flex-end;"} -->
:::: {.columns style="display:flex;"}
::: {.column width="35%"}
![`dark`](https://github.com/saforem2/enrich/blob/main/assets/dark.png?raw=true){#fig-dark .stretch}
:::
::: {.column width="35%"}
![`light`](https://github.com/saforem2/enrich/blob/main/assets/light.png?raw=true){#fig-light .stretch}
:::
::::
## 🧩 Install
```bash
python3 -m pip install "git+https://github.com/saforem2/enrich"
```
<details closed><summary><code>test_logger.py</code></summary>
```python
from enrich import get_logger
log = get_logger(name='test', level='DEBUG')
log.debug('debug')
log.info('info')
log.warning('warning')
log.error('error')
log.critical('critical')
```
<details closed><summary><code>Output</code>:</summary>
::: {.cell-output .cell-output-display style="font-family:monospace!important;"}
<pre style="white-space:pre;overflow-x:auto;line-height:normal;font-family:monospace;">
<span style="color:var(--ansi-black);">[</span><span style="color:var(--ansi-black-bright);">2024-01-28 </span><span style="color:var(--ansi-black-bright);">11:13:30</span><span style="color:var(--ansi-black);">]</span><span style="color:var(--ansi-black);">[</span><b><span style="color:var(--ansi-green);">DEBUG</span></b><span style="color:var(--ansi-black);">]</span><span style="color:var(--ansi-black);">[</span><i><span style="color:var(--ansi-cyan);">test_logger</span></i><span style="color:var(--ansi-green-bright)">:</span><span style="color:var(--ansi-black);">24</span><span style="color:var(--ansi-black);">]</span><b><span style="color:var(--ansi-yellow-bright);"> - </span></b>debug
<span style="color:var(--ansi-black);">[</span><span style="color:var(--ansi-black-bright);">2024-01-28 </span><span style="color:var(--ansi-black-bright);">11:13:30</span><span style="color:var(--ansi-black);">]</span><span style="color:var(--ansi-black);">[</span><b><span style="color:var(--ansi-blue);">INFO</span></b><span style="color:var(--ansi-black);">]</span><span style="color:var(--ansi-black);">[</span><i><span style="color:var(--ansi-cyan);">test_logger</span></i><span style="color:var(--ansi-green-bright)">:</span><span style="color:var(--ansi-black);">25</span><span style="color:var(--ansi-black);">]</span><b><span style="color:var(--ansi-yellow-bright);"> - </span></b>info
<span style="color:var(--ansi-black);">[</span><span style="color:var(--ansi-black-bright);">2024-01-28 </span><span style="color:var(--ansi-black-bright);">11:13:30</span><span style="color:var(--ansi-black);">]</span><span style="color:var(--ansi-black);">[</span><span style="color:var(--ansi-yellow);">WARNING</span><span style="color:var(--ansi-black);">]</span><span style="color:var(--ansi-black);">[</span><i><span style="color:var(--ansi-cyan);">test_logger</span></i><span style="color:var(--ansi-green-bright)">:</span><span style="color:var(--ansi-black);">26</span><span style="color:var(--ansi-black);">]</span><b><span style="color:var(--ansi-yellow-bright);"> - </span></b>warning
<span style="color:var(--ansi-black);">[</span><span style="color:var(--ansi-black-bright);">2024-01-28 </span><span style="color:var(--ansi-black-bright);">11:13:30</span><span style="color:var(--ansi-black);">]</span><span style="color:var(--ansi-black);">[</span><b><span style="color:var(--ansi-red);">ERROR</span></b><span style="color:var(--ansi-black);">]</span><span style="color:var(--ansi-black);">[</span><i><span style="color:var(--ansi-cyan);">test_logger</span></i><span style="color:var(--ansi-green-bright)">:</span><span style="color:var(--ansi-black);">27</span><span style="color:var(--ansi-black);">]</span><b><span style="color:var(--ansi-yellow-bright);"> - </span></b>error
<span style="color:var(--ansi-black);">[</span><span style="color:var(--ansi-black-bright)">2024-01-28 </span><span style="color:var(--ansi-black-bright)">11:13:30</span><span style="color:var(--ansi-black);">]</span><span style="color:var(--ansi-black);">[</span><b><span style="color:var(--black000); background-color: var(--ansi-red-bright);">CRITICAL</span></b><span style="color:var(--ansi-black);">]</span><span style="color:var(--ansi-black);">[</span><i><span style="color:var(--ansi-cyan);">test_logger</span></i><span style="color:var(--ansi-green-bright)">:</span><span style="color:var(--ansi-black);">28</span><span style="color:var(--ansi-black);">]</span><b><span style="color:var(--ansi-yellow-bright);"> - </span></b>critical
</pre>
:::
</details>
</details>
## 📸 Screenshots
<details><summary><b>🤷🏻♂️ Ambivalent</b></summary>
![ambivalent](https://github.com/saforem2/enrich/blob/main/assets/logs/amvbivalent.png?raw=true){.stretch}
![ambivalent-transparent](https://github.com/saforem2/enrich/blob/main/assets/logs/ambivalent-transparent.png?raw=true){.stretch}
</details>
<details><summary><b>😻 Catpuccin Latte</b></summary>
![catpuccin-latte](https://github.com/saforem2/enrich/blob/main/assets/logs/catpuccin-latte.png?raw=true){.stretch}
![catpuccin-latte-transparent](https://github.com/saforem2/enrich/blob/main/assets/logs/catpuccin-latte-transparent.png?raw=true){.stretch}
</details>
<details><summary><b>😈 Doom One</b></summary>
![doom-one](https://github.com/saforem2/enrich/blob/main/assets/logs/doom-one.png?raw=true){.stretch}
![doom-one-transparent](https://github.com/saforem2/enrich/blob/main/assets/logs/doom-one-transparent.png?raw=true){.stretch}
</details>
## 💅 Styles
<details closed><summary>Dark Styles</summary>
![dark-styles](./assets/styles-dark.png){#fig-dark-styles .stretch}
</details>
<details closed><summary>Light Styles</summary>
![light-styles](./assets/styles-light.png){#fig-light-styles .stretch}
</details>
</details>
<!-- <details closed><summary>Screenshot:</summary> -->
<!---->
<!-- ::: {#fig-test-logger} -->
<!---->
<!-- ![`test_logger.py`](./assets/test_logger.png){.stretch} -->
<!---->
<!-- ::: -->
<!---->
<!-- </details> -->
## ⚙️ Setup
```python
from enrich import get_logger
log = get_logger(name='enrich', level='DEBUG')
```
<details><summary><code>Output</code></summary>
```python
log.debug('debug')
log.info('info')
log.warning('warning')
log.error('error')
log.critical('critical')
[2024-01-28 11:04:50][DEBUG][ipython:1] - debug
[2024-01-28 11:04:50][INFO][ipython:2] - info
[2024-01-28 11:04:50][WARNING][ipython:3] - warning
[2024-01-28 11:04:50][ERROR][ipython:4] - error
[2024-01-28 11:04:50][CRITICAL][ipython:5] - critical
```
</details>
<details><summary>Alternative Setup</summary>
- `logging.config.dictConfig(...)`:
```python
import yaml
with Path('logconf.yaml').open('r') as stream:
config = yaml.load(stream, Loader=yaml.FullLoader)
log_config = logging.config.dictConfig(config)
log = logging.getLogger(__name__)
log.setLevel('INFO')
```
- Where `logconf.yaml`:
```yaml
---
# logconf.yaml
handlers:
term:
class: enrich.handler.RichHandler
show_time: true
show_level: true
enable_link_path: false
level: DEBUG
root:
handlers: [term]
disable_existing_loggers: false
...
```
</details>
## 📚 Details
### ↪️ Redirect support
Our Console class adds one additional option to rich.Console in order to
redirect `sys.stdout` and `sys.stderr` streams using a FileProxy.
```python
from enrich.console import Console
import sys
console = Console(
redirect=True, # <-- not supported by rich.console.Console
record=True)
sys.write("foo")
# this assert would have passed without redirect=True
assert console.export_text() == "foo"
```
### 🌯 Soft wrapping
If you want to produce fluid terminal output, one where the client terminal
decides where to wrap the text instead of the application, you can now
tell the Console constructor the soft_wrap preference.
```python
from enrich.console import Console
import sys
console = Console(soft_wrap=True)
console.print(...) # no longer need to pass soft_wrap to each print
```
Rich logger assumes that you always have a fixed width console and it does
wrap logged output according to it. Our alternative logger does exactly the
opposite: it ignores the columns of the current console and prints output
using a Console with soft wrapping enabled.
The result are logged lines that can be displayed on any terminal or web
page as they will allow the client to decide when to perform the wrapping.
```python
import logging
from enrich.logging import RichHandler
FORMAT = "%(message)s"
logging.basicConfig(
level="NOTSET", format=FORMAT, datefmt="[%X]", handlers=[RichHandler()]
)
log = logging.getLogger("rich")
log.info("Text that we do not want pre-wrapped by logger: %s", 100 * "x")
```
### 💾 ANSI Escapes
Extends Rich Console to detect if original text already had ANSI escapes and
decodes it before processing it. This solves the case where printing
output captured from other processes that contained ANSI escapes would brake.
[upstream-404](https://github.com/willmcgugan/rich/discussions/404)
</details>
---
::: {.callout-tip icon=false title='[❤️🩹 Status]{style="color: var(--ansi-red);"}' collapse="false" style="text-align: left!important; width:100%; border: none!important; border-color: var(--dim-text)!important; background-color: var(--bg-transparent); opacity:100%; border-left: 2px solid var(--ansi-red)!important; border-radius: 0pt!important;"}
```{python}
#| echo: false
import datetime
from rich import print
now = datetime.datetime.now()
day = now.strftime('%m/%d/%Y')
time = now.strftime('%H:%M:%S')
print(' '.join([
"[dim italic]Last Updated[/]:",
f"[#F06292]{day}[/]",
f"[dim]@[/]",
f"[#1A8FFF]{time}[/]"
]))
```
<!-- [[![](https://hits.seeyoufarm.com/api/count/incr/badge.svg?url=https%3A%2F%2Fsaforem2.github.io&count_bg=%2300CCFF&title_bg=%23303030&icon=&icon_color=%23E7E7E7&title=hits&edge_flat=false)](https://hits.seeyoufarm.com)]{style="text-align:center;"} -->
<p align="center">
<a href="https://hits.seeyoufarm.com"><img align="center" src="https://hits.seeyoufarm.com/api/count/incr/badge.svg?url=https%3A%2F%2Fsaforem2.github.io%2Fenrich&count_bg=%2300CCFF&title_bg=%23303030&icon=&icon_color=%23E7E7E7&title=hits&edge_flat=false"/></a>
</p>
:::