Skip to content

Commit e1bbaa3

Browse files
committed
fix logging, kill smurfs and fix some language issues
1 parent e686d5e commit e1bbaa3

4 files changed

Lines changed: 68 additions & 35 deletions

File tree

examples/logger_test.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
from metricq import get_logger
2+
from metricq.cli import command
3+
4+
5+
logger = get_logger()
6+
7+
8+
logger.info("global scope")
9+
10+
11+
@command(default_token="logging_test")
12+
def run(server, token):
13+
logger.warning("in run")
14+
15+
16+
if __name__ == "__main__":
17+
run()

metricq/cli/__init__.py

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
from .decorator import (
2-
metricq_command,
3-
metricq_metric_option,
4-
metricq_server_option,
5-
metricq_syslog_option,
6-
metricq_token_option,
2+
command,
3+
metric_option,
4+
server_option,
5+
syslog_option,
6+
token_option,
77
)
88
from .params import (
99
ChoiceParam,
@@ -21,9 +21,9 @@
2121
"TemplateStringParam",
2222
"TimestampParam",
2323
"MetricParam",
24-
"metricq_command",
25-
"metricq_metric_option",
26-
"metricq_server_option",
27-
"metricq_syslog_option",
28-
"metricq_token_option",
24+
"command",
25+
"metric_option",
26+
"server_option",
27+
"syslog_option",
28+
"token_option",
2929
]

metricq/cli/decorator.py

Lines changed: 28 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
from click import Context, option
88
from dotenv import find_dotenv, load_dotenv
99

10-
from .. import get_logger
10+
from ..logging import get_logger, FORMAT
1111
from .params import MetricParam, TemplateStringParam
1212
from .syslog import SyslogFormatter, get_syslog_handler
1313

@@ -24,15 +24,15 @@
2424
FC = TypeVar("FC", bound=Union[Callable[..., Any], click.Command])
2525

2626

27-
def metricq_syslog_option() -> Callable[[FC], FC]:
27+
def syslog_option() -> Callable[[FC], FC]:
2828
"""
2929
Exposes the -\\-syslog option as a click param.
3030
3131
The program will try read the 'token' from the click params.
3232
if the token is not set, the default value of 'metricq.program' will be used.
33-
That's why the @metricq_syslog_option should be the 2nd decorator in the chain.
33+
That's why the @syslog_option should be the 2nd decorator in the chain.
3434
35-
It is recommended to use the :py:func:`~metricq.cli.decorator.metricq_command` decorator instead of using this
35+
It is recommended to use the :py:func:`~metricq.cli.decorator.command` decorator instead of using this
3636
function directly.
3737
"""
3838

@@ -50,20 +50,21 @@ def enable_syslog(ctx: Context, param: Any | None, value: Optional[str]) -> None
5050

5151
return option(
5252
"--syslog",
53-
help="Enable syslog logging by specifying the a Unix socket or host:port for the logger. If --syslog is set "
54-
"but no value is specified, the default of localhost:514 will be used.",
53+
help="Enable syslog logging by specifying a Unix socket or a host:port"
54+
" combination for the logger. If --syslog is set but no value is "
55+
"specified, the default of localhost:514 will be used.",
5556
callback=enable_syslog,
5657
expose_value=False,
5758
is_flag=False,
5859
flag_value="",
5960
)
6061

6162

62-
def metricq_server_option() -> Callable[[FC], FC]:
63+
def server_option() -> Callable[[FC], FC]:
6364
"""
6465
Allows the User to provide a -\\-server option. This option has no input validation and therefore can be any string.
6566
66-
It is recommended to use the :py:func:`~metricq.cli.decorator.metricq_command` decorator instead
67+
It is recommended to use the :py:func:`~metricq.cli.decorator.command` decorator instead
6768
of using this function directly.
6869
6970
"""
@@ -76,12 +77,12 @@ def metricq_server_option() -> Callable[[FC], FC]:
7677
)
7778

7879

79-
def metricq_token_option(default: str) -> Callable[[FC], FC]:
80+
def token_option(default: str) -> Callable[[FC], FC]:
8081
"""
8182
Allows the User to provide a -\\-metric option. The input must follow the specification provided
8283
`here <https://github.com/metricq/metricq/wiki/Metrics#selecting-good-metric-names>`_.
8384
84-
It is recommended to use the :py:func:`~metricq.cli.decorator.metricq_command` decorator instead of using this
85+
It is recommended to use the :py:func:`~metricq.cli.decorator.command` decorator instead of using this
8586
function directly.
8687
"""
8788
return option(
@@ -94,7 +95,7 @@ def metricq_token_option(default: str) -> Callable[[FC], FC]:
9495
)
9596

9697

