Skip to content

Commit

Permalink
add /get_log_level and /set_log_level to all rpcs
Browse files Browse the repository at this point in the history
  • Loading branch information
altendky committed Nov 8, 2024
1 parent 9eb1d5a commit 2fd27fd
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 3 deletions.
6 changes: 6 additions & 0 deletions chia/rpc/rpc_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,12 @@ async def stop_node(self) -> dict:
async def healthz(self) -> dict:
return await self.fetch("healthz", {})

async def get_log_level(self) -> dict:
return await self.fetch("get_log_level", {})

async def set_log_level(self, level_number: int) -> dict:
return await self.fetch("set_log_level", {"level_number": level_number})

def close(self) -> None:
self.closing_task = asyncio.create_task(self.session.close())

Expand Down
18 changes: 18 additions & 0 deletions chia/rpc/rpc_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
from chia.server.ws_connection import WSChiaConnection
from chia.types.peer_info import PeerInfo
from chia.util.byte_types import hexstr_to_bytes
from chia.util.chia_logging import set_log_level
from chia.util.config import str2bool
from chia.util.ints import uint16
from chia.util.json_util import dict_to_json_str
Expand Down Expand Up @@ -245,6 +246,8 @@ def _get_routes(self) -> dict[str, Endpoint]:
"/get_routes": self.get_routes,
"/get_version": self.get_version,
"/healthz": self.healthz,
"/get_log_level": self.get_log_level,
"/set_log_level": self.set_log_level,
}

async def get_routes(self, request: dict[str, Any]) -> EndpointResult:
Expand Down Expand Up @@ -309,6 +312,21 @@ async def get_version(self, request: dict[str, Any]) -> EndpointResult:
"version": __version__,
}

async def get_log_level(self, request: dict[str, Any]) -> EndpointResult:
logger = logging.getLogger()
level_number = logger.getEffectiveLevel()
level_name = logging.getLevelName(level_number)
return {
"level_number": level_number,
"level_name": level_name,
}

async def set_log_level(self, request: dict[str, Any]) -> EndpointResult:
requested_level_number = request["level_number"]
set_log_level(log_level=requested_level_number, service_name=self.service_name)

return {}

async def ws_api(self, message: WsRpcMessage) -> Optional[dict[str, object]]:
"""
This function gets called when new message is received via websocket.
Expand Down
14 changes: 11 additions & 3 deletions chia/util/chia_logging.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,14 +93,22 @@ def initialize_logging(
handlers.append(get_file_log_handler(file_log_formatter, beta_root_path, get_beta_logging_config()))

root_logger = logging.getLogger()
log_level_exceptions = {}
for handler in handlers:
root_logger.addHandler(handler)

set_log_level(log_level=log_level, service_name=service_name)


def set_log_level(log_level: str, service_name: str) -> None:
root_logger = logging.getLogger()
log_level_exceptions = {}

for handler in root_logger.handlers:
try:
handler.setLevel(log_level)
except Exception as e:
handler.setLevel(default_log_level)
log_level_exceptions[handler] = e
root_logger.addHandler(handler)

for handler, exception in log_level_exceptions.items():
root_logger.error(
Expand All @@ -110,7 +118,7 @@ def initialize_logging(

# Adjust the root logger to the smallest used log level since its default level is WARNING which would overwrite
# the potentially smaller log levels of specific handlers.
root_logger.setLevel(min(handler.level for handler in handlers))
root_logger.setLevel(min(handler.level for handler in root_logger.handlers))

if root_logger.level <= logging.DEBUG:
logging.getLogger("aiosqlite").setLevel(logging.INFO) # Too much logging on debug level
Expand Down

0 comments on commit 2fd27fd

Please sign in to comment.