{"id":182,"date":"2012-11-28T13:19:04","date_gmt":"2012-11-28T13:19:04","guid":{"rendered":"http:\/\/blog.knofafo.de\/?p=182"},"modified":"2013-01-16T14:33:46","modified_gmt":"2013-01-16T14:33:46","slug":"logging-in-python","status":"publish","type":"post","link":"https:\/\/blog.knofafo.de\/?p=182","title":{"rendered":"Logging in Python"},"content":{"rendered":"<p>I am an excessive user of the built-in logging facilities of Python. Most information about that topic can be found in the <a title=\"Python Logging Howto\" href=\"http:\/\/docs.python.org\/2\/howto\/logging.html\" target=\"_blank\">Python Logging Howto<\/a> or in the <a title=\"description module logging\" href=\"http:\/\/docs.python.org\/2\/library\/logging.html\" target=\"_blank\">description of the logging module<\/a>. If you&#8217;re interested how I use it in my modules, just go on reading <!--more--><\/p>\n<p>Nearly every piece of python code I write contains the following (adopted) lines:<br \/>\n<code><br \/>\nimport logging<\/p>\n<p>module_logger = logging.getLogger(\"some funny name\")<br \/>\nmodule_logger.propagate = False<\/p>\n<p>formatter = logging.Formatter(<br \/>\nfmt = \"%(asctime)s -- %(name)s -- %(levelname)s -- %(message)s\" )<\/p>\n<p>ch = logging.StreamHandler()<br \/>\nch.setFormatter(formatter)<br \/>\nch.setLevel(logging.WARNING)<\/p>\n<p>module_logger.addHandler(ch)<br \/>\nmodule_logger.setLevel(logging.WARNING)<\/p>\n<p>def set_debug_level(level):<br \/>\n    possible_levels = dict(<br \/>\n            debug = logging.DEBUG, info = logging.INFO,<br \/>\n            warning = logging.WARNING, error = logging.ERROR,<br \/>\n            fatal = logging.FATAL)<br \/>\n    ch.setLevel(possible_levels[level])<br \/>\n    module_logger.setLevel(possible_levels[level])<br \/>\n<\/code><\/p>\n<p>So that&#8217;s it. My policy is to use mainly debug and info as the output level where the distinction lies in the amount of data a logging call produces. Information about steps in long iterations always is on debug level. Logging for methods which tend to be called very often is also mainly debug, while, e.g., constructors produce info-output. You see that by default I get no output at all as the predefined level is WARNING, which be the <code>set_debug_level<\/code> method of the module can be adjusted.<\/p>\n<p>BTW: I just realized that the <code>code<\/code> tag of WordPress within this style spoils whitespaces, which is especially bad for Python. I&#8217;m going to figure this out as soon as possible.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I am an excessive user of the built-in logging facilities of Python. Most information about that topic can be found in the Python Logging Howto or in the description of the logging module. If you&#8217;re interested how I use it in my modules, just go on reading<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"ngg_post_thumbnail":0},"categories":[3,23],"tags":[25,38,24],"_links":{"self":[{"href":"https:\/\/blog.knofafo.de\/index.php?rest_route=\/wp\/v2\/posts\/182"}],"collection":[{"href":"https:\/\/blog.knofafo.de\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.knofafo.de\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.knofafo.de\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.knofafo.de\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=182"}],"version-history":[{"count":14,"href":"https:\/\/blog.knofafo.de\/index.php?rest_route=\/wp\/v2\/posts\/182\/revisions"}],"predecessor-version":[{"id":190,"href":"https:\/\/blog.knofafo.de\/index.php?rest_route=\/wp\/v2\/posts\/182\/revisions\/190"}],"wp:attachment":[{"href":"https:\/\/blog.knofafo.de\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=182"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.knofafo.de\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=182"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.knofafo.de\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=182"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}