Create and use class_logger() utility function
This commit is contained in:
parent
3afa403a08
commit
1d664fa5d7
@ -8,9 +8,10 @@
|
|||||||
'''Class for server environment configuration and defaults.'''
|
'''Class for server environment configuration and defaults.'''
|
||||||
|
|
||||||
|
|
||||||
import logging
|
|
||||||
from os import environ
|
from os import environ
|
||||||
|
|
||||||
|
from electrumx.lib.util import class_logger
|
||||||
|
|
||||||
|
|
||||||
class EnvBase(object):
|
class EnvBase(object):
|
||||||
'''Wraps environment configuration.'''
|
'''Wraps environment configuration.'''
|
||||||
@ -19,8 +20,7 @@ class EnvBase(object):
|
|||||||
pass
|
pass
|
||||||
|
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
self.logger = logging.getLogger(__name__)\
|
self.logger = class_logger(__name__, self.__class__.__name__)
|
||||||
.getChild(self.__class__.__name__)
|
|
||||||
self.allow_root = self.boolean('ALLOW_ROOT', False)
|
self.allow_root = self.boolean('ALLOW_ROOT', False)
|
||||||
self.host = self.default('HOST', 'localhost')
|
self.host = self.default('HOST', 'localhost')
|
||||||
self.rpc_host = self.default('RPC_HOST', 'localhost')
|
self.rpc_host = self.default('RPC_HOST', 'localhost')
|
||||||
|
|||||||
@ -6,13 +6,14 @@
|
|||||||
# and warranty status of this software.
|
# and warranty status of this software.
|
||||||
|
|
||||||
import asyncio
|
import asyncio
|
||||||
import logging
|
|
||||||
import os
|
import os
|
||||||
import signal
|
import signal
|
||||||
import sys
|
import sys
|
||||||
import time
|
import time
|
||||||
from functools import partial
|
from functools import partial
|
||||||
|
|
||||||
|
from electrumx.lib.util import class_logger
|
||||||
|
|
||||||
|
|
||||||
class ServerBase(object):
|
class ServerBase(object):
|
||||||
'''Base class server implementation.
|
'''Base class server implementation.
|
||||||
@ -36,8 +37,7 @@ class ServerBase(object):
|
|||||||
'''Save the environment, perform basic sanity checks, and set the
|
'''Save the environment, perform basic sanity checks, and set the
|
||||||
event loop policy.
|
event loop policy.
|
||||||
'''
|
'''
|
||||||
self.logger = logging.getLogger(__name__)\
|
self.logger = class_logger(__name__, self.__class__.__name__)
|
||||||
.getChild(self.__class__.__name__)
|
|
||||||
self.env = env
|
self.env = env
|
||||||
|
|
||||||
# Sanity checks
|
# Sanity checks
|
||||||
|
|||||||
@ -38,6 +38,7 @@ from struct import pack, Struct
|
|||||||
|
|
||||||
# Logging utilities
|
# Logging utilities
|
||||||
|
|
||||||
|
|
||||||
class ConnectionLogger(logging.LoggerAdapter):
|
class ConnectionLogger(logging.LoggerAdapter):
|
||||||
'''Prepends a connection identifier to a logging message.'''
|
'''Prepends a connection identifier to a logging message.'''
|
||||||
def process(self, msg, kwargs):
|
def process(self, msg, kwargs):
|
||||||
@ -53,12 +54,19 @@ class CompactFormatter(logging.Formatter):
|
|||||||
|
|
||||||
|
|
||||||
def make_logger(name, *, handler, level):
|
def make_logger(name, *, handler, level):
|
||||||
|
'''Return the root ElectrumX logger.'''
|
||||||
logger = logging.getLogger(name)
|
logger = logging.getLogger(name)
|
||||||
logger.addHandler(handler)
|
logger.addHandler(handler)
|
||||||
logger.setLevel(logging.INFO)
|
logger.setLevel(logging.INFO)
|
||||||
logger.propagate = False
|
logger.propagate = False
|
||||||
return logger
|
return logger
|
||||||
|
|
||||||
|
|
||||||
|
def class_logger(path, classname):
|
||||||
|
'''Return a hierarchical logger for a class.'''
|
||||||
|
return logging.getLogger(path).getChild(classname)
|
||||||
|
|
||||||
|
|
||||||
# Method decorator. To be used for calculations that will always
|
# Method decorator. To be used for calculations that will always
|
||||||
# deliver the same result. The method cannot take any arguments
|
# deliver the same result. The method cannot take any arguments
|
||||||
# and should be accessed as an attribute.
|
# and should be accessed as an attribute.
|
||||||
|
|||||||
@ -11,14 +11,13 @@
|
|||||||
|
|
||||||
import array
|
import array
|
||||||
import asyncio
|
import asyncio
|
||||||
import logging
|
|
||||||
from struct import pack, unpack
|
from struct import pack, unpack
|
||||||
import time
|
import time
|
||||||
from functools import partial
|
from functools import partial
|
||||||
|
|
||||||
from electrumx.server.daemon import DaemonError
|
from electrumx.server.daemon import DaemonError
|
||||||
from electrumx.lib.hash import hash_to_str, HASHX_LEN
|
from electrumx.lib.hash import hash_to_str, HASHX_LEN
|
||||||
from electrumx.lib.util import chunks, formatted_time
|
from electrumx.lib.util import chunks, formatted_time, class_logger
|
||||||
import electrumx.server.db
|
import electrumx.server.db
|
||||||
|
|
||||||
|
|
||||||
@ -26,8 +25,7 @@ class Prefetcher(object):
|
|||||||
'''Prefetches blocks (in the forward direction only).'''
|
'''Prefetches blocks (in the forward direction only).'''
|
||||||
|
|
||||||
def __init__(self, bp):
|
def __init__(self, bp):
|
||||||
self.logger = logging.getLogger(__name__)\
|
self.logger = class_logger(__name__, self.__class__.__name__)
|
||||||
.getChild(self.__class__.__name__)
|
|
||||||
self.bp = bp
|
self.bp = bp
|
||||||
self.caught_up = False
|
self.caught_up = False
|
||||||
# Access to fetched_height should be protected by the semaphore
|
# Access to fetched_height should be protected by the semaphore
|
||||||
|
|||||||
@ -10,7 +10,6 @@ daemon.'''
|
|||||||
|
|
||||||
import asyncio
|
import asyncio
|
||||||
import json
|
import json
|
||||||
import logging
|
|
||||||
import time
|
import time
|
||||||
from calendar import timegm
|
from calendar import timegm
|
||||||
from struct import pack
|
from struct import pack
|
||||||
@ -18,7 +17,7 @@ from time import strptime
|
|||||||
|
|
||||||
import aiohttp
|
import aiohttp
|
||||||
|
|
||||||
from electrumx.lib.util import int_to_varint, hex_to_bytes
|
from electrumx.lib.util import int_to_varint, hex_to_bytes, class_logger
|
||||||
from electrumx.lib.hash import hex_str_to_hash
|
from electrumx.lib.hash import hex_str_to_hash
|
||||||
from aiorpcx import JSONRPC
|
from aiorpcx import JSONRPC
|
||||||
|
|
||||||
@ -37,8 +36,7 @@ class Daemon(object):
|
|||||||
'''Raised when the daemon returns an error in its results.'''
|
'''Raised when the daemon returns an error in its results.'''
|
||||||
|
|
||||||
def __init__(self, env):
|
def __init__(self, env):
|
||||||
self.logger = logging.getLogger(__name__)\
|
self.logger = class_logger(__name__, self.__class__.__name__)
|
||||||
.getChild(self.__class__.__name__)
|
|
||||||
self.coin = env.coin
|
self.coin = env.coin
|
||||||
self.set_urls(env.coin.daemon_urls(env.daemon_url))
|
self.set_urls(env.coin.daemon_urls(env.daemon_url))
|
||||||
self._height = None
|
self._height = None
|
||||||
|
|||||||
@ -11,7 +11,6 @@
|
|||||||
|
|
||||||
import array
|
import array
|
||||||
import ast
|
import ast
|
||||||
import logging
|
|
||||||
import os
|
import os
|
||||||
from struct import pack, unpack
|
from struct import pack, unpack
|
||||||
from bisect import bisect_right
|
from bisect import bisect_right
|
||||||
@ -42,8 +41,7 @@ class DB(object):
|
|||||||
'''Raised on general DB errors generally indicating corruption.'''
|
'''Raised on general DB errors generally indicating corruption.'''
|
||||||
|
|
||||||
def __init__(self, env):
|
def __init__(self, env):
|
||||||
self.logger = logging.getLogger(__name__)\
|
self.logger = util.class_logger(__name__, self.__class__.__name__)
|
||||||
.getChild(self.__class__.__name__)
|
|
||||||
self.env = env
|
self.env = env
|
||||||
self.coin = env.coin
|
self.coin = env.coin
|
||||||
|
|
||||||
|
|||||||
@ -13,7 +13,6 @@ import ast
|
|||||||
import bisect
|
import bisect
|
||||||
from collections import defaultdict
|
from collections import defaultdict
|
||||||
from functools import partial
|
from functools import partial
|
||||||
import logging
|
|
||||||
from struct import pack, unpack
|
from struct import pack, unpack
|
||||||
|
|
||||||
import electrumx.lib.util as util
|
import electrumx.lib.util as util
|
||||||
@ -25,8 +24,7 @@ class History(object):
|
|||||||
DB_VERSIONS = [0]
|
DB_VERSIONS = [0]
|
||||||
|
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
self.logger = logging.getLogger(__name__)\
|
self.logger = util.class_logger(__name__, self.__class__.__name__)
|
||||||
.getChild(self.__class__.__name__)
|
|
||||||
# For history compaction
|
# For history compaction
|
||||||
self.max_hist_row_entries = 12500
|
self.max_hist_row_entries = 12500
|
||||||
self.unflushed = defaultdict(partial(array.array, 'I'))
|
self.unflushed = defaultdict(partial(array.array, 'I'))
|
||||||
|
|||||||
@ -9,11 +9,11 @@
|
|||||||
|
|
||||||
import asyncio
|
import asyncio
|
||||||
import itertools
|
import itertools
|
||||||
import logging
|
|
||||||
import time
|
import time
|
||||||
from collections import defaultdict
|
from collections import defaultdict
|
||||||
|
|
||||||
from electrumx.lib.hash import hash_to_str, hex_str_to_hash
|
from electrumx.lib.hash import hash_to_str, hex_str_to_hash
|
||||||
|
from electrumx.lib.util import class_logger
|
||||||
from electrumx.server.daemon import DaemonError
|
from electrumx.server.daemon import DaemonError
|
||||||
from electrumx.server.db import UTXO
|
from electrumx.server.db import UTXO
|
||||||
|
|
||||||
@ -33,8 +33,7 @@ class MemPool(object):
|
|||||||
'''
|
'''
|
||||||
|
|
||||||
def __init__(self, bp, controller):
|
def __init__(self, bp, controller):
|
||||||
self.logger = logging.getLogger(__name__)\
|
self.logger = class_logger(__name__, self.__class__.__name__)
|
||||||
.getChild(self.__class__.__name__)
|
|
||||||
self.daemon = bp.daemon
|
self.daemon = bp.daemon
|
||||||
self.controller = controller
|
self.controller = controller
|
||||||
self.coin = bp.coin
|
self.coin = bp.coin
|
||||||
|
|||||||
@ -8,7 +8,6 @@
|
|||||||
'''Peer management.'''
|
'''Peer management.'''
|
||||||
|
|
||||||
import asyncio
|
import asyncio
|
||||||
import logging
|
|
||||||
import random
|
import random
|
||||||
import socket
|
import socket
|
||||||
import ssl
|
import ssl
|
||||||
@ -19,7 +18,7 @@ from functools import partial
|
|||||||
from aiorpcx import ClientSession, RPCError, SOCKSProxy, ConnectionError
|
from aiorpcx import ClientSession, RPCError, SOCKSProxy, ConnectionError
|
||||||
|
|
||||||
from electrumx.lib.peer import Peer
|
from electrumx.lib.peer import Peer
|
||||||
from electrumx.lib.util import ConnectionLogger
|
from electrumx.lib.util import ConnectionLogger, class_logger
|
||||||
|
|
||||||
|
|
||||||
PEER_GOOD, PEER_STALE, PEER_NEVER, PEER_BAD = range(4)
|
PEER_GOOD, PEER_STALE, PEER_NEVER, PEER_BAD = range(4)
|
||||||
@ -224,8 +223,7 @@ class PeerManager(object):
|
|||||||
Issues a 'peers.subscribe' RPC to them and tells them our data.
|
Issues a 'peers.subscribe' RPC to them and tells them our data.
|
||||||
'''
|
'''
|
||||||
def __init__(self, env, controller):
|
def __init__(self, env, controller):
|
||||||
self.logger = logging.getLogger(__name__)\
|
self.logger = class_logger(__name__, self.__class__.__name__)
|
||||||
.getChild(self.__class__.__name__)
|
|
||||||
# Initialise the Peer class
|
# Initialise the Peer class
|
||||||
Peer.DEFAULT_PORTS = env.coin.PEER_DEFAULT_PORTS
|
Peer.DEFAULT_PORTS = env.coin.PEER_DEFAULT_PORTS
|
||||||
self.env = env
|
self.env = env
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user