Closed mschnitzer closed 9 years ago
It's a little bit tricky to figure out all the relevant things for logging. To initialize the logging
module, I usually use this code:
import logging
log = logging.getLogger(__file__)
ch = logging.StreamHandler(sys.stderr)
frmt = logging.Formatter('%(levelname)s: %(message)s')
ch.setFormatter(frmt)
log.setLevel(logging.DEBUG)
log.addHandler(ch)
Additionally, add a -v
/--verbose
option with action="count"
:
parser.add_argument('-v', '--verbose',
action='count',
help="Increase verbosity level"
)
To set the log level accordingly:
loglevel = {None: logging.NOTSET,
# 1:logging.CRITICAL,
# 2:logging.ERROR,
# 1:logging.WARNING,
1:logging.INFO,
2:logging.DEBUG,
}
args=parser.parse_args()
log.setLevel(loglevel.get(args.verbose, logging.DEBUG))
Use log.debug()
and the other functions as usual.
@mschnitzer You've forgot to push the file logmanager.py
. I've tried to recover it, but the logmgr_flog()
function needs special dedication from you. :-) Please implement them from your working copy.
It might be a good idea to add a logging module to the project.