1import logging
2
3import click
4
5from suricata_check._version import get_version
6
7
8class ClickHelpOption(click.HelpOption):
9 @staticmethod
10 def show_help(ctx: click.Context, param: click.Parameter, value: bool) -> None:
11 click.echo("suricata-check {}\n".format(get_version()))
12
13 click.HelpOption.show_help(ctx, param, value)
14
15
[docs]
16class ClickHandler(logging.Handler):
17 """Handler to color and write logging messages for the click module."""
18
19 def __init__(
20 self: "ClickHandler",
21 level: int = 0,
22 github: bool = False,
23 github_level: int = logging.WARNING,
24 **kwargs: dict,
25 ) -> None:
26 super().__init__(level, **kwargs)
27 self.github = github
28 self.github_level = github_level
29
[docs]
30 def emit(self: "ClickHandler", record: logging.LogRecord) -> None:
31 """Log the record via click stdout with appropriate colors."""
32 msg = self.format(record)
33
34 if logging.getLevelName(record.levelno) == "DEBUG":
35 click.secho(msg, color=True, dim=True)
36 if logging.getLevelName(record.levelno) == "INFO":
37 click.secho(msg, color=True)
38 if logging.getLevelName(record.levelno) == "WARNING":
39 click.secho(msg, color=True, bold=True, fg="yellow")
40 if logging.getLevelName(record.levelno) == "ERROR":
41 click.secho(msg, color=True, bold=True, fg="red")
42 if logging.getLevelName(record.levelno) == "CRITICAL":
43 click.secho(msg, color=True, bold=True, blink=True, fg="red")
44
45 if self.github and record.levelno >= self.github_level:
46 print(f"::debug::{msg}") # noqa: T201