Use module loggers rather than the root logger. Exceptions caused by bad clients shouldn't cause ERROR level logging. Fix sql logging to use 'repr' rather than 'str'

pull/23/head
Mark Haines 2014-11-20 17:10:37 +00:00
parent 32090aee16
commit dfdda2c871
8 changed files with 17 additions and 10 deletions

View File

@ -17,6 +17,8 @@
import logging import logging
logger = logging.getLogger(__name__)
class Codes(object): class Codes(object):
UNAUTHORIZED = "M_UNAUTHORIZED" UNAUTHORIZED = "M_UNAUTHORIZED"
@ -38,7 +40,7 @@ class CodeMessageException(Exception):
"""An exception with integer code and message string attributes.""" """An exception with integer code and message string attributes."""
def __init__(self, code, msg): def __init__(self, code, msg):
logging.error("%s: %s, %s", type(self).__name__, code, msg) logger.info("%s: %s, %s", type(self).__name__, code, msg)
super(CodeMessageException, self).__init__("%d: %s" % (code, msg)) super(CodeMessageException, self).__init__("%d: %s" % (code, msg))
self.code = code self.code = code
self.msg = msg self.msg = msg
@ -140,7 +142,8 @@ def cs_exception(exception):
if isinstance(exception, CodeMessageException): if isinstance(exception, CodeMessageException):
return exception.error_dict() return exception.error_dict()
else: else:
logging.error("Unknown exception type: %s", type(exception)) logger.error("Unknown exception type: %s", type(exception))
return {}
def cs_error(msg, code=Codes.UNKNOWN, **kwargs): def cs_error(msg, code=Codes.UNKNOWN, **kwargs):

View File

@ -83,6 +83,8 @@ class SynapseEvent(JsonEncodedObject):
"content", "content",
] ]
outlier = False
def __init__(self, raises=True, **kwargs): def __init__(self, raises=True, **kwargs):
super(SynapseEvent, self).__init__(**kwargs) super(SynapseEvent, self).__init__(**kwargs)
# if "content" in kwargs: # if "content" in kwargs:

View File

@ -116,7 +116,7 @@ class SynapseHomeServer(HomeServer):
# extra resources to existing nodes. See self._resource_id for the key. # extra resources to existing nodes. See self._resource_id for the key.
resource_mappings = {} resource_mappings = {}
for (full_path, resource) in desired_tree: for (full_path, resource) in desired_tree:
logging.info("Attaching %s to path %s", resource, full_path) logger.info("Attaching %s to path %s", resource, full_path)
last_resource = self.root_resource last_resource = self.root_resource
for path_seg in full_path.split('/')[1:-1]: for path_seg in full_path.split('/')[1:-1]:
if not path_seg in last_resource.listNames(): if not path_seg in last_resource.listNames():
@ -221,12 +221,12 @@ def setup():
db_name = hs.get_db_name() db_name = hs.get_db_name()
logging.info("Preparing database: %s...", db_name) logger.info("Preparing database: %s...", db_name)
with sqlite3.connect(db_name) as db_conn: with sqlite3.connect(db_name) as db_conn:
prepare_database(db_conn) prepare_database(db_conn)
logging.info("Database prepared in %s.", db_name) logger.info("Database prepared in %s.", db_name)
hs.get_db_pool() hs.get_db_pool()

View File

@ -30,7 +30,7 @@ logger = logging.getLogger(__name__)
def check_event_content_hash(event, hash_algorithm=hashlib.sha256): def check_event_content_hash(event, hash_algorithm=hashlib.sha256):
"""Check whether the hash for this PDU matches the contents""" """Check whether the hash for this PDU matches the contents"""
computed_hash = _compute_content_hash(event, hash_algorithm) computed_hash = _compute_content_hash(event, hash_algorithm)
logging.debug("Expecting hash: %s", encode_base64(computed_hash.digest())) logger.debug("Expecting hash: %s", encode_base64(computed_hash.digest()))
if computed_hash.name not in event.hashes: if computed_hash.name not in event.hashes:
raise SynapseError( raise SynapseError(
400, 400,

View File

@ -138,7 +138,7 @@ class JsonResource(HttpServer, resource.Resource):
) )
except CodeMessageException as e: except CodeMessageException as e:
if isinstance(e, SynapseError): if isinstance(e, SynapseError):
logger.error("%s SynapseError: %s - %s", request, e.code, logger.info("%s SynapseError: %s - %s", request, e.code,
e.msg) e.msg)
else: else:
logger.exception(e) logger.exception(e)

View File

@ -508,7 +508,7 @@ def prepare_database(db_conn):
"new for the server to understand" "new for the server to understand"
) )
elif user_version < SCHEMA_VERSION: elif user_version < SCHEMA_VERSION:
logging.info( logger.info(
"Upgrading database from version %d", "Upgrading database from version %d",
user_version user_version
) )

View File

@ -57,7 +57,7 @@ class LoggingTransaction(object):
if args and args[0]: if args and args[0]:
values = args[0] values = args[0]
sql_logger.debug( sql_logger.debug(
"[SQL values] {%s} " + ", ".join(("<%s>",) * len(values)), "[SQL values] {%s} " + ", ".join(("<%r>",) * len(values)),
self.name, self.name,
*values *values
) )

View File

@ -1,6 +1,8 @@
import threading import threading
import logging import logging
logger = logging.getLogger(__name__)
class LoggingContext(object): class LoggingContext(object):
"""Additional context for log formatting. Contexts are scoped within a """Additional context for log formatting. Contexts are scoped within a
@ -53,7 +55,7 @@ class LoggingContext(object):
None to avoid suppressing any exeptions that were thrown. None to avoid suppressing any exeptions that were thrown.
""" """
if self.thread_local.current_context is not self: if self.thread_local.current_context is not self:
logging.error( logger.error(
"Current logging context %s is not the expected context %s", "Current logging context %s is not the expected context %s",
self.thread_local.current_context, self.thread_local.current_context,
self self