Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Redirect stdout to the logger system for the Datalayer service #14346

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 29 additions & 10 deletions chia/server/start_data_layer.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
from __future__ import annotations

import contextlib
import logging
import pathlib
import sys
from io import TextIOWrapper
from typing import Any, Dict, Optional, cast

from chia.data_layer.data_layer import DataLayer
Expand All @@ -25,6 +27,20 @@
log = logging.getLogger(__name__)


class StdOutputLogger(TextIOWrapper):
def __init__(self, logger: logging.Logger, level: int = logging.INFO) -> None:
self.logger = logger
self.level = level

def write(self, message: str) -> int:
if message and not message.isspace():
self.logger.log(self.level, message)
return 0

def flush(self) -> None:
pass


# TODO: Review need for config and if retained then hint it properly.
def create_data_layer_service(
root_path: pathlib.Path,
Expand Down Expand Up @@ -79,16 +95,19 @@ async def async_main() -> int:
root_path=DEFAULT_ROOT_PATH,
)

create_all_ssl(
root_path=DEFAULT_ROOT_PATH,
private_node_names=["data_layer"],
public_node_names=["data_layer"],
overwrite=False,
)

service = create_data_layer_service(DEFAULT_ROOT_PATH, config)
await service.setup_process_global_state()
await service.run()
with contextlib.redirect_stdout(
StdOutputLogger(log, logging.INFO)
) if not sys.stdout.isatty() else contextlib.nullcontext():
create_all_ssl(
root_path=DEFAULT_ROOT_PATH,
private_node_names=["data_layer"],
public_node_names=["data_layer"],
overwrite=False,
)

service = create_data_layer_service(DEFAULT_ROOT_PATH, config)
await service.setup_process_global_state()
await service.run()

return 0

Expand Down