Skip to content

Commit

Permalink
Log full command names (#263)
Browse files Browse the repository at this point in the history
  • Loading branch information
Alex-Burmak authored Nov 29, 2024
1 parent ddbdc96 commit bd8912a
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 14 deletions.
14 changes: 7 additions & 7 deletions ch_tools/chadmin/cli/chadmin_group.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

from ch_tools import __version__
from ch_tools.common import logging
from ch_tools.common.utils import get_full_command_name

# pylint: disable=too-many-ancestors

Expand Down Expand Up @@ -33,12 +34,13 @@ def add_command(
@cloup.pass_context
def wrapper(ctx, *a, **kw):
logging.configure(
ctx.obj["config"]["loguru"], "chadmin", {"cmd_name": cmd.name}
ctx.obj["config"]["loguru"],
"chadmin",
{"cmd_name": get_full_command_name(ctx)},
)

logging.debug(
"Executing command '{}', params: {}, args: {}, version: {}",
cmd.name,
"Command starts executing, params: {}, args: {}, version: {}",
{
**ctx.parent.params,
**ctx.params,
Expand All @@ -49,11 +51,9 @@ def wrapper(ctx, *a, **kw):

try:
cmd_callback(*a, **kw)
logging.debug("Command '{}' completed", cmd.name)
logging.debug("Command completed")
except Exception:
logging.exception(
"Command '{}' failed with error:", cmd.name, short_stdout=True
)
logging.exception("Command failed with error:", short_stdout=True)

cmd.callback = wrapper
super().add_command(
Expand Down
2 changes: 1 addition & 1 deletion ch_tools/common/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@
},
"loguru": {
"formatters": {
"default": "{time:YYYY-MM-DD HH:mm:ss,SSS} {process.name:11} {process.id:5} [{level:8}] {extra[logger_name]}: {extra[cmd_name]}: {message}",
"default": "{time:YYYY-MM-DD HH:mm:ss,SSS} {process.name:11} {process.id:5} [{level:8}] {extra[logger_name]} {extra[cmd_name]}: {message}",
},
"handlers": {
"chadmin": {
Expand Down
14 changes: 14 additions & 0 deletions ch_tools/common/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
import subprocess
from pathlib import Path

from click import Context


def version_ge(version1, version2):
"""
Expand Down Expand Up @@ -81,3 +83,15 @@ def first_key(mapping):

def first_value(mapping):
return next(iter(mapping.values()))


def get_full_command_name(ctx: Context) -> str:
"""
Return full command name (with names of groups).
"""
if ctx.parent is None:
return ""

cmd_name = ctx.command.name or "unknown"
parent_cmd_name = get_full_command_name(ctx.parent)
return f"{parent_cmd_name} {cmd_name}" if parent_cmd_name else cmd_name
8 changes: 5 additions & 3 deletions ch_tools/monrun_checks/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

from ch_tools.common import logging
from ch_tools.common.config import CH_MONITORING_LOG_FILE, load_config
from ch_tools.common.utils import get_full_command_name

warnings.filterwarnings(action="ignore", message="Python 3.6 is no longer supported")

Expand Down Expand Up @@ -66,12 +67,13 @@ def add_command(
@pass_context
def callback_wrapper(ctx, *args, **kwargs):
logging.configure(
ctx.obj["config"]["loguru"], "ch-monitoring", {"cmd_name": cmd.name}
ctx.obj["config"]["loguru"],
"ch-monitoring",
{"cmd_name": get_full_command_name(ctx)},
)

logging.debug(
"Executing command '{}', params: {}, args: {}, version: {}",
cmd.name,
"Command starts executing, params: {}, args: {}, version: {}",
{
**ctx.parent.params,
**ctx.params,
Expand Down
9 changes: 6 additions & 3 deletions ch_tools/monrun_checks_keeper/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
import click
import cloup

from ch_tools.common.utils import get_full_command_name

warnings.filterwarnings(action="ignore", message="Python 3.6 is no longer supported")

# pylint: disable=wrong-import-position
Expand Down Expand Up @@ -55,12 +57,13 @@ def add_command(
@cloup.pass_context
def wrapper(ctx, *a, **kw):
logging.configure(
ctx.obj["config"]["loguru"], "keeper-monitoring", {"cmd_name": cmd.name}
ctx.obj["config"]["loguru"],
"keeper-monitoring",
{"cmd_name": get_full_command_name(ctx)},
)

logging.debug(
"Executing command '{}', params: {}, args: {}, version: {}",
cmd.name,
"Command starts executing, params: {}, args: {}, version: {}",
{
**ctx.parent.params,
**ctx.params,
Expand Down

0 comments on commit bd8912a

Please sign in to comment.