Skip to content

Commit

Permalink
Merge pull request #313 from mattbennett/logging-config
Browse files Browse the repository at this point in the history
Configure logging via dictConfig and config.yaml
  • Loading branch information
mattbennett committed May 11, 2016
2 parents 9381e99 + 1f60ea0 commit 5dd191c
Show file tree
Hide file tree
Showing 5 changed files with 85 additions and 16 deletions.
1 change: 1 addition & 0 deletions CHANGES
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ Version 2.3.1
Released PENDING

* Deprecate ``MethodProxy.async`` in favour of ``MethodProxy.call_async`` in preparation for async becoming a keyword
* Add support for loading logging configuration from ``config.yaml``

Version 2.3.0
-------------
Expand Down
16 changes: 11 additions & 5 deletions docs/cli.rst
Original file line number Diff line number Diff line change
Expand Up @@ -24,17 +24,23 @@ and providing a simple YAML configuration file:
.. code-block:: yaml
# foobar.yaml
AMQP_URI: 'amqp://guest:guest@localhost'
WEB_SERVER_ADDRESS: '0.0.0.0:8000'
rpc_exchange: 'nameko-rpc'
max_workers: 10
parent_calls_tracked: 10
LOGGING:
version: 1
handlers:
console:
class: logging.StreamHandler
root:
level: DEBUG
handlers: [console]
The ``LOGGING`` entry is passed to :func:`logging.config.dictConfig` and should conform to the schema for that call.


Interacting with running services
Expand Down
25 changes: 15 additions & 10 deletions nameko/cli/run.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,14 @@
import errno
import inspect
import logging
import logging.config
import os
import re
import signal
import sys
import yaml

import yaml
from eventlet import backdoor

from nameko.constants import AMQP_URI_CONFIG_KEY
from nameko.exceptions import CommandError
from nameko.extensions import ENTRYPOINT_EXTENSIONS_ATTR
Expand Down Expand Up @@ -160,23 +160,28 @@ def shutdown(signum, frame):


def main(args):
logging.basicConfig(level=logging.INFO, format='%(message)s')

if '.' not in sys.path:
sys.path.insert(0, '.')

if args.config:
with open(args.config) as fle:
config = yaml.load(fle)
else:
config = {
AMQP_URI_CONFIG_KEY: args.broker
}

if "LOGGING" in config:
logging.config.dictConfig(config['LOGGING'])
else:
logging.basicConfig(level=logging.INFO, format='%(message)s')

services = []
for path in args.services:
services.extend(
import_service(path)
)

if args.config:
with open(args.config) as fle:
config = yaml.load(fle)
else:
config = {AMQP_URI_CONFIG_KEY: args.broker}

run(services, config, backdoor_port=args.backdoor_port)


Expand Down
53 changes: 53 additions & 0 deletions test/cli/test_run.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from os.path import join, dirname, abspath
import eventlet
from mock import patch
from textwrap import dedent
import pytest

from nameko.cli.main import setup_parser
Expand Down Expand Up @@ -66,6 +67,58 @@ def test_main_with_config(rabbit_config):
}


def test_main_with_logging_config(rabbit_config, tmpdir):

config = """
AMQP_URI: {amqp_uri}
LOGGING:
version: 1
disable_existing_loggers: false
formatters:
simple:
format: "%(name)s - %(levelname)s - %(message)s"
handlers:
capture:
class: logging.FileHandler
level: INFO
formatter: simple
filename: {capture_file}
root:
level: INFO
handlers: [capture]
"""

capture_file = tmpdir.join('capture.log')

config_file = tmpdir.join('config.yaml')
config_file.write(
dedent(config.format(
capture_file=capture_file.strpath,
amqp_uri=rabbit_config['AMQP_URI']
))
)

parser = setup_parser()
args = parser.parse_args([
'run',
'--config',
config_file.strpath,
'test.sample',
])

gt = eventlet.spawn(main, args)
eventlet.sleep(1)

with ClusterRpcProxy(rabbit_config) as proxy:
proxy.service.ping()

pid = os.getpid()
os.kill(pid, signal.SIGTERM)
gt.wait()

assert "test.sample - INFO - ping!" in capture_file.read()


def test_import_ok():
assert import_service('test.sample') == [Service]
assert import_service('test.sample:Service') == [Service]
Expand Down
6 changes: 5 additions & 1 deletion test/sample.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
import logging

from nameko.rpc import rpc

log = logging.getLogger('test.sample')


class Service(object):
name = "service"

@rpc
def ping(self):
pass
log.info('ping!')

0 comments on commit 5dd191c

Please sign in to comment.