summaryrefslogtreecommitdiff
path: root/src/leap/bitmask
diff options
context:
space:
mode:
authorIvan Alejandro <ivanalejandro0@gmail.com>2015-06-05 13:36:51 -0300
committerIvan Alejandro <ivanalejandro0@gmail.com>2015-06-22 15:52:37 -0300
commit584a6d93ad1fe1ba46929108f002a16a8b70e95d (patch)
tree966ad07fdbe9cff29911dacbcdc750f75a92bd87 /src/leap/bitmask
parent8752f7f03a04ca7fa1169885adc9dbfce8bebbd4 (diff)
[feat] add a zmq based logger, change log window
- Add a new handler for a zmq/thread-safe log send between components. - Update the log window to use this new handler. - Remove old custom handler We have implemented a new handler that uses logbook, so this one is no longer needed. - Adapt log silencer to new handler - Use log file always as default - Related: #6733
Diffstat (limited to 'src/leap/bitmask')
-rw-r--r--src/leap/bitmask/app.py11
-rw-r--r--src/leap/bitmask/config/flags.py4
-rw-r--r--src/leap/bitmask/gui/logwindow.py (renamed from src/leap/bitmask/gui/loggerwindow.py)175
-rw-r--r--src/leap/bitmask/logs/__init__.py6
-rw-r--r--src/leap/bitmask/logs/leap_log_handler.py137
-rw-r--r--src/leap/bitmask/logs/log_silencer.py10
-rw-r--r--src/leap/bitmask/logs/safezmqhandler.py118
-rw-r--r--src/leap/bitmask/logs/tests/test_leap_log_handler.py120
-rw-r--r--src/leap/bitmask/logs/utils.py87
-rw-r--r--src/leap/bitmask/util/leap_argparse.py3
10 files changed, 306 insertions, 365 deletions
diff --git a/src/leap/bitmask/app.py b/src/leap/bitmask/app.py
index 731c168c..adab8652 100644
--- a/src/leap/bitmask/app.py
+++ b/src/leap/bitmask/app.py
@@ -124,7 +124,6 @@ def start_app():
options = {
'start_hidden': opts.start_hidden,
'debug': opts.debug,
- 'log_file': opts.log_file,
}
flags.STANDALONE = opts.standalone
@@ -137,7 +136,13 @@ def start_app():
flags.CA_CERT_FILE = opts.ca_cert_file
- replace_stdout = True
+ flags.DEBUG = opts.debug
+
+ logger = get_logger()
+
+ # NOTE: since we are not using this right now, the code that replaces the
+ # stdout needs to be reviewed when we enable this again
+ # replace_stdout = True
# XXX mail repair commands disabled for now
# if opts.repair or opts.import_maildir:
@@ -145,8 +150,6 @@ def start_app():
# this could be more generic with a Command class.
# replace_stdout = False
- logger = create_logger(opts.debug, opts.log_file, replace_stdout)
-
# ok, we got logging in place, we can satisfy mail plumbing requests
# and show logs there. it normally will exit there if we got that path.
# XXX mail repair commands disabled for now
diff --git a/src/leap/bitmask/config/flags.py b/src/leap/bitmask/config/flags.py
index cdde1971..1cf1d15a 100644
--- a/src/leap/bitmask/config/flags.py
+++ b/src/leap/bitmask/config/flags.py
@@ -58,3 +58,7 @@ SKIP_WIZARD_CHECKS = False
# This flag tells us whether the current pyzmq supports using CurveZMQ or not.
ZMQ_HAS_CURVE = None
+
+# Store the needed loglevel globally since the logger handlers goes through
+# threads and processes
+DEBUG = False
diff --git a/src/leap/bitmask/gui/loggerwindow.py b/src/leap/bitmask/gui/logwindow.py
index 463d2412..123f14cc 100644
--- a/src/leap/bitmask/gui/loggerwindow.py
+++ b/src/leap/bitmask/gui/logwindow.py
@@ -1,5 +1,5 @@
# -*- coding: utf-8 -*-
-# loggerwindow.py
+# logwindow.py
# Copyright (C) 2013 LEAP
#
# This program is free software: you can redistribute it and/or modify
@@ -19,18 +19,96 @@
History log window
"""
import cgi
-import logging
from PySide import QtCore, QtGui
+import logbook
+from logbook.queues import ZeroMQSubscriber
+
from ui_loggerwindow import Ui_LoggerWindow
+from leap.bitmask.logs import LOG_FORMAT
+from leap.bitmask.logs.utils import get_logger
from leap.bitmask.util.constants import PASTEBIN_API_DEV_KEY
-from leap.bitmask.logs.leap_log_handler import LeapLogHandler
from leap.bitmask.util import pastebin
-from leap.common.check import leap_assert, leap_assert_type
-logger = logging.getLogger(__name__)
+logger = get_logger()
+
+# log history global variable used to store received logs through different
+# opened instances of this window
+_LOGS_HISTORY = []
+
+
+class QtLogHandler(logbook.Handler, logbook.StringFormatterHandlerMixin):
+ """
+ Custom log handler which emits a log record with the message properly
+ formatted using a Qt Signal.
+ """
+
+ class _QtSignaler(QtCore.QObject):
+ """
+ inline class used to hold the `new_log` Signal, if this is used
+ directly in the outside class it fails due how PySide works.
+
+ This is the message we get if not use this method:
+ TypeError: Error when calling the metaclass bases
+ metaclass conflict: the metaclass of a derived class must be a
+ (non-strict) subclass of the metaclasses of all its bases
+
+ """
+ new_log = QtCore.Signal(object)
+
+ def emit(self, data):
+ """
+ emit the `new_log` Signal with the given `data` parameter.
+
+ :param data: the data to emit along with the signal.
+ :type data: object
+ """
+ # WARNING: the new-style connection does NOT work because PySide
+ # translates the emit method to self.emit, and that collides with
+ # the emit method for logging.Handler
+ # self.new_log.emit(log_item)
+ QtCore.QObject.emit(self, QtCore.SIGNAL('new_log(PyObject)'), data)
+
+ def __init__(self, level=logbook.NOTSET, format_string=None,
+ encoding=None, filter=None, bubble=False):
+
+ logbook.Handler.__init__(self, level, filter, bubble)
+ logbook.StringFormatterHandlerMixin.__init__(self, format_string)
+
+ self.qt = self._QtSignaler()
+
+ def __enter__(self):
+ return logbook.Handler.__enter__(self)
+
+ def __exit__(self, exc_type, exc_value, tb):
+ return logbook.Handler.__exit__(self, exc_type, exc_value, tb)
+
+ def emit(self, record):
+ """
+ Emit the specified logging record using a Qt Signal.
+ Also add it to the history in order to be able to access it later.
+
+ :param record: the record to emit
+ :type record: logbook.LogRecord
+ """
+ global _LOGS_HISTORY
+ record.msg = self.format(record)
+ # NOTE: not optimal approach, we may want to look at
+ # bisect.insort with a custom approach to use key or
+ # http://code.activestate.com/recipes/577197-sortedcollection/
+ # Sort logs on arrival, logs transmitted over zmq may arrive unsorted.
+ _LOGS_HISTORY.append(record)
+ _LOGS_HISTORY = sorted(_LOGS_HISTORY, key=lambda r: r.time)
+
+ # XXX: emitting the record on arrival does not allow us to sort here so
+ # in the GUI the logs may arrive with with some time sort problem.
+ # We should implement a sort-on-arrive for the log window.
+ # Maybe we should switch to a tablewidget item that sort automatically
+ # by timestamp.
+ # As a user workaround you can close/open the log window
+ self.qt.emit(record)
class LoggerWindow(QtGui.QDialog):
@@ -40,16 +118,11 @@ class LoggerWindow(QtGui.QDialog):
_paste_ok = QtCore.Signal(object)
_paste_error = QtCore.Signal(object)
- def __init__(self, parent, handler):
+ def __init__(self, parent):
"""
- Initialize the widget with the custom handler.
-
- :param handler: Custom handler that supports history and signal.
- :type handler: LeapLogHandler.
+ Initialize the widget.
"""
QtGui.QDialog.__init__(self, parent)
- leap_assert(handler, "We need a handler for the logger window")
- leap_assert_type(handler, LeapLogHandler)
# Load UI
self.ui = Ui_LoggerWindow()
@@ -72,36 +145,38 @@ class LoggerWindow(QtGui.QDialog):
self._current_filter = ""
self._current_history = ""
- # Load logging history and connect logger with the widget
- self._logging_handler = handler
- self._connect_to_handler()
+ self._set_logs_to_display()
+
+ self._my_handler = QtLogHandler(format_string=LOG_FORMAT)
+ self._my_handler.qt.new_log.connect(self._add_log_line)
+
self._load_history()
+ self._connect_to_logbook()
- def _connect_to_handler(self):
+ def _connect_to_logbook(self):
"""
- This method connects the loggerwindow with the handler through a
- signal communicate the logger events.
+ Run in the background the log receiver.
"""
- self._logging_handler.new_log.connect(self._add_log_line)
+ subscriber = ZeroMQSubscriber('tcp://127.0.0.1:5000', multi=True)
+ self._logbook_controller = subscriber.dispatch_in_background(
+ self._my_handler)
def _add_log_line(self, log):
"""
Adds a line to the history, only if it's in the desired levels to show.
:param log: a log record to be inserted in the widget
- :type log: a dict with RECORD_KEY and MESSAGE_KEY.
- the record contains the LogRecord of the logging module,
- the message contains the formatted message for the log.
+ :type log: Logbook.LogRecord.
"""
html_style = {
- logging.DEBUG: "background: #CDFFFF;",
- logging.INFO: "background: white;",
- logging.WARNING: "background: #FFFF66;",
- logging.ERROR: "background: red; color: white;",
- logging.CRITICAL: "background: red; color: white; font: bold;"
+ logbook.DEBUG: "background: #CDFFFF;",
+ logbook.INFO: "background: white;",
+ logbook.WARNING: "background: #FFFF66;",
+ logbook.ERROR: "background: red; color: white;",
+ logbook.CRITICAL: "background: red; color: white; font: bold;"
}
- level = log[LeapLogHandler.RECORD_KEY].levelno
- message = cgi.escape(log[LeapLogHandler.MESSAGE_KEY])
+ level = log.level
+ message = cgi.escape(log.msg)
if self._logs_to_display[level]:
open_tag = "<tr style='" + html_style[level] + "'>"
@@ -125,12 +200,10 @@ class LoggerWindow(QtGui.QDialog):
"""
self._set_logs_to_display()
self.ui.txtLogHistory.clear()
- history = self._logging_handler.log_history
current_history = []
- for line in history:
- self._add_log_line(line)
- message = line[LeapLogHandler.MESSAGE_KEY]
- current_history.append(message)
+ for record in _LOGS_HISTORY:
+ self._add_log_line(record)
+ current_history.append(record.msg)
self._current_history = "\n".join(current_history)
@@ -139,11 +212,11 @@ class LoggerWindow(QtGui.QDialog):
Sets the logs_to_display dict getting the toggled options from the ui
"""
self._logs_to_display = {
- logging.DEBUG: self.ui.btnDebug.isChecked(),
- logging.INFO: self.ui.btnInfo.isChecked(),
- logging.WARNING: self.ui.btnWarning.isChecked(),
- logging.ERROR: self.ui.btnError.isChecked(),
- logging.CRITICAL: self.ui.btnCritical.isChecked()
+ logbook.DEBUG: self.ui.btnDebug.isChecked(),
+ logbook.INFO: self.ui.btnInfo.isChecked(),
+ logbook.WARNING: self.ui.btnWarning.isChecked(),
+ logbook.ERROR: self.ui.btnError.isChecked(),
+ logbook.CRITICAL: self.ui.btnCritical.isChecked()
}
def _filter_by(self, text):
@@ -261,3 +334,27 @@ class LoggerWindow(QtGui.QDialog):
self._paste_thread = QtCore.QThread()
self._paste_thread.run = lambda: do_pastebin()
self._paste_thread.start()
+
+ def closeEvent(self, e):
+ """
+ Disconnect logger on close.
+ """
+ self._disconnect_logger()
+ e.accept()
+
+ def reject(self):
+ """
+ Disconnect logger on reject.
+ """
+ self._disconnect_logger()
+ QtGui.QDialog.reject(self)
+
+ def _disconnect_logger(self):
+ """
+ Stop the background thread that receives messages through zmq, also
+ close the subscriber socket.
+ This allows us to re-create the subscriber when we reopen this window
+ without getting an error at trying to connect twice to the zmq port.
+ """
+ self._logbook_controller.stop()
+ self._logbook_controller.subscriber.close()
diff --git a/src/leap/bitmask/logs/__init__.py b/src/leap/bitmask/logs/__init__.py
index 0516b304..837a5ed9 100644
--- a/src/leap/bitmask/logs/__init__.py
+++ b/src/leap/bitmask/logs/__init__.py
@@ -1,3 +1,3 @@
-# levelname length == 8, since 'CRITICAL' is the longest
-LOG_FORMAT = ('%(asctime)s - %(levelname)-8s - '
- 'L#%(lineno)-4s : %(name)s:%(funcName)s() - %(message)s')
+LOG_FORMAT = (u'[{record.time:%Y-%m-%d %H:%M:%S}] '
+ u'{record.level_name: <8} - L#{record.lineno: <4} : '
+ u'{record.module}:{record.func_name} - {record.message}')
diff --git a/src/leap/bitmask/logs/leap_log_handler.py b/src/leap/bitmask/logs/leap_log_handler.py
deleted file mode 100644
index 24141638..00000000
--- a/src/leap/bitmask/logs/leap_log_handler.py
+++ /dev/null
@@ -1,137 +0,0 @@
-# -*- coding: utf-8 -*-
-# leap_log_handler.py
-# Copyright (C) 2013 LEAP
-#
-# This program is free software: you can redistribute it and/or modify
-# it under the terms of the GNU General Public License as published by
-# the Free Software Foundation, either version 3 of the License, or
-# (at your option) any later version.
-#
-# This program is distributed in the hope that it will be useful,
-# but WITHOUT ANY WARRANTY; without even the implied warranty of
-# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
-# GNU General Public License for more details.
-#
-# You should have received a copy of the GNU General Public License
-# along with this program. If not, see <http://www.gnu.org/licenses/>.
-"""
-Custom handler for the logger window.
-"""
-import logging
-
-from PySide import QtCore
-
-from leap.bitmask.logs import LOG_FORMAT
-
-
-class LogHandler(logging.Handler):
- """
- This is the custom handler that implements our desired formatting
- and also keeps a history of all the logged events.
- """
-
- MESSAGE_KEY = 'message'
- RECORD_KEY = 'record'
-
- def __init__(self, qtsignal):
- """
- LogHander initialization.
- Calls parent method and keeps a reference to the qtsignal
- that will be used to fire the gui update.
- """
- # TODO This is going to eat lots of memory after some time.
- # Should be pruned at some moment.
- self._log_history = []
-
- logging.Handler.__init__(self)
- self._qtsignal = qtsignal
-
- def _get_format(self, logging_level):
- """
- Sets the log format depending on the parameter.
- It uses html and css to set the colors for the logs.
-
- :param logging_level: the debug level to define the color.
- :type logging_level: str.
- """
- formatter = logging.Formatter(LOG_FORMAT)
- return formatter
-
- def emit(self, logRecord):
- """
- This method is fired every time that a record is logged by the
- logging module.
- This method reimplements logging.Handler.emit that is fired
- in every logged message.
-
- :param logRecord: the record emitted by the logging module.
- :type logRecord: logging.LogRecord.
- """
- self.setFormatter(self._get_format(logRecord.levelname))
- log = self.format(logRecord)
- log_item = {self.RECORD_KEY: logRecord, self.MESSAGE_KEY: log}
- self._log_history.append(log_item)
- self._qtsignal(log_item)
-
-
-class HandlerAdapter(object):
- """
- New style class that accesses all attributes from the LogHandler.
-
- Used as a workaround for a problem with multiple inheritance with Pyside
- that surfaced under OSX with pyside 1.1.0.
- """
- MESSAGE_KEY = 'message'
- RECORD_KEY = 'record'
-
- def __init__(self, qtsignal):
- self._handler = LogHandler(qtsignal=qtsignal)
-
- def setLevel(self, *args, **kwargs):
- return self._handler.setLevel(*args, **kwargs)
-
- def addFilter(self, *args, **kwargs):
- return self._handler.addFilter(*args, **kwargs)
-
- def handle(self, *args, **kwargs):
- return self._handler.handle(*args, **kwargs)
-
- @property
- def level(self):
- return self._handler.level
-
-
-class LeapLogHandler(QtCore.QObject, HandlerAdapter):
- """
- Custom logging handler. It emits Qt signals so it can be plugged to a gui.
-
- Its inner handler also stores an history of logs that can be fetched after
- having been connected to a gui.
- """
- # All dicts returned are of the form
- # {'record': LogRecord, 'message': str}
- new_log = QtCore.Signal(dict)
-
- def __init__(self):
- """
- LeapLogHandler initialization.
- Initializes parent classes.
- """
- QtCore.QObject.__init__(self)
- HandlerAdapter.__init__(self, qtsignal=self.qtsignal)
-
- def qtsignal(self, log_item):
- # WARNING: the new-style connection does NOT work because PySide
- # translates the emit method to self.emit, and that collides with
- # the emit method for logging.Handler
- # self.new_log.emit(log_item)
- QtCore.QObject.emit(
- self,
- QtCore.SIGNAL('new_log(PyObject)'), log_item)
-
- @property
- def log_history(self):
- """
- Returns the history of the logged messages.
- """
- return self._handler._log_history
diff --git a/src/leap/bitmask/logs/log_silencer.py b/src/leap/bitmask/logs/log_silencer.py
index 56b290e4..540532cb 100644
--- a/src/leap/bitmask/logs/log_silencer.py
+++ b/src/leap/bitmask/logs/log_silencer.py
@@ -17,14 +17,13 @@
"""
Filter for leap logs.
"""
-import logging
import os
import re
from leap.bitmask.util import get_path_prefix
-class SelectiveSilencerFilter(logging.Filter):
+class SelectiveSilencerFilter(object):
"""
Configurable filter for root leap logger.
@@ -75,7 +74,7 @@ class SelectiveSilencerFilter(logging.Filter):
return map(lambda line: re.sub('\s', '', line),
lines)
- def filter(self, record):
+ def filter(self, record, handler):
"""
Implements the filter functionality for this Filter
@@ -86,7 +85,10 @@ class SelectiveSilencerFilter(logging.Filter):
"""
if not self.rules:
return True
- logger_path = record.name
+ logger_path = record.module
+ if logger_path is None:
+ return True
+
for path in self.rules:
if logger_path.startswith(path):
return False
diff --git a/src/leap/bitmask/logs/safezmqhandler.py b/src/leap/bitmask/logs/safezmqhandler.py
new file mode 100644
index 00000000..7aac6a6a
--- /dev/null
+++ b/src/leap/bitmask/logs/safezmqhandler.py
@@ -0,0 +1,118 @@
+# -*- coding: utf-8 -*-
+# safezmqhandler.py
+# Copyright (C) 2013, 2014, 2015 LEAP
+#
+# This program is free software: you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation, either version 3 of the License, or
+# (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program. If not, see <http://www.gnu.org/licenses/>.
+"""
+A thread-safe zmq handler for LogBook.
+"""
+import json
+import threading
+
+from logbook.queues import ZeroMQHandler
+from logbook import NOTSET
+
+import zmq
+
+
+class SafeZMQHandler(ZeroMQHandler):
+ """
+ A ZMQ log handler for LogBook that is thread-safe.
+
+ This log handler makes use of the existing zmq handler and if the user
+ tries to log something from a different thread than the one used to
+ create the handler a new socket is created for that thread.
+
+ Note: In ZMQ, Contexts are threadsafe objects, but Sockets are not.
+ """
+
+ def __init__(self, uri=None, level=NOTSET, filter=None, bubble=False,
+ context=None, multi=False):
+
+ ZeroMQHandler.__init__(self, uri, level, filter, bubble, context,
+ multi)
+
+ current_id = self._get_caller_id()
+ # we store the socket created on the parent
+ self._sockets = {current_id: self.socket}
+
+ # store the settings for new socket creation
+ self._multi = multi
+ self._uri = uri
+
+ def _get_caller_id(self):
+ """
+ Return an id for the caller that depends on the current thread.
+ Thanks to this we can detect if we are running in a thread different
+ than the one who created the socket and create a new one for it.
+
+ :rtype: int
+ """
+ # NOTE it makes no sense to use multiprocessing id since the sockets
+ # list can't/shouldn't be shared between processes. We only use
+ # thread id. The user needs to make sure that the handler is created
+ # inside each process.
+ return threading.current_thread().ident
+
+ def _get_new_socket(self):
+ """
+ Return a new socket using the `uri` and `multi` parameters given in the
+ constructor.
+
+ :rtype: zmq.Socket
+ """
+ socket = None
+
+ if self._multi:
+ socket = self.context.socket(zmq.PUSH)
+ if self._uri is not None:
+ socket.connect(self._uri)
+ else:
+ socket = self.context.socket(zmq.PUB)
+ if self._uri is not None:
+ socket.bind(self._uri)
+
+ return socket
+
+ def emit(self, record):
+ """
+ Emit the given `record` through the socket.
+
+ :param record: the record to emit
+ :type record: Logbook.LogRecord
+ """
+ current_id = self._get_caller_id()
+ socket = None
+
+ if current_id in self._sockets:
+ socket = self._sockets[current_id]
+ else:
+ # TODO: create new socket
+ socket = self._get_new_socket()
+ self._sockets[current_id] = socket
+
+ socket.send(json.dumps(self.export_record(record)).encode("utf-8"))
+
+ def close(self, linger=-1):
+ """
+ Close all the sockets and linger `linger` time.
+
+ This reimplements the ZeroMQHandler.close method that is used by
+ context methods.
+
+ :param linger: time to linger, -1 to not to.
+ :type linger: int
+ """
+ for socket in self._sockets.values():
+ socket.close(linger)
diff --git a/src/leap/bitmask/logs/tests/test_leap_log_handler.py b/src/leap/bitmask/logs/tests/test_leap_log_handler.py
deleted file mode 100644
index 20b09aef..00000000
--- a/src/leap/bitmask/logs/tests/test_leap_log_handler.py
+++ /dev/null
@@ -1,120 +0,0 @@
-# -*- coding: utf-8 -*-
-# test_leap_log_handler.py
-# Copyright (C) 2013 LEAP
-#
-# This program is free software: you can redistribute it and/or modify
-# it under the terms of the GNU General Public License as published by
-# the Free Software Foundation, either version 3 of the License, or
-# (at your option) any later version.
-#
-# This program is distributed in the hope that it will be useful,
-# but WITHOUT ANY WARRANTY; without even the implied warranty of
-# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
-# GNU General Public License for more details.
-#
-# You should have received a copy of the GNU General Public License
-# along with this program. If not, see <http://www.gnu.org/licenses/>.
-"""
-tests for leap_log_handler
-"""
-try:
- import unittest2 as unittest
-except ImportError:
- import unittest
-
-import logging
-
-from leap.bitmask.logs.leap_log_handler import LeapLogHandler
-from leap.bitmask.util.pyside_tests_helper import BasicPySlotCase
-from leap.common.testing.basetest import BaseLeapTest
-
-from mock import Mock
-
-
-class LeapLogHandlerTest(BaseLeapTest, BasicPySlotCase):
- """
- LeapLogHandlerTest's tests.
- """
- def _callback(self, *args):
- """
- Simple callback to track if a signal was emitted.
- """
- self.called = True
- self.emitted_msg = args[0][LeapLogHandler.MESSAGE_KEY]
-
- def setUp(self):
- BasicPySlotCase.setUp(self)
-
- # Create the logger
- level = logging.DEBUG
- self.logger = logging.getLogger(name='test')
- self.logger.setLevel(level)
-
- # Create the handler
- self.leap_handler = LeapLogHandler()
- self.leap_handler.setLevel(level)
- self.logger.addHandler(self.leap_handler)
-
- def tearDown(self):
- BasicPySlotCase.tearDown(self)
- try:
- self.leap_handler.new_log.disconnect()
- except Exception:
- pass
-
- def test_history_starts_empty(self):
- self.assertEqual(self.leap_handler.log_history, [])
-
- def test_one_log_captured(self):
- self.logger.debug('test')
- self.assertEqual(len(self.leap_handler.log_history), 1)
-
- def test_history_records_order(self):
- self.logger.debug('test 01')
- self.logger.debug('test 02')
- self.logger.debug('test 03')
-
- logs = []
- for message in self.leap_handler.log_history:
- logs.append(message[LeapLogHandler.RECORD_KEY].msg)
-
- self.assertIn('test 01', logs)
- self.assertIn('test 02', logs)
- self.assertIn('test 03', logs)
-
- def test_history_messages_order(self):
- self.logger.debug('test 01')
- self.logger.debug('test 02')
- self.logger.debug('test 03')
-
- logs = []
- for message in self.leap_handler.log_history:
- logs.append(message[LeapLogHandler.MESSAGE_KEY])
-
- self.assertIn('test 01', logs[0])
- self.assertIn('test 02', logs[1])
- self.assertIn('test 03', logs[2])
-
- def test_emits_signal(self):
- log_format = '%(name)s - %(levelname)s - %(message)s'
- formatter = logging.Formatter(log_format)
- get_format = Mock(return_value=formatter)
- self.leap_handler._handler._get_format = get_format
-
- self.leap_handler.new_log.connect(self._callback)
- self.logger.debug('test')
-
- expected_log_msg = "test - DEBUG - test"
-
- # signal emitted
- self.assertTrue(self.called)
-
- # emitted message
- self.assertEqual(self.emitted_msg, expected_log_msg)
-
- # Mock called
- self.assertTrue(get_format.called)
-
-
-if __name__ == "__main__":
- unittest.main()
diff --git a/src/leap/bitmask/logs/utils.py b/src/leap/bitmask/logs/utils.py
index 8367937a..72efae97 100644
--- a/src/leap/bitmask/logs/utils.py
+++ b/src/leap/bitmask/logs/utils.py
@@ -1,80 +1,57 @@
import logging
import sys
+from leap.bitmask.config import flags
from leap.bitmask.logs import LOG_FORMAT
from leap.bitmask.logs.log_silencer import SelectiveSilencerFilter
-from leap.bitmask.logs.leap_log_handler import LeapLogHandler
+from leap.bitmask.logs.safezmqhandler import SafeZMQHandler
from leap.bitmask.logs.streamtologger import StreamToLogger
from leap.bitmask.platform_init import IS_WIN
+import logbook
+from logbook.more import ColorizedStderrHandler
-def create_logger(debug=False, logfile=None, replace_stdout=True):
- """
- Create the logger and attach the handlers.
-
- :param debug: the level of the messages that we should log
- :type debug: bool
- :param logfile: the file name of where we should to save the logs
- :type logfile: str
- :return: the new logger with the attached handlers.
- :rtype: logging.Logger
- """
- # TODO: get severity from command line args
- if debug:
- level = logging.DEBUG
- else:
- level = logging.WARNING
- # Create logger and formatter
- logger = logging.getLogger(name='leap')
- logger.setLevel(level)
- formatter = logging.Formatter(LOG_FORMAT)
+def get_logger(debug=True, logfile=None, replace_stdout=True):
+ level = logbook.WARNING
+ if flags.DEBUG:
+ level = logbook.NOTSET
- # Console handler
- try:
- import coloredlogs
- console = coloredlogs.ColoredStreamHandler(level=level)
- except ImportError:
- console = logging.StreamHandler()
- console.setLevel(level)
- console.setFormatter(formatter)
- using_coloredlog = False
- else:
- using_coloredlog = True
-
- if using_coloredlog:
- replace_stdout = False
+ # This handler consumes logs not handled by the others
+ null_handler = logbook.NullHandler(bubble=False)
+ null_handler.push_application()
silencer = SelectiveSilencerFilter()
- console.addFilter(silencer)
- logger.addHandler(console)
- logger.debug('Console handler plugged!')
- # LEAP custom handler
- leap_handler = LeapLogHandler()
- leap_handler.setLevel(level)
- leap_handler.addFilter(silencer)
- logger.addHandler(leap_handler)
- logger.debug('Leap handler plugged!')
+ zmq_handler = SafeZMQHandler('tcp://127.0.0.1:5000', multi=True,
+ level=level, filter=silencer.filter)
+ zmq_handler.push_application()
+
+ file_handler = logbook.FileHandler('bitmask.log', format_string=LOG_FORMAT,
+ bubble=True, filter=silencer.filter)
+ file_handler.push_application()
- # File handler
- if logfile is not None:
- logger.debug('Setting logfile to %s ', logfile)
- fileh = logging.FileHandler(logfile)
- fileh.setLevel(logging.DEBUG)
- fileh.setFormatter(formatter)
- fileh.addFilter(silencer)
- logger.addHandler(fileh)
- logger.debug('File handler plugged!')
+ # don't use simple stream, go for colored log handler instead
+ # stream_handler = logbook.StreamHandler(sys.stdout,
+ # format_string=LOG_FORMAT,
+ # bubble=True)
+ # stream_handler.push_application()
+ stream_handler = ColorizedStderrHandler(
+ level=level, format_string=LOG_FORMAT, bubble=True,
+ filter=silencer.filter)
+ stream_handler.push_application()
- if replace_stdout:
- replace_stdout_stderr_with_logging(logger)
+ logger = logbook.Logger('leap')
return logger
def replace_stdout_stderr_with_logging(logger):
"""
+ NOTE:
+ we are not using this right now (see commented lines on app.py),
+ this needs to be reviewed since the log handler has changed.
+
Replace:
- the standard output
- the standard error
diff --git a/src/leap/bitmask/util/leap_argparse.py b/src/leap/bitmask/util/leap_argparse.py
index 12fd9736..383e5f8a 100644
--- a/src/leap/bitmask/util/leap_argparse.py
+++ b/src/leap/bitmask/util/leap_argparse.py
@@ -38,9 +38,6 @@ def build_parser():
help='Displays Bitmask version and exits.')
# files
- parser.add_argument('-l', '--logfile', metavar="LOG FILE", nargs='?',
- action="store", dest="log_file",
- help='Optional log file.')
parser.add_argument('-m', '--mail-logfile',
metavar="MAIL LOG FILE", nargs='?',
action="store", dest="mail_log_file",