if record.exc_text: s =s.replace( ' ','')+'| ' return s def configure_logging (): fh =logging.FileHandler('output.txt ','w') f =OneLineExceptionFormatter(' %(asctime)s | %(levelname)s | %(message)s | ', ' %d /%m/%Y %H:%M:%S') fh.setFormatter(f) root =logging.getLogger() root.setLevel(logging .DEBUG) root.addHandler(fh) def main(): configure_logging() logging.info('Sample message') try : x =1/0 except ZeroDivisionError as e: logging.exception( 'ZeroDivisionError: %s ', e) if __name__ =='__main__ ': main() 28/01/2015 07:21:23|INFO|Sample message| 28/01/2015 07:21:23|ERROR|ZeroDivisionError: integer ↪ → 'Traceback (most recent call last):
 File "logtest7.py", line 30, ␣ ↪ → x = 1 / 0
ZeroDivisionError: integer division or modulo by zero'| import logging import subprocess import sys classTTSHandler (logging .Handler): def emit(self, record): msg =self.format(record) # Speak slowly in a female English voice cmd =['espeak','-s150 ','-ven+f3 ', msg] p =subprocess.Popen(cmd, stdout =subprocess.PIPE, stderr=subprocess.STDOUT) # wait for the program to finish (continues on next page) 35 (continued from previous page) When run, this produces a file with exactly two lines: While the above treatment is simplistic, it points the way to how exception information can be formatted to your liking. The$_{traceback}$module may be helpful for more specialized needs. There might be situations when it is desirable to have logging messages rendered in an audible rather than a visible format. This is easy to do if you have text-to-speech (TTS) functionality available in your system, even if it doesn’t have a Python binding. Most TTS systems have a command line program you can run, and this can be invoked from a handler using$_{subprocess}$. It’s assumed here that TTS command line programs won’t expect to interact with users or take a long time to complete, and that the frequency of logged messages will be not so high as to swamp the user with messages, and that it’s acceptable to have the messages spoken one at a time rather than concurrently, The example implementation below waits for one message to be spoken before the next is processed, and this might cause other handlers to be kept waiting. Here is a short example showing the approach, which assumes that the$_{espeak}$ TTS package is available: 24 Speaking logging messages