Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,7 @@ These are the configuration allowed:
| port | RabbitMQ Server port. | 5672 |
| username | Username for authentication. | None |
| password | Provide a password for the username. | None |
| url | AMQP url to use instead of host/port/username/password. | 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 |
| remove_request | If True (default), remove request & exc info. | True |
Expand All @@ -155,6 +156,7 @@ These are the configuration allowed:
#### RabbitMQ Connection

```python
# via host, port, username, password
rabbit = RabbitMQHandler(
host='localhost',
port=5672,
Expand All @@ -168,6 +170,13 @@ rabbit = RabbitMQHandler(
)
```

```python
# via url
rabbit = RabbitMQHandler(
url=amqps://guest:guest@example.host/vhost-example
)
```

#### Custom fields

```python
Expand Down
11 changes: 9 additions & 2 deletions python_logging_rabbitmq/handlers.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ class RabbitMQHandler(logging.Handler):
"""
def __init__(self, level=logging.NOTSET, formatter=None,
host='localhost', port=5672, connection_params=None,
username=None, password=None,
username=None, password=None, url=None,
exchange='log', declare_exchange=False,
remove_request=True,
routing_key_format="{name}.{level}",
Expand All @@ -36,6 +36,7 @@ def __init__(self, level=logging.NOTSET, formatter=None,
# :param message_headers: A dictionary of headers to be published with the message. Optional.
# :param username: Username in case of authentication.
# :param password: Password for the username.
# :param url: AMQP url to be used instead of username/password/host/port.
# :param exchange: Send logs using this exchange.
# :param declare_exchange: Whether or not to declare the exchange.
# :param remove_request: If true (default), remove request/exc info
Expand All @@ -56,6 +57,7 @@ def __init__(self, level=logging.NOTSET, formatter=None,
self.exchange = exchange
self.connection = None
self.channel = None
self._url = url
self.exchange_declared = not declare_exchange
self.remove_request = remove_request
self.routing_key_format = routing_key_format
Expand Down Expand Up @@ -106,7 +108,12 @@ def open_connection(self):
rabbitmq_logger.setLevel(logging.WARNING)

if not self.connection or self.connection.is_closed:
self.connection = pika.BlockingConnection(pika.ConnectionParameters(**self.connection_params))
parameters = None
if self._url:
parameters = pika.URLParameters(self._url)
else:
parameters = pika.ConnectionParameters(**self.connection_params)
self.connection = pika.BlockingConnection(parameters)

if not self.channel or self.channel.is_closed:
self.channel = self.connection.channel()
Expand Down
11 changes: 9 additions & 2 deletions python_logging_rabbitmq/handlers_oneway.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ class RabbitMQHandlerOneWay(logging.Handler):
"""
def __init__(self, level=logging.NOTSET, formatter=None,
host='localhost', port=5672, connection_params=None,
username=None, password=None,
username=None, password=None, url=None,
exchange='log', declare_exchange=False,
remove_request=True,
routing_key_format="{name}.{level}",
Expand All @@ -40,6 +40,7 @@ def __init__(self, level=logging.NOTSET, formatter=None,
# :param message_headers: A dictionary of headers to be published with the message. Optional.
# :param username: Username in case of authentication.
# :param password: Password for the username.
# :param url: AMQP url to be used instead of username/password/host/port.
# :param exchange: Send logs using this exchange.
# :param declare_exchange: Whether or not to declare the exchange.
# :param remove_request: If true (default), remove request/exc info
Expand All @@ -60,6 +61,7 @@ def __init__(self, level=logging.NOTSET, formatter=None,
self.exchange = exchange
self.connection = None
self.channel = None
self._url = url
self.exchange_declared = not declare_exchange
self.remove_request = remove_request
self.routing_key_format = routing_key_format
Expand Down Expand Up @@ -119,7 +121,12 @@ def open_connection(self):

# Connect.
if not self.connection or self.connection.is_closed:
self.connection = pika.BlockingConnection(pika.ConnectionParameters(**self.connection_params))
parameters = None
if self._url:
parameters = pika.URLParameters(self._url)
else:
parameters = pika.ConnectionParameters(**self.connection_params)
self.connection = pika.BlockingConnection(parameters)

if not self.channel or self.channel.is_closed:
self.channel = self.connection.channel()
Expand Down