From 7672f261b1ab81753390d6df6d055c470b20e530 Mon Sep 17 00:00:00 2001 From: James Rea Date: Tue, 13 Feb 2024 15:50:17 +0000 Subject: [PATCH 1/2] adds URLParameter for RabbitMQHandler/RabbitMQHandlerOneWay --- python_logging_rabbitmq/handlers.py | 11 +++++++++-- python_logging_rabbitmq/handlers_oneway.py | 11 +++++++++-- 2 files changed, 18 insertions(+), 4 deletions(-) diff --git a/python_logging_rabbitmq/handlers.py b/python_logging_rabbitmq/handlers.py index 639221f..d992317 100644 --- a/python_logging_rabbitmq/handlers.py +++ b/python_logging_rabbitmq/handlers.py @@ -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}", @@ -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 @@ -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 @@ -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() diff --git a/python_logging_rabbitmq/handlers_oneway.py b/python_logging_rabbitmq/handlers_oneway.py index a63ecce..298027c 100644 --- a/python_logging_rabbitmq/handlers_oneway.py +++ b/python_logging_rabbitmq/handlers_oneway.py @@ -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}", @@ -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 @@ -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 @@ -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() From 55c42779e20250f008111e35c9e6b9a438da3e9a Mon Sep 17 00:00:00 2001 From: James Rea Date: Tue, 13 Feb 2024 15:53:47 +0000 Subject: [PATCH 2/2] updates README.md with URL --- README.md | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/README.md b/README.md index 145a1ea..34732ae 100644 --- a/README.md +++ b/README.md @@ -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 | @@ -155,6 +156,7 @@ These are the configuration allowed: #### RabbitMQ Connection ```python +# via host, port, username, password rabbit = RabbitMQHandler( host='localhost', port=5672, @@ -168,6 +170,13 @@ rabbit = RabbitMQHandler( ) ``` +```python +# via url +rabbit = RabbitMQHandler( + url=amqps://guest:guest@example.host/vhost-example +) +``` + #### Custom fields ```python