3

Is it possible to have multiple logging handlers referring to the same log file in python logging configuration. I can make it work by having one more handler handler_two added to the handlers object, but this seems to be a boilerplate.

"handler_two": {
      "level": "DEBUG",
      "class": "logging.handlers.TimedRotatingFileHandler",
      "formatter": "verbose",
      "filename": "{}/abc.log".format(log_folder),
      "when": "midnight",
      "backupCount": 10,
      "encoding": "utf8"
    },

logging.conf -

  "version": 1,
  "disable_existing_loggers": False,
  "formatters": {
    "verbose": {
      "format": "%(asctime)s  %(name)s  %(levelname)s  (PID: %(process)d) %(message)s",
      "datefmt": "%d/%m/%Y %I:%M:%S %p %Z"
    },
    "simple": {
      "format": "%(asctime)s  %(name)s %(levelname)s >  %(message)s"
    }
  },
  "handlers": {
    "handler_one": {
      "level": "DEBUG",
      "class": "logging.handlers.TimedRotatingFileHandler",
      "formatter": "verbose",
      "filename": "{}/abc.log".format(log_folder),
      "when": "midnight",
      "backupCount": 10,
      "encoding": "utf8"
    },
    "error": {
      "level": "ERROR",
      "class": "logging.handlers.TimedRotatingFileHandler",
      "formatter": "verbose",
      "filename": "{}/error.log".format(log_folder),
      "when": "midnight",
      "backupCount": 10,
      "encoding": "utf8"
    }
  },
  "root": {
    "level": "ERROR",
    "handlers": [
      "error"
    ]
  },
  "loggers": {
    "handler_one": {
      "level": "DEBUG",
      "handlers": [
        "handler_one"
      ],
      "propagate": "false"
    },
  }
}
Pro
  • 305
  • 3
  • 13
  • Now you have 2 handlers to 2 different log files. What are you trying to achieve? – Adrian Krupa Dec 03 '19 at 09:04
  • Does this answer your question? [Python logging to multiple handlers, at different log levels?](https://stackoverflow.com/questions/25187083/python-logging-to-multiple-handlers-at-different-log-levels) – shaik moeed Dec 03 '19 at 09:05
  • @AdrianKrupa I want multiple handler writing to the same file. ignore the error handler. As I mentioned, I can acheive this by duplicating `handler_one` and change the name to `handler_two`. But there would be many modules and for each of them we want to log to a single file but at the same time we need an identifier to distinguish. __ name __as a logger name is something I cannot use as it's not aligned with the company's coding guidelines. – Pro Dec 03 '19 at 09:44

1 Answers1

4

It is possible to achieve that by opening the file stream yourself and passing it to the stream handler, as in the following:

import logging

logger = logging.getLogger()
logger.setLevel(logging.INFO)

stream = open("test.log", "a")

handler_1 = logging.StreamHandler(stream)
logger.addHandler(handler_1)
handler_2 = logging.StreamHandler(stream)
logger.addHandler(handler_2)

logger.info("test log")

The test.log file will then have content:

test log
test log
cicolus
  • 697
  • 5
  • 16