97-
def metricq_metric_option(
98+
def metric_option(
9899
default: Optional[str] = None, multiple: bool = False, required: bool = False
99100
) -> Callable[[FC], FC]:
100101
"""
@@ -111,8 +112,8 @@ def metricq_metric_option(
111112
112113
**Example**::
113114
114-
@metricq_command(default_token="example.program")
115-
@metricq_metricq_option(required=true, default="example.metric")
115+
@command(default_token="example.program")
116+
@metric_option(required=true, default="example.metric")
116117
def metric_example(
117118
server: str, token: str, metric: str
118119
) -> None:
@@ -123,8 +124,8 @@ def metric_example(
123124
# Run the sink. This call will block until the connection is closed.
124125
sink.run()
125126
126-
@metricq_command(default_token="example.program")
127-
@metricq_metricq_option(required=true, multiple=True) # <-- multiple is set
127+
@command(default_token="example.program")
128+
@metric_option(required=true, multiple=True) # <-- multiple is set
128129
def multi_metric_example(
129130
server: str, token: str, metric: List[str]
130131
) -> None:
@@ -150,15 +151,17 @@ def multi_metric_example(
150151
)
151152

152153

153-
def get_metric_command_logger() -> logging.Logger:
154+
def _get_metric_command_logger() -> logging.Logger:
154155
logger = get_logger()
155156
logger.setLevel(logging.WARNING)
156157
click_log.basic_config(logger)
157158

159+
logger.handlers[0].formatter = logging.Formatter(fmt=FORMAT)
160+
158161
return logger
159162

160163

161-
def metricq_command(
164+
def command(
162165
default_token: str, client_version: str | None = None
163166
) -> Callable[[FC], click.Command]:
164167
"""Standardized wrapper for click commands
@@ -170,7 +173,7 @@ def metricq_command(
170173
Returns:
171174
Callable[[FC], click.Command]: click command
172175
173-
The :py:func:`~metricq.cli.wrapper.metricq_command` is the first parameter of any given click/cli command. The main purpose is to provide the most used parameters.
176+
The :py:func:`~metricq.cli.wrapper.command` is the first parameter of any given click/cli command. The main purpose is to provide the most used parameters.
174177
These parameters are 'server' and 'token'.
175178
176179
- -\\-server:
@@ -198,7 +201,7 @@ def metricq_command(
198201
199202
**Example**::
200203
201-
@metricq_command(default_token="source-py-dummy")
204+
@command(default_token="source-py-dummy")
202205
def dummy(
203206
server: str, token: str
204207
) -> None:
@@ -210,15 +213,15 @@ def dummy(
210213
dummy()
211214
212215
"""
213-
logger = get_metric_command_logger()
216+
logger = _get_metric_command_logger()
214217

215218
log_decorator = cast(
216219
Callable[[FC], FC], click_log.simple_verbosity_option(logger, default="warning")
217220
)
218221
context_settings = {"auto_envvar_prefix": "METRICQ"}
219222
epilog = (
220-
"All options can be passed as environment variables prefixed with 'METRICQ_'."
221-
"I.e., 'METRICQ_SERVER=amqps://...'.\n"
223+
"All options can be passed as environment variables prefixed with 'METRICQ_', "
224+
"for example, 'METRICQ_SERVER=amqps://...'.\n"
222225
"\n"
223226
"You can also create a '.metricq' file in the current or home directory that "
224227
"contains environment variable settings in the same format.\n"
@@ -230,9 +233,9 @@ def dummy(
230233
def decorator(func: FC) -> click.Command:
231234
return click.version_option(version=client_version)(
232235
log_decorator(
233-
metricq_token_option(default_token)(
234-
metricq_server_option()(
235-
metricq_syslog_option()(
236+
token_option(default_token)(
237+
server_option()(
238+
syslog_option()(
236239
click.command(
237240
context_settings=context_settings, epilog=epilog
238241
)(func)

metricq/logging.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
# FROM https://stackoverflow.com/a/36294984/620382
22
import functools
33
import logging
4+
import sys
45
import types
56
from collections.abc import Callable
67
from typing import Optional, TypeVar
@@ -40,3 +41,15 @@ def get_logger(name: Optional[str] = None) -> logging.Logger:
4041
log.handle = _handle_wrap(log.handle) # type: ignore[method-assign]
4142
setattr(log, "_newstyle", True)
4243
return log
44+
45+
46+
# We do not want to depend on click_log here, so instead we do a VERY
47+
# basic root logger setup
48+
FORMAT = "%(asctime)s [%(levelname)-8s] [%(name)-20s] %(message)s"
49+
_logger = get_logger()
50+
logging.root.handlers = []
51+
logging.basicConfig(
52+
level=logging.WARNING,
53+
format=FORMAT,
54+
handlers=[logging.StreamHandler(sys.stdout)],
55+
)

0 commit comments

Comments
 (0)