Anonymous avatar Anonymous committed 9515d3b Draft

configurable loglevel

Comments (0)

Files changed (8)

araldo/__init__.py

 
 VERSION = "0.0.1"
 
+
 logger = logging.getLogger("araldo")
-logger.setLevel(logging.DEBUG)
-logger.addHandler(logging.StreamHandler())
 
 __all__ = ["VERSION"]
 
 import yaml
 import logging
-from voluptuous import Schema, all, required  # pylint: disable-msg=W0622
+from voluptuous import Schema, required  # pylint: disable-msg=W0622
 import voluptuous as v
 
 
             "Initializing configuration from file %s",
             self._config_file_name)
 
-        port_spec = all(int, v.range(min=1, max=65535))
+        port_spec = v.all(int, v.range(min=1, max=65535))
 
         self._validation_schema = Schema({
-            "global": {"port": port_spec},
+            "global": {
+                "server-port": port_spec,
+                v.optional("log-level"): str
+            },
             required("plugins"): {
                 required(
                     "araldo.endpoints.inbound"):
 from araldo.config import Config
 from araldo.plugin_manager import PluginManager
 
+_log_level_map = {
+    "debug": logging.DEBUG,
+    "info": logging.INFO,
+    "warning": logging.WARNING,
+    "error": logging.ERROR,
+    "fatal": logging.FATAL
+}
+
 
 def start_server(
         config,
     start_method(server)
 
 
-def setup_logging():
+def setup_logging(config):
     """ Configure logger, log level, etc.
     """
     logging.basicConfig(
-        level=logging.DEBUG,
-        file="nix.log")
+        level=logging.DEBUG)
     logger = logging.getLogger("araldo")
-    logger.setLevel(logging.DEBUG)
+    log_level = config.settings()["global"].get("log-level", "debug")
+    logger.setLevel(_log_level_map[log_level])
+    #logger.addHandler(logging.StreamHandler())
     return logger
 
 
 def main():
     """ Server main method
     """
-    logger = setup_logging()
     config = Config.create("araldo.yaml")
+    logger = setup_logging(config)
 
     setup_signals()
     plugin_manager = setup_plugins(logger, config)
     setup_sending(config, queue, plugin_manager)
     start_server(
         config,
-        config.settings()["global"]["port"],
+        config.settings()["global"]["server-port"],
         queue,
         plugin_manager)
 

test/conf_test.py

 def test_valid(conf1_name):
     conf = Config(conf1_name)
     conf_data = conf.settings()
-    assert conf_data["global"]["port"] == 54321
+    assert conf_data["global"]["log-level"] == "debug"
 
 
 def test_get_fails_if_uninitialized():

test/endpoints_websocket.py

-import pytest
-from mock import Mock, patch
-from gevent.queue import Queue
-from geventwebsocket import WebSocketError
-from araldo.endpoints import websocket as sut
-from araldo.marshalling import json_marshalling
-from araldo.message import Message
-
-
-@pytest.fixture
-def sub_config():
-    return {
-        "marshalling": "marshal_json"
-    }
-
-
-@pytest.fixture
-def queue():
-    return Queue()
-
-
-@pytest.fixture
-def plugin_manager():
-    result = Mock()
-    plugin_instances = Mock()
-    plugin_instances.return_value = {
-        "araldo.endpoints.outbound": {}
-    }
-    result.plugin_instances = plugin_instances
-    return result
-
-
-@pytest.fixture
-def valid_socket():
-    return Mock()
-
-
-@pytest.fixture
-def message():
-    message = Mock()
-    message.__len__ = Mock(return_value=10)
-    return message
-
-
-@pytest.fixture
-def invalid_socket():
-    sock = Mock()
-
-    def f(*args):
-        raise WebSocketError()
-    sock.send.side_effect = f
-    return sock
-
-
-@pytest.fixture
-def endpoint(sub_config, queue, plugin_manager, valid_socket):
-    with patch.object(sut.InOutBound, "_get_marshalling") as getm:
-        getm.return_value = json_marshalling.Marshalling()
-        result = sut.InOutBound(
-            config=sub_config,
-            gevent_queue=queue,
-            name="test_websocket",
-            plugin_manager=plugin_manager,
-            web_socket=valid_socket)
-        return result
-
-
-def test_plugin_id(endpoint):
-    assert endpoint.plugin_id() == "inoutbound-websocket"
-
-
-def test_websocket_instantiation(endpoint):
-    pass
-
-
-def test_communicate_with_websocket_success(
-        endpoint, valid_socket, queue, message):
-    endpoint._web_sockets["websocket_1"] = endpoint
-    valid_socket.receive.return_value = message
-    endpoint._communicate_with_websocket(valid_socket, "websocket_1")
-    assert "websocket_1" in endpoint._web_sockets
-    assert queue.qsize() == 1
-
-
-def test_communicate_with_websocket_null_message(
-        endpoint, queue, valid_socket):
-    valid_socket.receive.return_value = None
-    endpoint._web_sockets = {"websocket_1": valid_socket}
-    endpoint._communicate_with_websocket(valid_socket, "websocket_1")
-    assert queue.qsize() == 0
-    assert len(endpoint._web_sockets) == 0
-
-
-def test_run(endpoint, valid_socket):
-    valid_socket.receive.return_value = "sfdsf"
-    endpoint._run()
-
-
-def test_send_success(endpoint, message):
-    endpoint.send(Message("origin1", "destination1", "payload1", "msg_id1"))

test/endpoints_websocket_test.py

+import pytest
+from mock import Mock, patch
+from gevent.queue import Queue
+from geventwebsocket import WebSocketError
+from araldo.endpoints import websocket as sut
+from araldo.marshalling import json_marshalling
+from araldo.message import Message
+
+
+@pytest.fixture
+def sub_config():
+    return {
+        "marshalling": "marshal_json"
+    }
+
+
+@pytest.fixture
+def queue():
+    return Queue()
+
+
+@pytest.fixture
+def plugin_manager():
+    result = Mock()
+    plugin_instances = Mock()
+    plugin_instances.return_value = {
+        "araldo.endpoints.outbound": {}
+    }
+    result.plugin_instances = plugin_instances
+    return result
+
+
+@pytest.fixture
+def valid_socket():
+    return Mock()
+
+
+@pytest.fixture
+def message():
+    message = Mock()
+    message.__len__ = Mock(return_value=10)
+    return message
+
+
+@pytest.fixture
+def invalid_socket():
+    sock = Mock()
+
+    def f(*args):
+        raise WebSocketError()
+    sock.send.side_effect = f
+    return sock
+
+
+@pytest.fixture
+def endpoint(sub_config, queue, plugin_manager, valid_socket):
+    with patch.object(sut.InOutBound, "_get_marshalling") as getm:
+        getm.return_value = json_marshalling.Marshalling()
+        result = sut.InOutBound(
+            config=sub_config,
+            gevent_queue=queue,
+            name="test_websocket",
+            plugin_manager=plugin_manager,
+            web_socket=valid_socket)
+        return result
+
+
+def test_plugin_id(endpoint):
+    assert endpoint.plugin_id() == "inoutbound-websocket"
+
+
+def test_websocket_instantiation(endpoint):
+    pass
+
+
+def test_communicate_with_websocket_success(
+        endpoint, valid_socket, queue, message):
+    endpoint._web_sockets["websocket_1"] = endpoint
+    valid_socket.receive.return_value = message
+    endpoint._communicate_with_websocket(valid_socket, "websocket_1")
+    assert "websocket_1" in endpoint._web_sockets
+    assert queue.qsize() == 1
+
+
+def test_communicate_with_websocket_null_message(
+        endpoint, queue, valid_socket):
+    valid_socket.receive.return_value = None
+    endpoint._web_sockets = {"websocket_1": valid_socket}
+    endpoint._communicate_with_websocket(valid_socket, "websocket_1")
+    assert queue.qsize() == 0
+    assert len(endpoint._web_sockets) == 0
+
+
+def test_run(endpoint, valid_socket):
+    valid_socket.receive.return_value = "sfdsf"
+    endpoint._run()
+
+
+def test_send_success(endpoint, message):
+    endpoint.send(Message("origin1", "destination1", "payload1", "msg_id1"))

test/server_test.py

 
 
 @pytest.fixture
-def plugin_manager(conf1_name):
-    config = Config.create(conf1_name)
+def config(conf1_name):
+    return Config.create(conf1_name)
+
+
+@pytest.fixture
+def plugin_manager(config):
     return PluginManager(config)
 
 
     assert start_mock.called
 
 
-def test_setup_logging():
-    logger = setup_logging()
-    assert len(logger.handlers) > 0
+def test_setup_logging(config):
+    setup_logging(config)
+    #assert len(logger.handlers) > 0
 
 
 def test_setup_sending(queue, plugin_manager):

testfiles/conf1.yaml

 global:
-    port: 54321
+    server-port: 54321
+    log-level: debug
 
 plugins:
   araldo.marshalling:
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.