{"id":513,"date":"2011-09-26T16:34:09","date_gmt":"2011-09-26T16:34:09","guid":{"rendered":"http:\/\/tech.avant.net\/q\/?p=513"},"modified":"2019-06-01T01:52:40","modified_gmt":"2019-06-01T01:52:40","slug":"python-logging","status":"publish","type":"post","link":"https:\/\/tech.avant.net\/q\/python-logging\/","title":{"rendered":"python logging"},"content":{"rendered":"<p>I would like customizable logging in python applications, and I would like to easily send log messages to multiple handlers without any modification of the application code. The built-in logging module provides a very robust and easy-to-use logging capability.<\/p>\n<p>In it&#8217;s simplest form, log messages will be sent to the console with minimal formatting, e.g.,<\/p>\n<pre class=\"sh_python\">&gt;&gt;&gt; import logging\n&gt;&gt;&gt; \n&gt;&gt;&gt; logging.warning('this is a warning')\nWARNING:root:this is a warning\n<\/pre>\n<p>In fact, your application and module code can simply use the built-in logging methods which can inherit logging handlers set by the main application code.<\/p>\n<p>Below is an example of a logging configuration that creates three logging handlers:<\/p>\n<pre class=\"sh_python\">import logging\n\n# Log to file\nlogging.basicConfig(\n    filename='test.log',\n    level=logging.INFO,\n    format='%(asctime)-15s %(levelname)s:%(filename)s:%(lineno)d -- %(message)s'\n)   \n\n# Log to console\nconsole = logging.StreamHandler()\nconsole.setLevel(logging.DEBUG)\nconsole.setFormatter(logging.Formatter('%(levelname)s:%(filename)s:%(lineno)d -- %(message)s'))\nlogging.getLogger().addHandler(console)\n\n# Log to syslog\nfrom logging.handlers import SysLogHandler\nsyslog = SysLogHandler(address='\/dev\/log')\nsyslog.setFormatter(logging.Formatter('%(asctime)-15s %(levelname)s:%(filename)s:%(lineno)d -- %(message)s'))\nlogging.getLogger().addHandler(syslog)\n<\/pre>\n<p>Once this module is imported, any logging calls to the root logger [e.g., logging.warning(), logging,critical(), etc] will be processed by all three handlers. You can add as many log handlers as needed while your module code maintains a very simple logging interface, e.g.,<\/p>\n<pre class=\"sh_python\">import logging\n\nlogging.debug('this is a debug msg')\nlogging.info('this is an info msg')\nlogging.warning('this is a warning msg')\nlogging.error('this is an error msg')\nlogging.critical('this is a critical error msg')\n<\/pre>\n<p>The console output will look like:<\/p>\n<pre>INFO:test_log.py:4 -- this is an info msg\nWARNING:test_log.py:5 -- this is a warning msg\nERROR:test_log.py:6 -- this is an error msg\nCRITICAL:test_log.py:7 -- this is a critical error msg\n<\/pre>\n<p>The filelog (and syslog) will look like:<\/p>\n<pre> \n2011-09-26 12:30:52,521 INFO:test_log.py:4 -- this is an info msg\n2011-09-26 12:30:52,522 WARNING:test_log.py:5 -- this is a warning msg\n2011-09-26 12:30:52,522 ERROR:test_log.py:6 -- this is an error msg\n2011-09-26 12:30:52,522 CRITICAL:test_log.py:7 -- this is a critical error msg\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>I would like customizable logging in python applications, and I would like to easily send log messages to multiple handlers without any modification of the application code. The built-in logging module provides a very robust and easy-to-use logging capability. In it&#8217;s simplest form, log messages will be sent to the console with minimal formatting, e.g., [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[6],"tags":[],"_links":{"self":[{"href":"https:\/\/tech.avant.net\/q\/wp-json\/wp\/v2\/posts\/513"}],"collection":[{"href":"https:\/\/tech.avant.net\/q\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/tech.avant.net\/q\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/tech.avant.net\/q\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/tech.avant.net\/q\/wp-json\/wp\/v2\/comments?post=513"}],"version-history":[{"count":8,"href":"https:\/\/tech.avant.net\/q\/wp-json\/wp\/v2\/posts\/513\/revisions"}],"predecessor-version":[{"id":988,"href":"https:\/\/tech.avant.net\/q\/wp-json\/wp\/v2\/posts\/513\/revisions\/988"}],"wp:attachment":[{"href":"https:\/\/tech.avant.net\/q\/wp-json\/wp\/v2\/media?parent=513"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/tech.avant.net\/q\/wp-json\/wp\/v2\/categories?post=513"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/tech.avant.net\/q\/wp-json\/wp\/v2\/tags?post=513"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}