|
3 | 3 |
|
4 | 4 | import click |
5 | 5 | import click_log # type: ignore |
6 | | -from click import option |
| 6 | +from click import Context, option |
7 | 7 | from dotenv import find_dotenv, load_dotenv |
8 | 8 |
|
9 | 9 | from .. import get_logger |
10 | 10 | from .params import MetricParam, TemplateStringParam |
| 11 | +from .syslog import SyslogFormatter, get_syslog_handler |
11 | 12 |
|
12 | 13 | # We do not interpolate (i.e. replace ${VAR} with corresponding environment variables). |
13 | 14 | # That is because we want to be able to interpolate ourselves for metrics and tokens |
|
22 | 23 | FC = TypeVar("FC", bound=Union[Callable[..., Any], click.Command]) |
23 | 24 |
|
24 | 25 |
|
| 26 | +def metricq_syslog_option() -> Callable[[FC], FC]: |
| 27 | + """ |
| 28 | + Exposes the -\\-syslog option as a click param. |
| 29 | +
|
| 30 | + The program will try read the 'token' from the click params. |
| 31 | + if the token is not set, the default value of 'metricq.program' will be used. |
| 32 | + That's why the @metricq_syslog_option should be the 2nd decorator in the chain. |
| 33 | +
|
| 34 | + It is recommended to use the :py:func:`~metricq.cli.decorator.metricq_command` decorator instead of using this |
| 35 | + function directly. |
| 36 | + """ |
| 37 | + |
| 38 | + def enable_syslog(ctx: Context, param: Any | None, value: Optional[str]) -> None: |
| 39 | + if value is not None: |
| 40 | + logger = get_logger() |
| 41 | + if value == "": |
| 42 | + value = None |
| 43 | + |
| 44 | + program_name = ctx.params.get("token", "metricq.program") |
| 45 | + |
| 46 | + handler = get_syslog_handler(value) |
| 47 | + handler.setFormatter(SyslogFormatter(name=program_name)) |
| 48 | + logger.addHandler(handler) |
| 49 | + |
| 50 | + return option( |
| 51 | + "--syslog", |
| 52 | + help="Enable syslog logging by specifying the a Unix socket or host:port for the logger. If --syslog is set " |
| 53 | + "but no value is specified, the default of localhost:514 will be used.", |
| 54 | + callback=enable_syslog, |
| 55 | + expose_value=False, |
| 56 | + is_flag=False, |
| 57 | + flag_value="", |
| 58 | + ) |
| 59 | + |
| 60 | + |
25 | 61 | def metricq_server_option() -> Callable[[FC], FC]: |
26 | 62 | """ |
27 | 63 | Allows the User to provide a -\\-server option. This option has no input validation and therefore can be any string. |
@@ -144,10 +180,20 @@ def metricq_command( |
144 | 180 | - -\\-token: |
145 | 181 | The Token is used to identify each program on the metricq network. for example: sink-py-dummy |
146 | 182 |
|
147 | | - The token param can be set using the environment variable METRICQ_TOKEN or adding the --token {value} option to the cli command |
| 183 | + The token param can be set using the environment variable METRICQ_TOKEN or adding the --token {value} option |
| 184 | + to the cli command |
| 185 | +
|
| 186 | + - -\\-syslog: |
| 187 | + The Syslog param is used to enable syslog. It can be used with or without parameter. |
| 188 | +
|
| 189 | + If used without parameter (for example: ``metricq-check --syslog`` ) the Syslog will default to localhost:514. |
| 190 | +
|
| 191 | + You can also specify a Unix socket (for example: /dev/log) or a custom host (for example: example.com:5114) |
| 192 | + by adding the value to the syslog flag (for example: ``metricq-check --syslog example.com:5114``) |
| 193 | +
|
148 | 194 |
|
149 | 195 | Full example: |
150 | | - ``metricq-check --server amqp://localhost/ --token sink-py-dummy`` |
| 196 | + ``metricq-check --server amqp://localhost/ --token sink-py-dummy --syslog`` |
151 | 197 |
|
152 | 198 | **Example**:: |
153 | 199 |
|
@@ -185,8 +231,10 @@ def decorator(func: FC) -> click.Command: |
185 | 231 | log_decorator( |
186 | 232 | metricq_token_option(default_token)( |
187 | 233 | metricq_server_option()( |
188 | | - click.command(context_settings=context_settings, epilog=epilog)( |
189 | | - func |
| 234 | + metricq_syslog_option()( |
| 235 | + click.command( |
| 236 | + context_settings=context_settings, epilog=epilog |
| 237 | + )(func) |
190 | 238 | ) |
191 | 239 | ) |
192 | 240 | ) |
|
0 commit comments