0

I'm trying to broadcast my error log with a SocketHandler, and it's working great if I put this in the main.py of my app engine project, but if I just write a simple stand alone python script and run it, it doesn't seem to successfully broadcast the logs.

import logging
import logging.handlers

sh = logging.handlers.SocketHandler('localhost', logging.handlers.DEFAULT_TCP_LOGGING_PORT)
rootLogger = logging.getLogger('')
rootLogger.addHandler(sh)

logging.info(['Test', 'This', 'List'])
logging.info(dict(Test = 'Blah', Test2 = 'Blah2', Test3 = dict(Test4 = 'Blah4')))

I'm at a loss as to how to go about debugging this. Any ideas?

Joren
  • 9,623
  • 19
  • 63
  • 104

1 Answers1

1

The default level for logging is WARNING, so INFO messages will not be seen by default. You need a rootLogger.setLevel(logging.INFO) to see INFO messages, for example, or set the level to logging.DEBUG to see all messages.

Update: It's hard to see how logging.error() would fail to output where logging.info() does, unless there is a filter involved or an error during the logging.error() call. Please post a runnable script which demonstrates the issue when run with the logging socket server described in the docs.

Further update: logging doesn't automatically capture exceptions. To log an exception, the usual thing for the developer to do is to use a try: except: block and to call logging.exception(...) in the exception handling code.

Vinay Sajip
  • 95,872
  • 14
  • 179
  • 191
  • Well that was easy. I guess app engine is setting the log level to DEBUG by default. Thanks! – Joren Sep 06 '13 at 22:47
  • This is pretty related so I'm going to try to ask it here. If I set the debug level to DEBUG or ERROR and use isEnabledFor(logging.ERROR) I get true, but it's not sending errors. Info logs send, but if I intentionally add an error right after the logging configuration, it does not send. – Joren Sep 07 '13 at 06:16
  • I think I miss-communicated. My problem is it won't broadcast exceptions. If I take that example code and introduce an exception by changing the first logging.info line to loging.info, that exception is not broadcast. – Joren Sep 08 '13 at 19:14
  • So what I needed was global exception handling, which I found here http://stackoverflow.com/questions/6598053/python-global-exception-handling – Joren Sep 08 '13 at 23:34