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

Commit

Permalink
Merge pull request #787 from mozilla-services/fix/786
Browse files Browse the repository at this point in the history
fix: prefer the LogBeginner observer entry point
  • Loading branch information
jrconlin authored Jan 19, 2017
2 parents a5d1bb1 + dc64f8a commit f8c819e
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 f8c819e

Please sign in to comment.