Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>If you wish to print to both the terminal and to a log file, then I'd suggest using the logging module. You can even define a custom formatter, so logging to the file can purge the terminal codes:</p> <pre><code>import optparse import logging def error(string): return '\033[31;1m' + string + '\033[0m' def standout(string): return '\033[34;1m' + string + '\033[0m' def plain(string): return string.replace('\033[34;1m','').replace('\033[31;1m','').replace('\033[0m','') if __name__=='__main__': logging.basicConfig(level=logging.DEBUG, format='%(message)s', filemode='w') logger=logging.getLogger(__name__) def parse_options(): usage = 'usage: %prog [Options]' parser = optparse.OptionParser() parser.add_option('-l', '--logfile', dest='logfile', help='use log file') opt,args = parser.parse_args() return opt,args opt,args=parse_options() if opt.logfile: class MyFormatter(logging.Formatter): def format(self,record): return plain(record.msg) fh = logging.FileHandler(opt.logfile) fh.setLevel(logging.INFO) formatter = MyFormatter('%(message)s') fh.setFormatter(formatter) logging.getLogger('').addHandler(fh) logger.info(error('There was a problem with the program')) logger.info("This is normal " + standout("and this stands out")) </code></pre> <p><code>test.py</code> prints only to the terminal.</p> <p><code>test.py -l test.out</code> prints to both the terminal and to the file <code>test.out</code>.</p> <p>In all cases, the text to the terminal has color codes, while the logging has none.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload