Skip to content
This repository has been archived by the owner on Jul 13, 2023. It is now read-only.

Commit

Permalink
fix: prefer the LogBeginner observer entry point
Browse files Browse the repository at this point in the history
otherwise its buffer pointlessly keeps around 65k old log message
events for replaying

fixes #786
  • Loading branch information
pjenvey committed Jan 19, 2017
1 parent a5d1bb1 commit dc64f8a
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 2 deletions.
23 changes: 22 additions & 1 deletion autopush/logging.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import sys
import time
import threading
from typing import Any # noqa

import boto3
import raven
Expand All @@ -17,6 +18,7 @@
from twisted.logger import (
formatEvent,
formatEventAsClassicLogText,
globalLogBeginner,
globalLogPublisher,
LogLevel,
ILogObserver
Expand Down Expand Up @@ -53,6 +55,25 @@
])


# whether the global LogBeginner.beginLoggingTo has been called: it
# should only be called once
began_logging = False


def begin_or_register(observer):
"""Register observer with the global LogPublisher
Registers via the global LogBeginner the first time called.
"""
# type: (Any) -> None
global began_logging
if not began_logging:
globalLogBeginner.beginLoggingTo([observer], redirectStandardIO=False)
began_logging = True
else:
globalLogPublisher.addObserver(observer)


@implementer(ILogObserver)
class PushLogger(object):
"""Twisted LogObserver implementation
Expand Down Expand Up @@ -182,7 +203,7 @@ def start(self):
self._output = io.open(self._filename, "a", encoding="utf-8")
if self.firehose:
self.firehose.start()
globalLogPublisher.addObserver(self)
begin_or_register(self)

def stop(self):
globalLogPublisher.removeObserver(self)
Expand Down
3 changes: 2 additions & 1 deletion autopush/tests/test_integration.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
get_month,
has_connected_this_month
)
from autopush.logging import begin_or_register
from autopush.main import endpoint_paths
from autopush.settings import AutopushSettings
from autopush.utils import base64url_encode
Expand Down Expand Up @@ -372,7 +373,7 @@ def setUp(self):
)

self.logs = TestingLogObserver()
globalLogPublisher.addObserver(self.logs)
begin_or_register(self.logs)

router_table = os.environ.get("ROUTER_TABLE", "router_int_test")
storage_table = os.environ.get("STORAGE_TABLE", "storage_int_test")
Expand Down

0 comments on commit dc64f8a

Please sign in to comment.