2

In my Django application I have set up my logging to log all levels to a file, which works well.

During management commands (and only there), I want to log (some levels) to the console aswell.

How can I (dynamically) set up the logging to achieve this?

jpic
  • 32,115
  • 5
  • 105
  • 108
Constantinius
  • 32,691
  • 7
  • 72
  • 83
  • Try this http://stackoverflow.com/questions/4558879/python-django-log-to-console-under-runserver-log-to-file-under-apache – Pol Nov 20 '12 at 16:48
  • @Pol: I know that I can write to the `stderr` stream, but my issue is that I want a different behavior for logging within commands than views. – Constantinius Nov 21 '12 at 09:42

1 Answers1

2

It was actually quite easy, all I had to do was to add a new handler to each logger I wanted to redirect:

loggernames = [ ... ]
level = logging.DEBUG
handler = logging.StreamHandler()
handler.setLevel(level)
handler.setFormatter(logging.Formatter('%(levelname)s: %(message)s'))

for name in loggernames:
    logging.getLogger(name).addHandler(handler)
Constantinius
  • 32,691
  • 7
  • 72
  • 83