Skip to content

Commit

Permalink
v1.0.7 - Customize how messages are routed to the queues.
Browse files Browse the repository at this point in the history
  • Loading branch information
albertomr86 committed May 16, 2017
1 parent a9e741f commit 261bcf2
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 43 deletions.
28 changes: 15 additions & 13 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -90,19 +90,20 @@ When create the handler, you're able to specify different parameters in order to
## Configuration
These are the configuration allowed:

| Parameter | Description | Default |
|-------------------|------------------------------------------------------------------------------------------------------------------------------------------|---------------------------------------|
| host | RabbitMQ Server hostname or ip address. | localhost |
| port | RabbitMQ Server port. | 5672 |
| username | Username for authentication. | None |
| password | Provide a password for the username. | None |
| exchange | Name of the exchange to publish the logs. This exchange is considered of type topic. | log |
| declare_exchange | Whether or not to declare the exchange. | False |
| connection_params | Allow extra params to connect with RabbitMQ. | None |
| formatter | Use custom formatter for the logs. | python_logging_rabbitmq.JSONFormatter |
| close_after_emit | Close the active connection after send a log. A new connection is open for the next log. | False |
| fields | Dict to add as a field in each logs send to RabbitMQ. This is useful when you want fields in each log but without pass them every time. | None |
| fields_under_root | When is True, each key in parameter 'fields' will be added as an entry in the log, otherwise they will be logged under the key 'fields'. | True |
| Parameter | Description | Default |
|--------------------|------------------------------------------------------------------------------------------------------------------------------------------|---------------------------------------|
| host | RabbitMQ Server hostname or ip address. | localhost |
| port | RabbitMQ Server port. | 5672 |
| username | Username for authentication. | None |
| password | Provide a password for the username. | None |
| exchange | Name of the exchange to publish the logs. This exchange is considered of type topic. | log |
| declare_exchange | Whether or not to declare the exchange. | False |
| routing_key_format | Customize how messages are routed to the queues. | {name}.{level} |
| connection_params | Allow extra params to connect with RabbitMQ. | None |
| formatter | Use custom formatter for the logs. | python_logging_rabbitmq.JSONFormatter |
| close_after_emit | Close the active connection after send a log. A new connection is open for the next log. | False |
| fields | Dict to add as a field in each logs send to RabbitMQ. This is useful when you want fields in each log but without pass them every time. | None |
| fields_under_root | When is True, each key in parameter 'fields' will be added as an entry in the log, otherwise they will be logged under the key 'fields'. | True |


