-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript_logging.py
More file actions
64 lines (47 loc) · 1.83 KB
/
Copy pathscript_logging.py
File metadata and controls
64 lines (47 loc) · 1.83 KB
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
#!/usr/bin/env python3
"""Demo: structured CLI logging with LoggingOptMixin.
Try:
./script_logging.py
./script_logging.py -v
./script_logging.py -vv
./script_logging.py -vvv
./script_logging.py -vv --log-format extended
./script_logging.py greet Ada
"""
from __future__ import annotations
import logging
from clak import Argument, Command, LoggingOptMixin, Parser
logger = logging.getLogger(__name__)
class GreetCmd(Parser):
"""Greet someone and emit logs at several levels."""
name = Argument("NAME", help="Name to greet", default="World", nargs="?")
def cli_run(self, name="World", **_):
self.logger.debug("Preparing greeting for %s", name)
self.logger.info("Hello, %s", name)
self.logger.success("Greeting delivered")
self.logger.notice("Tip: pass -v / -vv for more detail")
print(f"Hello, {name}!")
class AppMain(LoggingOptMixin, Parser):
"""Logging demo.
``Meta.log_levels`` lists cumulative ``-v`` tiers as ``LEVEL|logger``.
An empty logger name configures the root logger.
"""
class Meta:
log_prefix = __name__
log_default_level = "WARNING"
log_levels = [
["WARNING|" + __name__], # default
["INFO|" + __name__], # -v
["DEBUG|" + __name__], # -vv
["DEBUG|"], # -vvv (root — includes clak internals)
]
# Kept at WARNING until maximum verbosity (-vvv here)
log_silent = ["urllib3", "asyncio"]
greet = Command(GreetCmd)
def cli_run(self, **_):
logger.warning("App module logger (logging.getLogger(__name__))")
self.logger.info("Parser logger (self.logger) — visible from -v")
self.logger.debug("Debug line — visible from -vv")
print("Run a subcommand, e.g. greet — or pass -h")
if __name__ == "__main__":
AppMain()