Background worker in python using pika doesn't logs print

Hi,

I’m building a background worker in python.
My code is very simple and works fine but my “print” statements are not displayed in the logs.

I noticed that everything works fine without the pika library (RabbitMQ) but when I add it in the script, the “print” statements stop to appear.

Here is my code :

import pika
import os

# Get RabbitMQ URL from environment variable or use a default
RABBITMQ_URL = os.getenv('RABBITMQ_URL', 'amqp://user:password@localhost')

# Connect to RabbitMQ using the URL
params = pika.URLParameters(RABBITMQ_URL)
connection = pika.BlockingConnection(params)
channel = connection.channel()

channel.queue_declare(queue='task_queue',
                    #   durable=True
                      )

def callback(ch, method, properties, body):
    print(" [x] Received %r" % body)
    ch.basic_ack(delivery_tag=method.delivery_tag)


channel.basic_consume(queue='task_queue', on_message_callback=callback)

print(' [*] Waiting for messages. To exit press CTRL+C')
channel.start_consuming()

But when using the logging library, the logging appears (and so everything works fine).

import pika
import os
import logging

logging.basicConfig(level=logging.INFO)  # Ou DEBUG pour plus de détails
logger = logging.getLogger(__name__)

logger.info("Démarrage de l'application...")

# Get RabbitMQ URL from environment variable or use a default
RABBITMQ_URL = os.getenv('RABBITMQ_URL', 'amqp://user:password@localhost')

print(RABBITMQ_URL)

# Connect to RabbitMQ using the URL
params = pika.URLParameters(RABBITMQ_URL)
connection = pika.BlockingConnection(params)
channel = connection.channel()

channel.queue_declare(queue='task_queue',
                    #   durable=True
                      )

def callback(ch, method, properties, body):
    print(" [x] Received %r" % body)
    ch.basic_ack(delivery_tag=method.delivery_tag)


channel.basic_consume(queue='task_queue', on_message_callback=callback)

print(' [*] Waiting for messages. To exit press CTRL+C')
channel.start_consuming()

Do you have any idea ? What pika does that prevent showing the “print” ?

Thanks for your help.

Hi rmonnier9,

Render logs everything output to STDOUT. So you’ll need to make sure you configure your logging library to output to STDOUT.

Regards,

Matt

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.