3

I have a custom management command which starts a Tornado loop in order to listen to websocket events. That loops writes its output to the console, which i want to redirect to a log file. I read the Django Docs about Custom Management Commands, but still have no clue how to do it.

The relevent bits of code of my command which is based on sockjs-tornado:

from tornado import web, ioloop
ioloop.IOLoop.instance().start()
Alp
  • 29,274
  • 27
  • 120
  • 198
  • 1
    you could redirect output when you start your command `python manage.py your_command >> a_log_file.log` – dm03514 Apr 16 '13 at 14:43
  • 1
    You can also use [tee](http://en.wikipedia.org/wiki/Tee_(command)) to redirect output to stdout and to the log file. – alecxe Apr 16 '13 at 14:54
  • 1
    You might be able to get away with something like: `import sys; sys.stdout = open('mylog', 'a')` before starting it, but it's a bit hacky. The proper way would be to manipulate the file descriptors at a lower level. – Aya Apr 16 '13 at 14:56
  • Thanks for your comments. @dm03514: i tried that, but id doesn't catch the output of the ioloop. Also, the output is not appended in realtime, but after the process finished. – Alp Apr 16 '13 at 16:03
  • @AlexanderAfanasiev: tee looks very interesting! – Alp Apr 16 '13 at 16:03
  • @Aya: i will try that approach too. – Alp Apr 16 '13 at 16:04

0 Answers0