Bookworm-project / BookwormDB

Tools for text tokenization and encoding
MIT License
84 stars 12 forks source link

Make use of logging module #60

Closed bmschmidt closed 8 years ago

bmschmidt commented 9 years ago

This is low priority but useful for future contributions to the codebase. The handling of progress writing, error messages, and other problems is currently irregular: in general a stream is written to stderr, but some old debugging code is disappearing entirely.

Peter is right on a previous issue that it would be nice to allow debug logging code to remain around and be toggled on when needed. We'll want to write some errors simultaneously to the log and to stderr, which is possible.

Pasting Peter's comments below and closing the previous issue.

Logging lets you have different levels (debug, info, warn, error), allows you to route to a log file, and outputs in a standard format -- particularly log time and log level are helpful. Messages are only logged when they're equal or greater than a specific logging level; i.e. with the default 'warn' level, info and debug messages are not logged but warn and error are.

I started using it in this case a) because I can keep code to print verbose debugging in, rather than temporary putting in print statements and subsequently having to hunt them down before commits, and b) finding messages in the log file by log level is useful, so I can grep for caught-but-logged errors after running a big process.

Logging doesn't write anything unless you set it. It's global, so you could set it in OneClick.py or another top-level script, and the imported modules will log at that level. Remembering that I'm on mobile and there may be typos, here's an example:

logging.basicConfig("filename.log", level=logging.INFO)

organisciak commented 9 years ago

I made an error in my example (level is the proper flag for logging.basicConfig, not the R-style logging.level). Editing the comments to avoid confusion.

Also, two more patterns. First, logging doesn't include time by default, so when you're turning it on with logging basicConfig, you can pass a formatting string that includes asctime. i.e.

logging.basicConfig(format="%(levelname)s:%(asctime)s:%(message)s")

Logging a caught exception in Python 2.x:

try:
     something()
except Exception, e:
     logging.exception(e)

I think any message can be sent to logging.exception, the key part is that it includes a stack trace in the logs.

bmschmidt commented 8 years ago

Closing, implemented in v0.4.0