12

I want to output some strings to a log file and I want the log file to be continuously updated.

I have looked into the logging module pf python and found out that it is mostly about formatting and concurrent access.

Please let me know if I am missing something or amy other way of doing it

3 Answers 3

20

usually i do the following:

  # logging  
  LOG = "/tmp/ccd.log"                                                     
  logging.basicConfig(filename=LOG, filemode="w", level=logging.DEBUG)  

  # console handler  
  console = logging.StreamHandler()  
  console.setLevel(logging.ERROR)  
  logging.getLogger("").addHandler(console)

The logging part initialises logging's basic configurations. In the following I set up a console handler that prints out some logging information separately. Usually my console output is set to output only errors (logging.ERROR) and the detailed output in the LOG file.

Your loggings will now printed to file. For instance using:

logger = logging.getLogger(__name__)
logger.debug("hiho debug message")

or even

logging.debug("next line")

should work.

Doug Hellmann has a nice guide.

Sign up to request clarification or add additional context in comments.

Comments

14

To add my 10cents with regards to using logging. I've only recently discovered the Logging module and was put off at first. Maybe just because it initially looks like a lot of work, but it's really simple and incredibly handy.

This is the set up that I use. Similar to Mkinds answer, but includes a timestamp.

# Set up logging
log = "bot.log"
logging.basicConfig(filename=log,level=logging.DEBUG,format='%(asctime)s %(message)s', datefmt='%d/%m/%Y %H:%M:%S')
logging.info('Log Entry Here.')

Which will produce something like:

22/09/2015 14:39:34 Log Entry Here.

Comments

4

You can log to a file with the Logging API.

Example: http://docs.python.org/2/howto/logging.html#logging-to-a-file

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.