1616logger = get_logger ()
1717
1818
19+ DEFAULT_LOGGING_CONFIG_FILE = "log.conf"
20+
21+
1922def create_parser () -> ArgumentParser :
2023 """Create a parser for the cmdline arguments."""
2124 import configargparse # type: ignore
2225 parser : ArgumentParser = configargparse .ArgumentParser (auto_env_var_prefix = 'hathor_' )
2326 parser .add_argument ('--stratum-port' , help = 'Port of Stratum server' , type = int , default = 8000 )
2427 parser .add_argument ('--api-port' , help = 'Port of TxMining API server' , type = int , default = 8080 )
25- parser .add_argument ('--log-config' , help = 'Config file for logging' , default = 'log.conf' )
2628 parser .add_argument ('--max-tx-weight' , help = 'Maximum allowed tx weight to be mined' , type = float , default = None )
2729 parser .add_argument ('--max-timestamp-delta' , help = 'Maximum allowed tx timestamp delta' , type = int , default = None )
2830 parser .add_argument ('--tx-timeout' , help = 'Tx mining timeout (seconds)' , type = int , default = None )
@@ -37,6 +39,10 @@ def create_parser() -> ArgumentParser:
3739 parser .add_argument ('--toi-url' , help = 'toi service url' , type = str , default = None )
3840 parser .add_argument ('--toi-fail-block' , help = 'Block tx if toi fails' , default = False , action = 'store_true' )
3941 parser .add_argument ('backend' , help = 'Endpoint of the Hathor API (without version)' , type = str )
42+
43+ logs = parser .add_mutually_exclusive_group ()
44+ logs .add_argument ('--log-config' , help = 'Config file for logging' , default = DEFAULT_LOGGING_CONFIG_FILE )
45+ logs .add_argument ('--json-logs' , help = 'Enabled logging in json' , default = False , action = 'store_true' )
4046 return parser
4147
4248
@@ -52,7 +58,22 @@ def execute(args: Namespace) -> None:
5258
5359 # Configure log.
5460 start_logging ()
55- if os .path .exists (args .log_config ):
61+ if args .json_logs :
62+ logging .basicConfig (level = logging .INFO , format = '%(message)s' )
63+ from structlog .stdlib import LoggerFactory
64+ structlog .configure (
65+ logger_factory = LoggerFactory (),
66+ processors = [
67+ structlog .stdlib .filter_by_level ,
68+ structlog .stdlib .add_logger_name ,
69+ structlog .processors .add_log_level ,
70+ structlog .processors .TimeStamper (fmt = "%Y-%m-%d %H:%M:%S" ),
71+ structlog .processors .format_exc_info ,
72+ structlog .processors .JSONRenderer ()
73+ ])
74+ logger .info ('tx-mining-service' , backend = args .backend )
75+ logger .info ('Logging with json format...' )
76+ elif os .path .exists (args .log_config ):
5677 logging .config .fileConfig (args .log_config )
5778 from structlog .stdlib import LoggerFactory
5879 structlog .configure (logger_factory = LoggerFactory ())
@@ -97,7 +118,7 @@ def execute(args: Namespace) -> None:
97118 max_timestamp_delta = api_app .max_timestamp_delta , fix_invalid_timestamp = api_app .fix_invalid_timestamp ,
98119 only_standard_script = api_app .only_standard_script , tx_filters = tx_filters )
99120
100- web_runner = web .AppRunner (api_app .app )
121+ web_runner = web .AppRunner (api_app .app , logger = logger )
101122 loop .run_until_complete (web_runner .setup ())
102123 site = web .TCPSite (web_runner , '0.0.0.0' , args .api_port )
103124 loop .run_until_complete (site .start ())
0 commit comments