-
Notifications
You must be signed in to change notification settings - Fork 3
/
__init__.py
129 lines (108 loc) · 3.23 KB
/
__init__.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
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
# -*- coding: utf-8 -*-
"""An extremely simple command-line application."""
# standard library imports
import array
from time import sleep
# third-party imports
import click
from loguru import logger
# module imports
from click_loguru import ClickLoguru
# self imports
from .other_module import other_module_levels
# global constants
LOG_FILE_RETENTION = 3
VERSION = "0.4.0"
NAME = "simple"
# define the CLI
click_loguru = ClickLoguru(
NAME,
VERSION,
retention=LOG_FILE_RETENTION,
log_dir_parent="tests/data/logs",
timer_log_level="info",
)
@click_loguru.logging_options
@click.group()
@click_loguru.stash_subcommand()
@click.option(
"-e",
"--extra",
is_flag=True,
show_default=True,
default=False,
help="An extra global option.",
callback=click_loguru.user_global_options_callback,
)
@click.version_option(version=VERSION, prog_name=NAME)
def cli(verbose, quiet, logfile, profile_mem, extra):
"""simple -- a simple cli function with logging by loguru."""
unused_str = (
f"verbose: {verbose} quiet: {quiet}" +
f" logfile: {logfile} profile_mem: {profile_mem} extra{extra}"
)
@cli.command()
@click_loguru.init_logger()
def levels():
"""Log at different severity levels."""
logger.debug("debug message")
logger.info("info message")
logger.warning("warning message")
logger.error("error message")
state = click_loguru.get_global_options()
print(f"logfile_path: {state.logfile_path}")
@cli.command()
@click_loguru.init_logger()
def other_module():
"""Log from another module."""
other_module_levels()
state = click_loguru.get_global_options()
print(f"logfile_path: {state.logfile_path}")
@cli.command()
@click_loguru.init_logger()
def show_context():
"Print value of global quiet option."
state = click_loguru.get_global_options()
print(f"verbose: {state.verbose}")
print(f"quiet: {state.quiet}")
print(f"logfile: {state.logfile}")
if state.subcommand is None:
print("No subcommand")
else:
print(f'Subcommand: "{state.subcommand}"')
if state.logfile_handler_id is None:
print("No logfile handler.")
else:
print(f'Logfile path: "{state.logfile_path}"')
print(f"{state}")
@cli.command()
@click_loguru.init_logger(logfile=False)
def quiet_value():
"Print value of global quiet option."
state = click_loguru.get_global_options()
print(f"{state.quiet:d}", end="")
@cli.command()
@click_loguru.init_logger(logfile=False)
def extra_value():
"Print value of global quiet option."
user_options = click_loguru.get_user_global_options()
print(f"{user_options['extra']:d}", end="")
@cli.command()
@click_loguru.init_logger()
@click_loguru.log_elapsed_time(level="info")
def log_elapsed_time():
"""Print elapsed time in command."""
click_loguru.elapsed_time("first")
sleep(1)
click_loguru.elapsed_time("second")
sleep(2)
click_loguru.elapsed_time(None)
@cli.command()
@click_loguru.init_logger()
@click_loguru.log_peak_memory_use(level="info")
@click.argument("alloc_size", type=int)
def log_memory_use(alloc_size):
"""Print elapsed time in command."""
arr = array.array("b")
for unused_i in range(alloc_size * 1024 * 1024):
arr.append(0)