### Examples
Expand Down Expand Up @@ -273,6 +274,7 @@ LOGGING = {
## Releases
| Date | Version | Notes |
|--------------|---------|----------------------------------------------------------|
| May 15, 2017 | 1.0.7 | Adding support to customize the routing_key (Thanks to [@hansyulian](https://github.com/hansyulian)). |
| Mar 30, 2017 | 1.0.6 | Fix compatibility with python3 in RabbitMQHandlerOneWay (by [@sactre](https://github.com/sactre)). |
| Mar 28, 2017 | 1.0.5 | Explicit local imports. |
| Mar 16, 2017 | 1.0.4 | Added new handler RabbitMQHandlerOneWay (by [@wallezhang](https://github.com/wallezhang)). |
Expand Down
2 changes: 1 addition & 1 deletion python_logging_rabbitmq/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@
from .handlers import RabbitMQHandler # noqa: F401
from .handlers_oneway import RabbitMQHandlerOneWay # noqa: F401

__version__ = '1.0.6'
__version__ = '1.0.7'
30 changes: 16 additions & 14 deletions python_logging_rabbitmq/handlers.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,23 +16,24 @@ def __init__(self, level=logging.NOTSET, formatter=JSONFormatter(),
host='localhost', port=5672, connection_params=None,
username=None, password=None,
exchange='log', declare_exchange=False,
close_after_emit=False,
routing_key_format="{name}.{level}", close_after_emit=False,
fields=None, fields_under_root=True):
"""
Initialize the handler.
:param level: Logs level.
:param formatter: Use custom formatter for the logs.
:param host: RabbitMQ host. Default localhost
:param port: RabbitMQ Port. Default 5672
:param connection_params: Allow extra params to connect with RabbitMQ.
:param username: Username in case of authentication.
:param password: Password for the username.
:param exchange: Send logs using this exchange.
:param declare_exchange: Whether or not to declare the exchange.
:param close_after_emit: Close connection after emit the record?
:param fields: Send these fields as part of all logs.
:param fields_under_root: Merge the fields in the root object.
:param level: Logs level.
:param formatter: Use custom formatter for the logs.
:param host: RabbitMQ host. Default localhost
:param port: RabbitMQ Port. Default 5672
:param connection_params: Allow extra params to connect with RabbitMQ.
:param username: Username in case of authentication.
:param password: Password for the username.
:param exchange: Send logs using this exchange.
:param declare_exchange: Whether or not to declare the exchange.
:param routing_key_format: Customize how messages will be routed to the queues.
:param close_after_emit: Close connection after emit the record?
:param fields: Send these fields as part of all logs.
:param fields_under_root: Merge the fields in the root object.
"""

super(RabbitMQHandler, self).__init__(level=level)
Expand All @@ -42,6 +43,7 @@ def __init__(self, level=logging.NOTSET, formatter=JSONFormatter(),
self.connection = None
self.channel = None
self.exchange_declared = not declare_exchange
self.routing_key_format = routing_key_format
self.close_after_emit = close_after_emit

# Connection parameters.
Expand Down Expand Up @@ -112,7 +114,7 @@ def emit(self, record):
if not self.connection or self.connection.is_closed or not self.channel or self.channel.is_closed:
self.open_connection()

routing_key = "{name}.{level}".format(name=record.name, level=record.levelname)
routing_key = self.routing_key_format.format(name=record.name, level=record.levelname)

self.channel.basic_publish(
exchange=self.exchange,
Expand Down
30 changes: 16 additions & 14 deletions python_logging_rabbitmq/handlers_oneway.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,23 +20,24 @@ def __init__(self, level=logging.NOTSET, formatter=JSONFormatter(),
host='localhost', port=5672, connection_params=None,
username=None, password=None,
exchange='log', declare_exchange=False,
close_after_emit=False,
routing_key_format="{name}.{level}", close_after_emit=False,
fields=None, fields_under_root=True):
"""
Initialize the handler.
:param level: Logs level.
:param formatter: Use custom formatter for the logs.
:param host: RabbitMQ host. Default localhost
:param port: RabbitMQ Port. Default 5672
:param connection_params: Allow extra params to connect with RabbitMQ.
:param username: Username in case of authentication.
:param password: Password for the username.
:param exchange: Send logs using this exchange.
:param declare_exchange: Whether or not to declare the exchange.
:param close_after_emit: Close connection after emit the record?
:param fields: Send these fields as part of all logs.
:param fields_under_root: Merge the fields in the root object.
:param level: Logs level.
:param formatter: Use custom formatter for the logs.
:param host: RabbitMQ host. Default localhost
:param port: RabbitMQ Port. Default 5672
:param connection_params: Allow extra params to connect with RabbitMQ.
:param username: Username in case of authentication.
:param password: Password for the username.
:param exchange: Send logs using this exchange.
:param declare_exchange: Whether or not to declare the exchange.
:param routing_key_format: Customize how messages will be routed to the queues.
:param close_after_emit: Close connection after emit the record?
:param fields: Send these fields as part of all logs.
:param fields_under_root: Merge the fields in the root object.
"""

super(RabbitMQHandlerOneWay, self).__init__(level=level)
Expand All @@ -46,6 +47,7 @@ def __init__(self, level=logging.NOTSET, formatter=JSONFormatter(),
self.connection = None
self.channel = None
self.exchange_declared = not declare_exchange
self.routing_key_format = routing_key_format
self.close_after_emit = close_after_emit

# Connection parameters.
Expand Down Expand Up @@ -146,7 +148,7 @@ def message_worker(self):

def emit(self, record):
try:
routing_key = "{name}.{level}".format(name=record.name, level=record.levelname)
routing_key = self.routing_key_format.format(name=record.name, level=record.levelname)
self.queue.put((record, routing_key))
except Exception:
self.channel, self.connection = None, None
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
]

setup(name='python-logging-rabbitmq',
version='1.0.6',
version='1.0.7',

url='https://github.com/albertomr86/python-logging-rabbitmq',
description='Send logs to RabbitMQ from Python/Django',
Expand Down

0 comments on commit 261bcf2

Please sign in to comment.