L
L
LakeForest2021-09-12 22:50:20
Python
LakeForest, 2021-09-12 22:50:20

How to start logging ONLY what I ordered?

import logging


logging.basicConfig(
              filename="./log.txt",
              filemode="a",
              format="%(levelname)s;%(asctime)s;%(message)s",
              datefmt="%Y-%m-%d %H:%M:%S",
              level=logging.INFO)


But it logs both pika and tornado. How to stop logging all this? It would be nice if the opposite was output to the console.
INFO;2021-09-12 18:13:18;Pika version 1.2.0 connecting to ('172.30.0.2', 5672)
INFO;2021-09-12 18:13:18;Socket connected: <socket.socket fd=12, family=AddressFamily.AF_INET, type=SocketKind.SOCK_STREAM, proto=6, laddr=('172.30.0.5', 37154), raddr=('172.30.0.2', 5672)>
INFO;2021-09-12 18:13:18;Streaming transport linked up: (<pika.adapters.utils.io_services_utils._AsyncPlaintextTransport object at 0x7f7166cd3100>, _StreamingProtocolShim: <SelectConnection PROTOCOL transport=<pika.adapters.utils.io_services_utils._AsyncPlaintextTransport object at 0x7f7166cd3100> params=<ConnectionParameters host=rabbitmq port=5672 virtual_host=/ ssl=False>>).
INFO;2021-09-12 18:13:18;AMQPConnector - reporting success: <SelectConnection OPEN transport=<pika.adapters.utils.io_services_utils._AsyncPlaintextTransport object at 0x7f7166cd3100> params=<ConnectionParameters host=rabbitmq port=5672 virtual_host=/ ssl=False>>
INFO;2021-09-12 18:13:18;AMQPConnectionWorkflow - reporting success: <SelectConnection OPEN transport=<pika.adapters.utils.io_services_utils._AsyncPlaintextTransport object at 0x7f7166cd3100> params=<ConnectionParameters host=rabbitmq port=5672 virtual_host=/ ssl=False>>
INFO;2021-09-12 18:13:18;Connection workflow succeeded: <SelectConnection OPEN transport=<pika.adapters.utils.io_services_utils._AsyncPlaintextTransport object at 0x7f7166cd3100> params=<ConnectionParameters host=rabbitmq port=5672 virtual_host=/ ssl=False>>
INFO;2021-09-12 18:13:18;Created channel=1
INFO;2021-09-12 18:13:52;101 GET /ws (172.30.0.6) 0.45ms
INFO;2021-09-12 18:14:25;101 GET /ws (172.30.0.6) 0.41ms
INFO;2021-09-12 18:16:59;101 GET /ws (172.30.0.6) 0.38ms
INFO;2021-09-12 18:18:46;101 GET /ws (172.30.0.6) 0.38ms
INFO;2021-09-12 19:45:13;MQ: Connect guest rabbitmq:5672
INFO;2021-09-12 19:45:13;Pika version 1.2.0 connecting to ('172.30.0.2', 5672)
INFO;2021-09-12 19:45:13;Socket connected: <socket.socket fd=12, family=AddressFamily.AF_INET, type=SocketKind.SOCK_STREAM, proto=6, laddr=('172.30.0.5', 49150), raddr=('172.30.0.2', 5672)>
INFO;2021-09-12 19:45:13;Streaming transport linked up: (<pika.adapters.utils.io_services_utils._AsyncPlaintextTransport object at 0x7fae23aee100>, _StreamingProtocolShim: <SelectConnection PROTOCOL transport=<pika.adapters.utils.io_services_utils._AsyncPlaintextTransport object at 0x7fae23aee100> params=<ConnectionParameters host=rabbitmq port=5672 virtual_host=/ ssl=False>>).
INFO;2021-09-12 19:45:13;AMQPConnector - reporting success: <SelectConnection OPEN transport=<pika.adapters.utils.io_services_utils._AsyncPlaintextTransport object at 0x7fae23aee100> params=<ConnectionParameters host=rabbitmq port=5672 virtual_host=/ ssl=False>>
INFO;2021-09-12 19:45:13;AMQPConnectionWorkflow - reporting success: <SelectConnection OPEN transport=<pika.adapters.utils.io_services_utils._AsyncPlaintextTransport object at 0x7fae23aee100> params=<ConnectionParameters host=rabbitmq port=5672 virtual_host=/ ssl=False>>
INFO;2021-09-12 19:45:13;Connection workflow succeeded: <SelectConnection OPEN transport=<pika.adapters.utils.io_services_utils._AsyncPlaintextTransport object at 0x7fae23aee100> params=<ConnectionParameters host=rabbitmq port=5672 virtual_host=/ ssl=False>>
INFO;2021-09-12 19:45:13;Created channel=1

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
Vindicar, 2021-09-13
@Vindicar

Determine the names of the loggers used by the libraries (the name field in the message format), get the corresponding objects via logging.getLogger().
Then you can choose to
a) set them to ERROR, CRITICAL or even higher (for example, 100500) so that ordinary records are filtered out
b) create your own Filter that blocks all records and set it for these loggers through the appropriate method
c) set their propagate property to False so that they don't forward their records to the root logger you set up.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question