Chris Utter
Chris Utter

Reputation: 143

Does Python have a standard error log file and error_log function like PHP?

Trying as a developer to convert from php to python. Currently attempting to debug a python script that takes around 45 minutes to run. Haven't had it die yet when running from command line. Dies about half the time when run from cron.

With php I could just go back to the box it ran on, check the log file for the time it was run and find/fix errors if the script bombed out. Does python have something similar?

Upvotes: 2

Views: 14304

Answers (2)

little-dude
little-dude

Reputation: 1674

Python can print logs in a file, but that is not the behavior you get out of the box. By default, it prints logs with a log level equal or higher than WARNING on stderr:

import logging              

logging.debug('debug')
logging.info('info')
logging.warning('warning')
logging.error('error')
logging.critical('critical')

Here is what happens when you run this script:

$ python log.py            
WARNING:root:warning
ERROR:root:error
CRITICAL:root:critical

$ python log.py 2>/dev/null   # does not print anything

$

So if you redirected stderr to a file in your cron job, you should at least see the warnings and errors in that file. If you want to easily log directly into a file, you can use logging.basicConfig:

import logging                                                                

# print log in example.log instead of the console, and set the log level to DEBUG (by default, it is set to WARNING)
logging.basicConfig(filename='example.log', filemode='w', level=logging.DEBUG)

logging.debug('debug')
logging.info('info')
logging.warning('warning')
logging.error('error')
logging.critical('critical')

Now if you run it, you should not have any input in the console anymore, but everything should be in example.log:

$ python log.py         

$ cat example.log       
DEBUG:root:debug        
INFO:root:info          
WARNING:root:warning    
ERROR:root:error        
CRITICAL:root:critical  

You'll find much more details and configuration options in the logging HOWTO page

Upvotes: 3

SteveJ
SteveJ

Reputation: 3313

I believe you are asking for two different things. PHP is generally running as a server side application thus has specific constructs for error logging. Python can run about any type of application and run multiple instances at once - so what you are asking doesn't really make sense in that context. However, you can still accomplish what you desire.

Though you can pipe output to stderr directly from you cron job " > /path/to/log.txt", for more granular information, use pythons logging libarary - part of the standard libary. Personally, though, I prefer pygogo -- it provides a nice wrapper around the logging libary.

So...

  1. Add some logging to your Python with either the standard library or pygogo
  2. Pipe the output from your cron job to a log file using " > " or " tee ".
  3. Watch your logs real time using "tail -f /path/to/my/log.txt"

I do this regularly, works like a charm.

Upvotes: 1

Related Questions