openSUSE / docmanager

Manage DocBook 5 Meta Information
http://opensuse.github.io/docmanager/index.html
GNU General Public License v3.0
6 stars 6 forks source link

Use logging module #5

Closed mschnitzer closed 9 years ago

mschnitzer commented 9 years ago

It might be a good idea to add a logging module to the project.

tomschr commented 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 commented 9 years ago

Implemented with: https://github.com/openSUSE/docmanager/commit/856175f66201734588022676eab118e37b097a9b

tomschr commented 9 years ago

@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.

mschnitzer commented 9 years ago

Added by https://github.com/openSUSE/docmanager/commit/6d4f9273277fa742fca18b3b245e7f075e51ecae