{"id":619,"date":"2014-01-20T18:00:27","date_gmt":"2014-01-21T01:00:27","guid":{"rendered":"http:\/\/somethingk.com\/main\/?p=619"},"modified":"2014-01-20T18:00:27","modified_gmt":"2014-01-21T01:00:27","slug":"logging-on-python-2-7","status":"publish","type":"post","link":"https:\/\/somethingk.com\/main\/logging-on-python-2-7\/","title":{"rendered":"Logging on Python 2.7"},"content":{"rendered":"<p>Something actually useful&#8230; logging! Not talking about lumber but logging code issues, data, access, anything useful in an application environment. Python makes this super easy!<\/p>\n<p>Logging is part of the Python standard library and can be imported with the following line of code.<\/p>\n<pre>import logging<\/pre>\n<p style=\"text-align: center;\"><a href=\"http:\/\/laudyms.files.wordpress.com\/2010\/03\/old-forest-logging.jpg\"><img fetchpriority=\"high\" decoding=\"async\" class=\"aligncenter\" title=\"Logging\" alt=\"\" src=\"http:\/\/laudyms.files.wordpress.com\/2010\/03\/old-forest-logging.jpg\" width=\"426\" height=\"322\" \/><\/a><\/p>\n<p><strong>Setup<\/strong><\/p>\n<p><span style=\"line-height: 1.5em;\">First step after importing the library is to\u00a0<\/span>initiate<span style=\"line-height: 1.5em;\">\u00a0the logger.\u00a0<\/span><\/p>\n<pre>log = logging.getLogger(__name__) #create a log object<\/pre>\n<p>The logger name hierarchy is analogous\/identical to the Python package hierarchy if you organize your loggers on the recommended per-module basis.\u00a0This is why you should use &#8220;__name__&#8221; which is the module\u2019s name in the Python package namespace.<\/p>\n<p>After creating a log, now the level needs to be specified. There are different declared levels of severity for logging. Below is a list of these levels ordered from least to most severe.<\/p>\n<ul>\n<li>DEBUG &#8211; Detailed information for diagnosing problems.<\/li>\n<li>INFO &#8211; Confirmation that things are working as expected.<\/li>\n<li>WARNING &#8211; Notice that something unexpected happened, however the software is still working as expected.<\/li>\n<li>ERROR &#8211; Data on an issue that has happened, which has preventing a function from being performed.<\/li>\n<li>CRITICAL &#8211; Information about a serious problem! The program may not even run at all with this going on!<\/li>\n<\/ul>\n<p>Items are logged based on the level of severity you set for the log. The default level is &#8220;warning&#8221;, this will track warning, error and critical items. If you selected debug, everything on up would be tracked.<\/p>\n<p>Remember, you are the one who determines the level for a log message, so say you create a debug warning but your logging level is set to error. The debug message will not be printed because it is not of the error severity or higher.<\/p>\n<pre>log.setLevel(logging.&lt;LOGGING LEVEL&gt;)<\/pre>\n<p>Formats the log all pretty. This format, places the level first followed by the message I choose to insert.<\/p>\n<pre>formatter = logging.Formatter('[%(<wbr \/>levelname)s] %(message)s')<\/pre>\n<p>This creates a handler for logging everything to a specified file.<\/p>\n<pre>handler = logging.FileHandler(&lt;FILE PATH&gt;)<\/pre>\n<p>Set the file handler with the format specified earlier.<\/p>\n<pre>handler.setFormatter(<wbr \/>formatter)<\/pre>\n<p>Enforce the log to use the handler.<\/p>\n<pre>log.addHandler(handler)<\/pre>\n<p style=\"text-align: center;\">\u00a0<a href=\"http:\/\/img203.imageshack.us\/img203\/2374\/92470.jpg\"><img decoding=\"async\" class=\"aligncenter\" title=\"All Together Now\" alt=\"\" src=\"http:\/\/img203.imageshack.us\/img203\/2374\/92470.jpg\" width=\"288\" height=\"381\" \/><\/a><\/p>\n<p><strong>All together<\/strong><\/p>\n<pre>#!\/usr\/bin\/env python<\/pre>\n<pre>import logging<\/pre>\n<pre>log = logging.getLogger(__name__)<\/pre>\n<pre>log.setLevel(logging.error) #error level<\/pre>\n<pre>formatter = logging.Formatter('[%(<wbr \/>levelname)s] %(message)s')<\/pre>\n<pre>handler = logging.FileHandler('error.<wbr \/>log') #log to file error.log<\/pre>\n<pre>handler.setFormatter(<wbr \/>formatter)<\/pre>\n<pre>log.addHandler(handler)<\/pre>\n<p><a href=\"http:\/\/img.gawkerassets.com\/img\/1910l71l1m8vijpg\/original.jpg\"><img decoding=\"async\" class=\"aligncenter\" alt=\"\" src=\"http:\/\/img.gawkerassets.com\/img\/1910l71l1m8vijpg\/original.jpg\" width=\"260\" height=\"310\" \/><\/a><\/p>\n<p><strong>Implementation &#8211; Logging Items<\/strong><\/p>\n<p>It\u2019s simple, just use the following syntax wherever you want a log entry to be made in your code.<\/p>\n<pre>log.&lt;LEVEL&gt;('&lt;MESSAGE&gt;')<\/pre>\n<p>Example<\/p>\n<pre>logging.info('This is awesome!')<\/pre>\n<p>I like to use logging to report errors from &#8220;try and except&#8221; statements in a situation where I don\u2019t want the loop to stop because of an error.<br \/>\n<code>while 1:<br \/>\n<wbr \/>\u00a0 try:<br \/>\n<wbr \/>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 doSomething()<br \/>\n<wbr \/>\u00a0 except, Exception E:<br \/>\n<wbr \/>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 logging.error(str(datetime.<wbr \/>datetime.now())+\" \"+e) #I\u2019ll add the timestamp with datetime for kicks<br \/>\n<wbr \/>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 pass<\/code><br \/>\nThere you have it, very useful and simple python programming. More documentation at\u00a0<a href=\"http:\/\/docs.python.org\/2\/howto\/logging.html\" target=\"_blank\">http:\/\/docs.python.org\/2\/<wbr \/>howto\/logging.html<\/a>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Something actually useful&#8230; logging! Not talking about lumber but logging code issues, data, access, anything useful in an application environment. Python makes this super easy! Logging is part of the Python standard library and can be imported with the following line of code. import logging Setup First step after importing the library is to\u00a0initiate\u00a0the logger.\u00a0 log = logging.getLogger(__name__) #create a [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[72],"tags":[154,153],"class_list":["post-619","post","type-post","status-publish","format-standard","hentry","category-python","tag-logging","tag-python-2-7"],"_links":{"self":[{"href":"https:\/\/somethingk.com\/main\/wp-json\/wp\/v2\/posts\/619","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/somethingk.com\/main\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/somethingk.com\/main\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/somethingk.com\/main\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/somethingk.com\/main\/wp-json\/wp\/v2\/comments?post=619"}],"version-history":[{"count":18,"href":"https:\/\/somethingk.com\/main\/wp-json\/wp\/v2\/posts\/619\/revisions"}],"predecessor-version":[{"id":637,"href":"https:\/\/somethingk.com\/main\/wp-json\/wp\/v2\/posts\/619\/revisions\/637"}],"wp:attachment":[{"href":"https:\/\/somethingk.com\/main\/wp-json\/wp\/v2\/media?parent=619"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/somethingk.com\/main\/wp-json\/wp\/v2\/categories?post=619"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/somethingk.com\/main\/wp-json\/wp\/v2\/tags?post=619"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}