Skip to content

Commit 6dba7bb

Browse files
authored
Fix logging configuration (#1266)
* Fix logging configuration * cleanup how errors/exits are done * PR feedback
1 parent 6bc1db2 commit 6dba7bb

File tree

3 files changed

+43
-34
lines changed

3 files changed

+43
-34
lines changed

trinity/cli_parser.py

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -119,10 +119,6 @@ def __call__(self,
119119
log_levels[path] = log_level
120120

121121

122-
DEFAULT_STDERR_LOG_LEVEL = logging.INFO
123-
DEFAULT_DEBUG_LOG_LEVEL = logging.DEBUG
124-
125-
126122
parser = argparse.ArgumentParser(description='Trinity')
127123

128124
#
@@ -185,15 +181,13 @@ def __call__(self,
185181
logging_parser.add_argument(
186182
'--stderr-log-level',
187183
dest="stderr_log_level",
188-
default=DEFAULT_STDERR_LOG_LEVEL,
189184
help=(
190185
"Configure the logging level for the stderr logging."
191186
),
192187
)
193188
logging_parser.add_argument(
194189
'--file-log-level',
195190
dest="file_log_level",
196-
default=DEFAULT_DEBUG_LOG_LEVEL,
197191
help=(
198192
"Configure the logging level for file-based logging."
199193
),

trinity/main.py

Lines changed: 36 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
1-
from argparse import Namespace
1+
from argparse import ArgumentParser, Namespace
22
import asyncio
33
import logging
44
import signal
5-
import sys
65
import time
76
from typing import (
87
Any,
@@ -126,16 +125,30 @@ def main() -> None:
126125
"networks are supported.".format(args.network_id)
127126
)
128127

129-
logger, formatter, handler_stream = setup_trinity_stderr_logging(
130-
args.stderr_log_level
128+
has_ambigous_logging_config = (
129+
args.log_levels is not None and
130+
None in args.log_levels and
131+
args.stderr_log_level is not None
131132
)
133+
if has_ambigous_logging_config:
134+
parser.error(
135+
"\n"
136+
"Ambiguous logging configuration: The logging level for stderr was "
137+
"configured with both `--stderr-log-level` and `--log-level`. "
138+
"Please remove one of these flags",
139+
)
140+
141+
stderr_logger, formatter, handler_stream = setup_trinity_stderr_logging(
142+
args.stderr_log_level or (args.log_levels and args.log_levels.get(None))
143+
)
144+
132145
if args.log_levels:
133146
setup_log_levels(args.log_levels)
134147

135148
try:
136149
chain_config = ChainConfig.from_parser_args(args)
137150
except AmbigiousFileSystem:
138-
exit_because_ambigious_filesystem(logger)
151+
parser.error(TRINITY_AMBIGIOUS_FILESYSTEM_INFO)
139152

140153
if not is_data_dir_initialized(chain_config):
141154
# TODO: this will only work as is for chains with known genesis
@@ -144,20 +157,18 @@ def main() -> None:
144157
try:
145158
initialize_data_dir(chain_config)
146159
except AmbigiousFileSystem:
147-
exit_because_ambigious_filesystem(logger)
160+
parser.error(TRINITY_AMBIGIOUS_FILESYSTEM_INFO)
148161
except MissingPath as e:
149-
msg = (
162+
parser.error(
150163
"\n"
151-
"It appears that {} does not exist.\n"
152-
"Trinity does not attempt to create directories outside of its root path\n"
153-
"Either manually create the path or ensure you are using a data directory\n"
164+
f"It appears that {e.path} does not exist. "
165+
"Trinity does not attempt to create directories outside of its root path. "
166+
"Either manually create the path or ensure you are using a data directory "
154167
"inside the XDG_TRINITY_ROOT path"
155-
).format(e.path)
156-
logger.error(msg)
157-
sys.exit(1)
168+
)
158169

159-
logger, log_queue, listener = setup_trinity_file_and_queue_logging(
160-
logger,
170+
file_logger, log_queue, listener = setup_trinity_file_and_queue_logging(
171+
stderr_logger,
161172
formatter,
162173
handler_stream,
163174
chain_config,
@@ -168,8 +179,8 @@ def main() -> None:
168179

169180
# compute the minimum configured log level across all configured loggers.
170181
min_configured_log_level = min(
171-
args.stderr_log_level,
172-
args.file_log_level,
182+
stderr_logger.level,
183+
file_logger.level,
173184
*(args.log_levels or {}).values()
174185
)
175186

@@ -192,7 +203,7 @@ def main() -> None:
192203
listener,
193204
event_bus,
194205
main_endpoint,
195-
logger
206+
stderr_logger,
196207
)
197208

198209

@@ -237,7 +248,7 @@ def trinity_boot(args: Namespace,
237248
except TimeoutError as e:
238249
logger.error("Timeout waiting for database to start. Exiting...")
239250
kill_process_gracefully(database_server_process, logger)
240-
sys.exit(1)
251+
ArgumentParser().error(message="Timed out waiting for database start")
241252

242253
networking_process.start()
243254
logger.info("Started networking process (pid=%d)", networking_process.pid)
@@ -276,7 +287,8 @@ def kill_trinity_gracefully(logger: logging.Logger,
276287
database_server_process: Any,
277288
networking_process: Any,
278289
plugin_manager: PluginManager,
279-
event_bus: EventBus) -> None:
290+
event_bus: EventBus,
291+
message: str="Trinity shudown complete\n") -> None:
280292
# When a user hits Ctrl+C in the terminal, the SIGINT is sent to all processes in the
281293
# foreground *process group*, so both our networking and database processes will terminate
282294
# at the same time and not sequentially as we'd like. That shouldn't be a problem but if
@@ -297,7 +309,10 @@ def kill_trinity_gracefully(logger: logging.Logger,
297309
time.sleep(0.2)
298310
kill_process_gracefully(networking_process, logger)
299311
logger.info('Networking process (pid=%d) terminated', networking_process.pid)
300-
sys.exit()
312+
313+
# This is required to be within the `kill_trinity_gracefully` so that
314+
# plugins can trigger a shutdown of the trinity process.
315+
ArgumentParser().exit(message=message)
301316

302317

303318
@setup_cprofiler('run_database_process')
@@ -321,11 +336,6 @@ def _sigint_handler(*args: Any) -> None:
321336
raise
322337

323338

324-
def exit_because_ambigious_filesystem(logger: logging.Logger) -> None:
325-
logger.error(TRINITY_AMBIGIOUS_FILESYSTEM_INFO)
326-
sys.exit(1)
327-
328-
329339
async def exit_on_signal(service_to_exit: BaseService) -> None:
330340
loop = service_to_exit.get_event_loop()
331341
sigint_received = asyncio.Event()

trinity/utils/logging.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,8 +58,10 @@ def setup_log_levels(log_levels: Dict[str, int]) -> None:
5858
logger.setLevel(level)
5959

6060

61-
def setup_trinity_stderr_logging(level: int=logging.INFO,
61+
def setup_trinity_stderr_logging(level: int=None,
6262
) -> Tuple[Logger, Formatter, StreamHandler]:
63+
if level is None:
64+
level = logging.INFO
6365
logger = logging.getLogger('trinity')
6466
logger.setLevel(logging.DEBUG)
6567

@@ -86,9 +88,12 @@ def setup_trinity_file_and_queue_logging(
8688
formatter: Formatter,
8789
handler_stream: StreamHandler,
8890
chain_config: ChainConfig,
89-
level: int=logging.DEBUG) -> Tuple[Logger, 'Queue[str]', QueueListener]:
91+
level: int=None) -> Tuple[Logger, 'Queue[str]', QueueListener]:
9092
from .mp import ctx
9193

94+
if level is None:
95+
level = logging.DEBUG
96+
9297
log_queue = ctx.Queue()
9398

9499
handler_file = RotatingFileHandler(

0 commit comments

Comments
 (0)