summary refs log tree commit diff
diff options
context:
space:
mode:
authorDavid Baker <dave@matrix.org>2015-04-30 16:17:27 +0100
committerDavid Baker <dave@matrix.org>2015-04-30 16:21:38 +0100
commit054aa0d58c22ae76d3e094fc2fd6495456ffd2cf (patch)
tree5e2f589123200c2495a08a9119335025772bbeb6
parentDon't use sub queries, it makes postgres sad (diff)
downloadsynapse-054aa0d58c22ae76d3e094fc2fd6495456ffd2cf.tar.xz
Do access log using python's logging stuff, just under a separate logger name
Diffstat (limited to '')
-rwxr-xr-xsynapse/app/homeserver.py42
-rw-r--r--synapse/config/logger.py11
2 files changed, 39 insertions, 14 deletions
diff --git a/synapse/app/homeserver.py b/synapse/app/homeserver.py
index 0aa5c34c81..3ce5fa4a43 100755
--- a/synapse/app/homeserver.py
+++ b/synapse/app/homeserver.py
@@ -35,7 +35,7 @@ from twisted.enterprise import adbapi
 from twisted.web.resource import Resource
 from twisted.web.static import File
 from twisted.web.server import Site
-from twisted.web.http import proxiedLogFormatter
+from twisted.web.http import proxiedLogFormatter, combinedLogFormatter
 from synapse.http.server import JsonResource, RootRedirect
 from synapse.rest.media.v0.content_repository import ContentRepoResource
 from synapse.rest.media.v1.media_repository import MediaRepositoryResource
@@ -226,17 +226,13 @@ class SynapseHomeServer(HomeServer):
     def start_listening(self):
         config = self.get_config()
 
-        log_formatter = None
-        if config.captcha_ip_origin_is_x_forwarded:
-            log_formatter = proxiedLogFormatter
-
         if not config.no_tls and config.bind_port is not None:
             reactor.listenSSL(
                 config.bind_port,
-                Site(
+                SynapseSite(
+                    "synapse.access.https",
+                    config,
                     self.root_resource,
-                    logPath=config.access_log_file,
-                    logFormatter=log_formatter,
                 ),
                 self.tls_context_factory,
                 interface=config.bind_host
@@ -246,10 +242,10 @@ class SynapseHomeServer(HomeServer):
         if config.unsecure_port is not None:
             reactor.listenTCP(
                 config.unsecure_port,
-                Site(
+                SynapseSite(
+                    "synapse.access.http",
+                    config,
                     self.root_resource,
-                    logPath=config.access_log_file,
-                    logFormatter=log_formatter,
                 ),
                 interface=config.bind_host
             )
@@ -259,10 +255,10 @@ class SynapseHomeServer(HomeServer):
         if metrics_resource and config.metrics_port is not None:
             reactor.listenTCP(
                 config.metrics_port,
-                Site(
+                SynapseSite(
+                    "synapse.access.metrics",
+                    config,
                     metrics_resource,
-                    logPath=config.access_log_file,
-                    logFormatter=log_formatter,
                 ),
                 interface="127.0.0.1",
             )
@@ -484,6 +480,24 @@ class SynapseService(service.Service):
         return self._port.stopListening()
 
 
+class SynapseSite(Site):
+    """
+    Subclass of a twisted http Site that does access logging with python's
+    standard logging
+    """
+    def __init__(self, logger_name, config, resource, *args, **kwargs):
+        Site.__init__(self, resource, *args, **kwargs)
+        if config.captcha_ip_origin_is_x_forwarded:
+            self._log_formatter = proxiedLogFormatter
+        else:
+            self._log_formatter = combinedLogFormatter
+        self.access_logger = logging.getLogger(logger_name)
+
+    def log(self, request):
+        line = self._log_formatter(self._logDateTime, request)
+        self.access_logger.info(line)
+
+
 def run(hs):
 
     def in_thread():
diff --git a/synapse/config/logger.py b/synapse/config/logger.py
index 559cbe7963..077f20497a 100644
--- a/synapse/config/logger.py
+++ b/synapse/config/logger.py
@@ -83,6 +83,17 @@ class LoggingConfig(Config):
             handler.addFilter(LoggingContextFilter(request=""))
 
             logger.addHandler(handler)
+
+            if self.access_log_file:
+                access_logger = logging.getLogger('synapse.access')
+                # we log to both files by default
+                access_logger.propagate = 1
+                access_log_handler = logging.handlers.RotatingFileHandler(
+                    self.access_log_file, maxBytes=(1000 * 1000 * 100), backupCount=3
+                )
+                access_log_formatter = logging.Formatter('%(message)s')
+                access_log_handler.setFormatter(access_log_formatter)
+                access_logger.addHandler(access_log_handler)
         else:
             with open(self.log_config, 'r') as f:
                 logging.config.dictConfig(yaml.load(f))