forked from adafruit/Adafruit_CircuitPython_Logging
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlogging_formatter_example.py
48 lines (32 loc) · 1.38 KB
/
logging_formatter_example.py
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
# SPDX-FileCopyrightText: 2024 Tim Cocks
# SPDX-License-Identifier: MIT
"""Illustrate usage of default and custom Formatters including
one with timestamps."""
import adafruit_logging as logging
# To test on CPython, un-comment below and comment out above
# import logging
logger = logging.getLogger("example")
logger.setLevel(logging.INFO)
print_handler = logging.StreamHandler()
logger.addHandler(print_handler)
default_formatter = logging.Formatter()
print_handler.setFormatter(default_formatter)
logger.info("Default formatter example")
timestamp_formatter = logging.Formatter(fmt="%(asctime)s %(levelname)s: %(message)s")
print_handler.setFormatter(timestamp_formatter)
logger.info("Timestamp formatter example")
custom_vals_formatter = logging.Formatter(
fmt="%(ip)s %(levelname)s: %(message)s", defaults={"ip": "192.168.1.188"}
)
print_handler.setFormatter(custom_vals_formatter)
logger.info("Custom formatter example")
bracket_timestamp_formatter = logging.Formatter(
fmt="{asctime} {levelname}: {message}", style="{"
)
print_handler.setFormatter(bracket_timestamp_formatter)
logger.info("Timestamp formatter bracket style example")
bracket_custom_vals_formatter = logging.Formatter(
fmt="{ip} {levelname}: {message}", style="{", defaults={"ip": "192.168.1.188"}
)
print_handler.setFormatter(bracket_custom_vals_formatter)
logger.info("Custom formatter bracket style example")