Python twisted.logger.Logger() Examples

The following are 28 code examples of twisted.logger.Logger(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module twisted.logger , or try the search function .
Example #1
Source File: tx.py    From txaio with MIT License 6 votes vote down vote up
def make_logger(level=None, logger=_Logger, observer=None):
    # we want the namespace to be the calling context of "make_logger"
    # -- so we *have* to pass namespace kwarg to Logger (or else it
    # will always say the context is "make_logger")
    cf = inspect.currentframe().f_back
    if "self" in cf.f_locals:
        # We're probably in a class init or method
        cls = cf.f_locals["self"].__class__
        namespace = '{0}.{1}'.format(cls.__module__, cls.__name__)
    else:
        namespace = cf.f_globals["__name__"]
        if cf.f_code.co_name != "<module>":
            # If it's not the module, and not in a class instance, add the code
            # object's name.
            namespace = namespace + "." + cf.f_code.co_name
    logger = Logger(level=level, namespace=namespace, logger=logger,
                    observer=observer)
    return logger 
Example #2
Source File: listener.py    From maas with GNU Affero General Public License v3.0 6 votes vote down vote up
def __init__(self, alias="default"):
        self.alias = alias
        self.listeners = defaultdict(list)
        self.autoReconnect = False
        self.connection = None
        self.connectionFileno = None
        self.notifications = set()
        self.notifier = task.LoopingCall(self.handleNotifies)
        self.notifierDone = None
        self.connecting = None
        self.disconnecting = None
        self.registeredChannels = set()
        self.channelRegistrar = task.LoopingCall(
            lambda: ensureDeferred(self.registerChannels())
        )
        self.channelRegistrarDone = None
        self.log = Logger(__name__, self)
        self.events = EventGroup("connected", "disconnected") 
Example #3
Source File: _twisted.py    From maas with GNU Affero General Public License v3.0 6 votes vote down vote up
def install(cls, module, attribute="log", *, source=None, observer=None):
        """Install a `LegacyLogger` at `module.attribute`.

        Warns if `module.attribute` does not exist, but carries on anyway.

        :param module: A module (or any other object with assignable
            attributes and a `__name__`).
        :param attribute: The name of the attribute on `module` to replace.
        :param source: See `Logger.__init__`.
        :param observer: See `Logger.__init__`.
        :return: The newly created `LegacyLogger`.
        """
        replacing = getattr(module, attribute, "<not-found>")
        warn_unless(
            replacing is twistedLegacy,
            (
                "Legacy logger being installed to replace %r but expected a "
                "reference to twisted.python.log module; please investigate!"
                % (replacing,)
            ),
        )
        logger = cls(module.__name__, source=source, observer=observer)
        setattr(module, attribute, logger)
        return logger 
Example #4
Source File: test_mailmail.py    From learn_python3_spider with MIT License 6 votes vote down vote up
def setUp(self):
        """
        Override some things in mailmail, so that we capture C{stdout},
        and do not call L{reactor.stop}.
        """
        self.out = NativeStringIO()
        # Override the mailmail logger, so we capture stderr output
        from twisted.logger import textFileLogObserver, Logger
        logObserver = textFileLogObserver(self.out)
        self.patch(mailmail, '_log', Logger(observer=logObserver))
        self.host = None
        self.options = None
        self.ident = None

        # Override mailmail.sendmail, so we don't call reactor.stop()
        def sendmail(host, options, ident):
            self.host = host
            self.options = options
            self.ident = ident
            return smtp.sendmail(host, options.sender, options.to,
                                 options.body, reactor=self.memoryReactor)

        self.patch(mailmail, 'sendmail', sendmail) 
Example #5
Source File: test_logging.py    From txaio with MIT License 6 votes vote down vote up
def test_log_converter(handler, framework):
    pytest.importorskip("twisted.logger")
    # this checks that we can convert a plain Twisted Logger calling
    # failure() into a traceback on our observers.
    from twisted.logger import Logger
    from txaio.tx import _LogObserver

    out = StringIO()
    observer = _LogObserver(out)
    logger = Logger(observer=observer)

    try:
        raise RuntimeError("failed on purpose")
    except RuntimeError:
        logger.failure(None)

    output = out.getvalue()
    assert "failed on purpose" in output
    assert "Traceback" in output 
Example #6
Source File: fcmv1client.py    From autopush with Mozilla Public License 2.0 6 votes vote down vote up
def __init__(self,
                 project_id,
                 service_cred_path=None,
                 logger=None,
                 metrics=None,
                 **options):
        self.project_id = project_id
        self.endpoint = ("https://fcm.googleapis.com/v1/"
                         "projects/{}/messages:send".format(self.project_id))

        self.token = None
        self.metrics = metrics
        self.logger = logger or Logger()
        self._options = options
        if service_cred_path:
            self.svc_cred = ServiceAccountCredentials.from_json_keyfile_name(
                service_cred_path,
                ["https://www.googleapis.com/auth/firebase.messaging"])
        self._sender = treq.post 
Example #7
Source File: fcm_v1.py    From autopush with Mozilla Public License 2.0 6 votes vote down vote up
def __init__(self, conf, router_conf, metrics):
        """Create a new FCM router and connect to FCM"""
        self.conf = conf
        self.router_conf = router_conf
        self.metrics = metrics
        self.min_ttl = router_conf.get("ttl", 60)
        self.dryRun = router_conf.get("dryrun", False)
        self.collapseKey = router_conf.get("collapseKey", "webpush")
        self.version = router_conf["version"]
        self.log = Logger()
        self.clients = {}
        try:
            for (sid, creds) in router_conf["creds"].items():
                self.clients[sid] = FCMv1(
                    project_id=creds["projectid"],
                    service_cred_path=creds["auth"],
                    logger=self.log,
                    metrics=self.metrics)
        except Exception as e:
            self.log.error("Could not instantiate FCMv1: missing credentials,",
                           ex=e)
            raise IOError("FCMv1 Bridge not initiated in main")
        self._base_tags = ["platform:fcmv1"]
        self.log.debug("Starting FCMv1 router...") 
Example #8
Source File: test_refresher.py    From bitmask-dev with GNU General Public License v3.0 6 votes vote down vote up
def test_log_error_if_fetch_by_fingerprint_returns_wrong_key(self):
        pgp = openpgp.OpenPGPScheme(
            self._soledad, gpgbinary=self.gpg_binary_path)
        km = self._key_manager()

        with patch.object(Logger, 'error') as mock_logger_error:
            rf = RandomRefreshPublicKey(pgp, km)
            rf._get_random_key = \
                Mock(return_value=defer.succeed(OpenPGPKey(
                    fingerprint=KEY_FINGERPRINT)))

            km._nicknym.fetch_key_with_fingerprint = \
                Mock(return_value=defer.succeed(PUBLIC_KEY_2))

            yield rf.maybe_refresh_key()

            mock_logger_error.assert_called_with(
                ERROR_UNEQUAL_FINGERPRINTS %
                (KEY_FINGERPRINT, KEY_FINGERPRINT_2)) 
Example #9
Source File: internet.py    From Safejumper-for-Desktop with GNU General Public License v2.0 6 votes vote down vote up
def __init__(self, endpoint, factory, retryPolicy, clock, log):
        """
        @see: L{ClientService.__init__}

        @param log: The logger for the L{ClientService} instance this state
            machine is associated to.
        @type log: L{Logger}
        """
        self._endpoint = endpoint
        self._failedAttempts = 0
        self._stopped = False
        self._factory = factory
        self._timeoutForAttempt = retryPolicy
        self._clock = clock
        self._connectionInProgress = succeed(None)

        self._awaitingConnected = []

        self._stopWaiters = []
        self._log = log 
Example #10
Source File: test_incoming_mail.py    From bitmask-dev with GNU General Public License v3.0 6 votes vote down vote up
def testFlagMessageOnBadJsonWhileDecrypting(self):
        doc = SoledadDocument()
        doc.doc_id = '1'
        doc.content = {'_enc_json': ''}

        err = ValueError('No JSON object could be decoded')

        def assert_failure():
            mock_logger_error.assert_any_call(
                'Error while decrypting 1')
            mock_logger_error.assert_any_call(
                'No JSON object could be decoded')
            self.assertEquals(doc.content['errdecr'], True)

        with patch.object(Logger, 'error') as mock_logger_error:
            with patch.object(utils, 'json_loads') as mock_json_loader:
                self.fetcher._update_incoming_message = Mock()
                mock_json_loader.side_effect = err

                self.fetcher._process_decrypted_doc(doc, '')

                assert_failure() 
Example #11
Source File: test_incoming_mail.py    From bitmask-dev with GNU General Public License v3.0 6 votes vote down vote up
def testLogErrorIfDecryptFails(self):

        def assert_failure(_):
            mock_logger_error.assert_any_call('_decrypt_doc: '
                                              'Error decrypting document with '
                                              'ID 1')

        with patch.object(Logger, 'error') as mock_logger_error:
            doc = SoledadDocument()
            doc.doc_id = '1'
            doc.content = {'_enc_json': ''}

            self.fetcher._process_decrypted_doc = Mock()
            self.km.decrypt = Mock(
                return_value=defer.fail(Exception()))

            d = self.fetcher._decrypt_doc(doc)
            d.addCallback(assert_failure)
            return d 
Example #12
Source File: internet.py    From learn_python3_spider with MIT License 5 votes vote down vote up
def __init__(self, endpoint, factory, retryPolicy, clock,
                 prepareConnection, log):
        """
        @see: L{ClientService.__init__}

        @param log: The logger for the L{ClientService} instance this state
            machine is associated to.
        @type log: L{Logger}

        @ivar _awaitingConnected: notifications to make when connection
            succeeds, fails, or is cancelled
        @type _awaitingConnected: list of (Deferred, count) tuples
        """
        self._endpoint = endpoint
        self._failedAttempts = 0
        self._stopped = False
        self._factory = factory
        self._timeoutForAttempt = retryPolicy
        self._clock = clock
        self._prepareConnection = prepareConnection
        self._connectionInProgress = succeed(None)

        self._awaitingConnected = []

        self._stopWaiters = []
        self._log = log 
Example #13
Source File: _logging.py    From maas with GNU Affero General Public License v3.0 5 votes vote down vote up
def configure_standard_logging(verbosity: int, mode: LoggingMode):
    """Configure the standard library's `logging` module.

    Get `logging` working with options consistent with Twisted. NOTE CAREFULLY
    that `django.utils.log.DEFAULT_LOGGING` may have been applied (though only
    if installed and if configured in this environment). Those settings and
    the settings this function applies must be mentally combined to understand
    the resultant behaviour.

    :param verbosity: See `get_logging_level`.
    :param mode: The mode in which to configure logging. See `LoggingMode`.
    """
    set_standard_verbosity(verbosity)
    # Make sure that `logging` is not configured to capture warnings.
    logging.captureWarnings(False)
    # If a logger is ever configured `propagate=False` but without handlers
    # `logging.Logger.callHandlers` will employ the `lastResort` handler in
    # order that the log is not lost. This goes to standard error by default.
    # Here we arrange for these situations to be logged more distinctively so
    # that they're easier to diagnose.
    logging.lastResort = logging.StreamHandler(
        twistedModern.LoggingFile(
            logger=twistedModern.Logger("lost+found"),
            level=twistedModern.LogLevel.error,
        )
    ) 
Example #14
Source File: _twisted.py    From maas with GNU Affero General Public License v3.0 5 votes vote down vote up
def show_warning_via_twisted(
    message, category, filename, lineno, file=None, line=None
):
    """Replacement for `warnings.showwarning` that logs via Twisted."""
    if file is None:
        # Try to find a module name with which to log this warning.
        module = get_module_for_file(filename)
        logger = twistedModern.Logger(
            "global" if module is None else module.__name__
        )
        # `message` is/can be an instance of `category`, so stringify.
        logger.warn(
            "{category}: {message}",
            message=str(message),
            category=category.__qualname__,
            filename=filename,
            lineno=lineno,
            line=line,
        )
    else:
        # It's not clear why and when `file` will be specified, but try to
        # honour the intention.
        warning = warnings.formatwarning(
            message, category, filename, lineno, line
        )
        try:
            file.write(warning)
            file.flush()
        except OSError:
            pass  # We tried.


# Those levels for which we should emit log events. 
Example #15
Source File: test_refresher.py    From bitmask-dev with GNU General Public License v3.0 5 votes vote down vote up
def test_start_refreshing(self):
        pgp = openpgp.OpenPGPScheme(
            self._soledad, gpgbinary=self.gpg_binary_path)

        with patch.object(Logger, 'debug') as mock_logger_start:
            rf = RandomRefreshPublicKey(pgp, self._key_manager())
            rf.start()
            mock_logger_start.assert_called_with(DEBUG_START_REFRESH)
            rf.stop()
            mock_logger_start.assert_called_with(DEBUG_STOP_REFRESH) 
Example #16
Source File: data_router.py    From Rasa_NLU_Chi with Apache License 2.0 5 votes vote down vote up
def _create_query_logger(response_log):
        """Create a logger that will persist incoming query results."""

        # Ensures different log files for different
        # processes in multi worker mode
        if response_log:
            # We need to generate a unique file name,
            # even in multiprocess environments
            timestamp = datetime.datetime.now().strftime('%Y%m%d-%H%M%S')
            log_file_name = "rasa_nlu_log-{}-{}.log".format(timestamp,
                                                            os.getpid())
            response_logfile = os.path.join(response_log, log_file_name)
            # Instantiate a standard python logger,
            # which we are going to use to log requests
            utils.create_dir_for_file(response_logfile)
            query_logger = Logger(
                observer=jsonFileLogObserver(
                        io.open(response_logfile, 'a', encoding='utf8'),
                        recordSeparator=''),
                namespace='query-logger')
            # Prevents queries getting logged with parent logger
            # --> might log them to stdout
            logger.info("Logging requests to '{}'.".format(response_logfile))
            return query_logger
        else:
            # If the user didn't provide a logging directory, we wont log!
            logger.info("Logging of requests is disabled. "
                        "(No 'request_log' directory configured)")
            return None 
Example #17
Source File: statusserver.py    From piqueserver with GNU General Public License v3.0 5 votes vote down vote up
def listen(self):
        """Starts the status server on configured host/port"""
        app = self.create_app()
        logger = Logger() if logging_option.get() else None
        log_class = AccessLogger if logging_option.get() else None
        runner = web.AppRunner(app,
                               access_log=logger,
                               access_log_class=log_class)
        await as_deferred(runner.setup())
        site = web.TCPSite(runner, host_option.get(), port_option.get())
        await as_deferred(site.start())

        # TODO: explain why we do this
        await Deferred() 
Example #18
Source File: periodic.py    From telepresence with Apache License 2.0 5 votes vote down vote up
def __init__(self, reactor):
        self.reactor = reactor
        self.log = Logger("Poll")
        pool = HTTPConnectionPool(reactor)
        pool._factory = QuietHTTP11ClientFactory
        self.agent = Agent(reactor, connectTimeout=10.0, pool=pool) 
Example #19
Source File: data_router.py    From rasa_nlu with Apache License 2.0 5 votes vote down vote up
def _create_query_logger(response_log):
        """Create a logger that will persist incoming query results."""

        # Ensures different log files for different
        # processes in multi worker mode
        if response_log:
            # We need to generate a unique file name,
            # even in multiprocess environments
            timestamp = datetime.datetime.now().strftime('%Y%m%d-%H%M%S')
            log_file_name = "rasa_nlu_log-{}-{}.log".format(timestamp,
                                                            os.getpid())
            response_logfile = os.path.join(response_log, log_file_name)
            # Instantiate a standard python logger,
            # which we are going to use to log requests
            utils.create_dir_for_file(response_logfile)
            out_file = io.open(response_logfile, 'a', encoding='utf8')
            # noinspection PyTypeChecker
            query_logger = Logger(
                observer=jsonFileLogObserver(out_file, recordSeparator=''),
                namespace='query-logger')
            # Prevents queries getting logged with parent logger
            # --> might log them to stdout
            logger.info("Logging requests to '{}'.".format(response_logfile))
            return query_logger
        else:
            # If the user didn't provide a logging directory, we wont log!
            logger.info("Logging of requests is disabled. "
                        "(No 'request_log' directory configured)")
            return None 
Example #20
Source File: twisted.py    From eliot with Apache License 2.0 5 votes vote down vote up
def __init__(self):
        self._logger = TwistedLogger(namespace="eliot") 
Example #21
Source File: websocket.py    From autopush with Mozilla Public License 2.0 5 votes vote down vote up
def log_exception(func):
    """Exception Logger Decorator for protocol methods"""
    @wraps(func)
    def wrapper(self, *args, **kwargs):
        try:
            return func(self, *args, **kwargs)
        except Exception:
            if self._log_exc:
                self.log_failure(failure.Failure())
            else:
                raise
    return wrapper 
Example #22
Source File: tcp.py    From learn_python3_spider with MIT License 5 votes vote down vote up
def __enter__(self):
        """
        Enter a log buffering context.

        @return: A logger that buffers log events.
        @rtype: L{Logger}.
        """
        return Logger(namespace=self._namespace, observer=self._logs.append) 
Example #23
Source File: logs.py    From worker with GNU General Public License v3.0 5 votes vote down vote up
def audit():
    outFile = sys.stdout if config.LOG_DIRECTORY == "stdout" else daily("audit.log")
    observer = textFileLogObserver(outFile=outFile)
    observer._encoding = "utf-8"
    return Logger(observer=observer) 
Example #24
Source File: test_protocol.py    From txamqp with Apache License 2.0 5 votes vote down vote up
def setUp(self):
        super(AMQClientTest, self).setUp()
        self.delegate = TwistedDelegate()
        self.clock = Clock()
        self.heartbeat = 1
        self.protocol = AMQClient(
            self.delegate, "/", load(DEFAULT_SPEC), clock=self.clock,
            heartbeat=self.heartbeat)
        self.transport = AMQPump(Logger())
        self.transport.connect(self.protocol) 
Example #25
Source File: udp.py    From kotori with GNU Affero General Public License v3.0 5 votes vote down vote up
def __init__(self, bus, topic, transform, logger):
        self.bus = bus
        self.topic = topic
        self.transform = to_list(transform)
        self.log = logger or Logger()

        # TODO sanity checks 
Example #26
Source File: test_web_base.py    From autopush with Mozilla Public License 2.0 5 votes vote down vote up
def setUp(self):
        from autopush.web.base import BaseWebHandler

        conf = AutopushConfig(
            hostname="localhost",
            statsd_host=None,
        )

        self.request_mock = Mock(body=b'', arguments={},
                                 headers={"ttl": "0"},
                                 host='example.com:8080')

        self.base = BaseWebHandler(
            EndpointHTTPFactory(conf, db=test_db(SinkMetrics()), routers=None),
            self.request_mock
        )
        self.status_mock = self.base.set_status = Mock()
        self.write_mock = self.base.write = Mock()
        self.base.log = Mock(spec=Logger)
        d = self.finish_deferred = Deferred()
        self.base.finish = lambda: d.callback(True)

        # Attach some common cors stuff for testing
        self.base.cors_methods = "POST,PUT"
        self.base.cors_request_headers = ["content-encoding", "encryption",
                                          "crypto-key", "ttl",
                                          "encryption-key", "content-type",
                                          "authorization"]
        self.base.cors_response_headers = ["location", "www-authenticate"] 
Example #27
Source File: test_websocket.py    From autopush with Mozilla Public License 2.0 5 votes vote down vote up
def setUp(self):
        from twisted.logger import Logger
        twisted.internet.base.DelayedCall.debug = True

        self.conf = conf = AutopushConfig(
            hostname="localhost",
            port=8080,
            statsd_host=None,
            env="test",
        )
        db = DatabaseManager.from_config(
            conf,
            resource=autopush.tests.boto_resource
        )
        self.metrics = db.metrics = Mock(spec=SinkMetrics)
        db.setup_tables()

        self.mock_agent = agent = Mock(spec=Agent)
        self.factory = PushServerFactory(conf, db, agent, {})
        self.proto = self.factory.buildProtocol(('localhost', 8080))
        self.proto._log_exc = False
        self.proto.log = Mock(spec=Logger)

        self.proto.debug = True

        self.proto.sendMessage = self.send_mock = Mock()
        self.orig_close = self.proto.sendClose
        request_mock = Mock(spec=ConnectionRequest)
        request_mock.headers = {}
        self.proto.ps = PushState.from_request(request=request_mock, db=db)
        self.proto.sendClose = self.close_mock = Mock()
        self.proto.transport = self.transport_mock = Mock()
        self.proto.closeHandshakeTimeout = 0
        self.proto.autoPingInterval = 300
        self.proto._force_retry = self.proto.force_retry 
Example #28
Source File: test_router.py    From autopush with Mozilla Public License 2.0 4 votes vote down vote up
def setUp(self, mt, mc):
        from twisted.logger import Logger
        conf = AutopushConfig(
            hostname="localhost",
            statsd_host=None,
        )
        apns_config = {
            'firefox':
                {'cert': 'fake.cert',
                 'key': 'fake.key',
                 'topic': 'com.example.SomeApp',
                 'max_connections': 2,
                 }
        }
        self.mock_connection = mc
        mc.return_value = mc
        self.metrics = metrics = Mock(spec=SinkMetrics)
        self.router = APNSRouter(conf, apns_config, metrics)
        self.mock_response = Mock()
        self.mock_response.status = 200
        mc.get_response.return_value = self.mock_response
        # toss the existing connection
        try:
            self.router.apns['firefox'].connections.pop()
        except IndexError:  # pragma nocover
            pass
        self.router.apns['firefox'].connections.append(
            self.mock_connection
        )
        self.router.apns['firefox'].log = Mock(spec=Logger)
        self.headers = {"content-encoding": "aesgcm",
                        "encryption": "test",
                        "encryption-key": "test"}
        self.notif = WebPushNotification(
            uaid=uuid.UUID(dummy_uaid),
            channel_id=uuid.UUID(dummy_chid),
            data="q60d6g",
            headers=self.headers,
            ttl=200,
            message_id=10,
        )
        self.notif.cleanup_headers()
        self.router_data = dict(router_data=dict(token="connect_data",
                                                 rel_channel="firefox"))