From 32a2e929936adf925cddfb02a5ace1f023f0951b Mon Sep 17 00:00:00 2001 From: Jack Robison Date: Mon, 4 Apr 2022 10:34:51 -0400 Subject: [PATCH] log to file --- scribe/blockchain/__main__.py | 7 +++++-- scribe/common.py | 14 ++++++++++++-- scribe/elasticsearch/__main__.py | 6 ++++-- scribe/hub/__main__.py | 7 ++++--- scribe/hub/session.py | 20 +++++++++----------- 5 files changed, 34 insertions(+), 20 deletions(-) diff --git a/scribe/blockchain/__main__.py b/scribe/blockchain/__main__.py index a47cc88..274c520 100644 --- a/scribe/blockchain/__main__.py +++ b/scribe/blockchain/__main__.py @@ -1,3 +1,4 @@ +import os import logging import traceback import argparse @@ -7,14 +8,16 @@ from scribe.blockchain.service import BlockchainProcessorService def main(): - setup_logging() parser = argparse.ArgumentParser( prog='scribe' ) Env.contribute_to_arg_parser(parser) args = parser.parse_args() + try: - block_processor = BlockchainProcessorService(Env.from_arg_parser(args)) + env = Env.from_arg_parser(args) + setup_logging(os.path.join(env.db_dir, 'scribe.log')) + block_processor = BlockchainProcessorService(env) block_processor.run() except Exception: traceback.print_exc() diff --git a/scribe/common.py b/scribe/common.py index d42fed7..be4cb4c 100644 --- a/scribe/common.py +++ b/scribe/common.py @@ -2,6 +2,7 @@ import hashlib import hmac import ipaddress import logging +import logging.handlers import typing import collections from asyncio import get_event_loop, Event @@ -23,8 +24,17 @@ HISTOGRAM_BUCKETS = ( ) -def setup_logging(): - logging.basicConfig(level=logging.INFO, format="%(asctime)s %(levelname)-4s %(name)s:%(lineno)d: %(message)s") +def setup_logging(log_path: str): + log = logging.getLogger('scribe') + fmt = logging.Formatter("%(asctime)s %(levelname)-4s %(name)s:%(lineno)d: %(message)s") + handler = logging.handlers.RotatingFileHandler(log_path, maxBytes=1024*1024*5, backupCount=2) + handler.setFormatter(fmt) + log.addHandler(handler) + handler = logging.StreamHandler() + handler.setFormatter(fmt) + log.addHandler(handler) + + log.setLevel(logging.INFO) logging.getLogger('aiohttp').setLevel(logging.WARNING) logging.getLogger('elasticsearch').setLevel(logging.WARNING) diff --git a/scribe/elasticsearch/__main__.py b/scribe/elasticsearch/__main__.py index 976db56..c604e15 100644 --- a/scribe/elasticsearch/__main__.py +++ b/scribe/elasticsearch/__main__.py @@ -1,3 +1,4 @@ +import os import logging import traceback import argparse @@ -7,7 +8,6 @@ from scribe.elasticsearch.service import ElasticSyncService def main(): - setup_logging() parser = argparse.ArgumentParser( prog='scribe-elastic-sync' ) @@ -16,7 +16,9 @@ def main(): args = parser.parse_args() try: - server = ElasticSyncService(Env.from_arg_parser(args)) + env = Env.from_arg_parser(args) + setup_logging(os.path.join(env.db_dir, 'scribe-elastic-sync.log')) + server = ElasticSyncService(env) server.run(args.reindex) except Exception: traceback.print_exc() diff --git a/scribe/hub/__main__.py b/scribe/hub/__main__.py index 5d6ca0c..519026e 100644 --- a/scribe/hub/__main__.py +++ b/scribe/hub/__main__.py @@ -1,3 +1,4 @@ +import os import logging import traceback import argparse @@ -7,7 +8,6 @@ from scribe.hub.service import HubServerService def main(): - setup_logging() parser = argparse.ArgumentParser( prog='scribe-hub' ) @@ -15,8 +15,9 @@ def main(): args = parser.parse_args() try: - - server = HubServerService(Env.from_arg_parser(args)) + env = Env.from_arg_parser(args) + setup_logging(os.path.join(env.db_dir, 'scribe-hub.log')) + server = HubServerService(env) server.run() except Exception: traceback.print_exc() diff --git a/scribe/hub/session.py b/scribe/hub/session.py index 1cca881..f539eb2 100644 --- a/scribe/hub/session.py +++ b/scribe/hub/session.py @@ -215,8 +215,7 @@ class SessionManager: protocol_class = LBRYElectrumX else: raise ValueError(kind) - protocol_factory = partial(protocol_class, self, self.db, - self.mempool, kind) + protocol_factory = partial(protocol_class, self, kind) host, port = args[:2] try: @@ -666,7 +665,7 @@ class LBRYElectrumX(asyncio.Protocol): ) max_errors = 10 - def __init__(self, session_manager: SessionManager, db: 'HubDB', mempool: 'MemPool', kind: str): + def __init__(self, session_manager: SessionManager, kind: str): connection = JSONRPCConnection(JSONRPCAutoDetect) self.env = session_manager.env self.framer = self.default_framer() @@ -700,8 +699,7 @@ class LBRYElectrumX(asyncio.Protocol): self.logger = logging.getLogger(__name__) self.session_manager = session_manager - self.db = db - self.mempool = mempool + self.kind = kind # 'RPC', 'TCP' etc. self.coin = self.env.coin self.anon_logs = self.env.anon_logs @@ -719,8 +717,10 @@ class LBRYElectrumX(asyncio.Protocol): self.sv_seen = False self.protocol_tuple = self.PROTOCOL_MIN self.protocol_string = None + self.daemon = self.session_manager.daemon - self.db: 'HubDB' = self.session_manager.db + self.db = self.session_manager.db + self.mempool = self.session_manager.mempool def data_received(self, framed_message): """Called by asyncio when a message comes in.""" @@ -757,13 +757,11 @@ class LBRYElectrumX(asyncio.Protocol): self._pm_task = self.loop.create_task(self._receive_messages()) self.session_id = next(self.session_counter) - context = {'conn_id': f'{self.session_id}'} - self.logger = logging.getLogger(__name__) # util.ConnectionLogger(self.logger, context) + # context = {'conn_id': f'{self.session_id}'} + # self.logger = logging.getLogger(__name__) # util.ConnectionLogger(self.logger, context) self.group = self.session_manager.add_session(self) self.session_manager.session_count_metric.labels(version=self.client_version).inc() - peer_addr_str = self.peer_address_str() - self.logger.info(f'{self.kind} {peer_addr_str}, ' - f'{self.session_manager.session_count():,d} total') + # self.logger.info(f'{self.kind} {self.peer_address_str()}, {self.session_manager.session_count():,d} total') def connection_lost(self, exc): """Handle client disconnection."""