{"id":24,"date":"2023-12-06T21:28:40","date_gmt":"2023-12-07T02:28:40","guid":{"rendered":"https:\/\/joeacecil.com\/wordpress\/?p=24"},"modified":"2023-12-06T21:28:40","modified_gmt":"2023-12-07T02:28:40","slug":"how-to-customize-pythons-logging-level-without-configuration-madness","status":"publish","type":"post","link":"https:\/\/joeacecil.com\/wordpress\/?p=24","title":{"rendered":"How to customize Python&#8217;s logging level without &#8216;configuration madness&#8217;"},"content":{"rendered":"\n<p>Python&#8217;s standard <code><a href=\"https:\/\/docs.python.org\/3\/library\/logging.html\">logging<\/a><\/code> library offers a standard module (<code><a href=\"https:\/\/docs.python.org\/3\/library\/logging.config.html\">logging.config<\/a><\/code>) for configuration, and it offers a lot of options. Most of them look like a pain. If you just want to configure the logging level, for example, you might wonder: Do I have to write an entire file listing handlers and files and other configuration options just to say I only want to see <code>INFO<\/code>-level messages and above?<\/p>\n\n\n\n<p>Fortunately, there&#8217;s a much easier way to configure your code&#8217;s logging level. You can simply call <code><a href=\"https:\/\/docs.python.org\/3\/library\/logging.html#logging.basicConfig\">logging.basicConfig()<\/a><\/code> with the configuration settings you want to change. In this case, you&#8217;ll want something like:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import logging\nimport os\n\n...\n\ndef main():\n    ...\n    logging.basicConfig(\n        level=getattr(logging, os.getenv(\"LOGGING_LEVEL\", \"INFO\"))\n    )<\/code><\/pre>\n\n\n\n<p>This call to <code>basicConfig()<\/code> will set the logging level to the one defined by the environment variable <code>LOGGING_LEVEL<\/code> (which could be, for example, <code>ERROR<\/code>, <code>WARNING<\/code>, <code>INFO<\/code>, or <code>DEBUG<\/code> by default). If the user doesn&#8217;t set <code>LOGGING_LEVEL<\/code>, it defaults to the <code>INFO<\/code> level.<\/p>\n\n\n\n<p>Of course, we don&#8217;t have to get the logging level config from an environment variable&#8212;it could come from anywhere. If this is a CLI app, you could define a <code>--logging-level<\/code> argument that controls the logging level. You could define and load your own simple configuration file (perhaps a <code>json<\/code> file) that includes a logging level value. The sky&#8217;s the limit.<\/p>\n\n\n\n<p>So, don&#8217;t worry about setting up a complicated logging config file. You can easily configure logging with a simple call to <code>basicConfig()<\/code>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Python&#8217;s standard logging library offers a standard module (logging.config) for configuration, and it offers a lot of options. Most of them look like a pain. If you just want to configure the logging level, for example, you might wonder: Do I have to write an entire file listing handlers and files and other configuration options [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[1],"tags":[],"class_list":["post-24","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"jetpack_featured_media_url":"","jetpack-related-posts":[],"jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/joeacecil.com\/wordpress\/index.php?rest_route=\/wp\/v2\/posts\/24","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/joeacecil.com\/wordpress\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/joeacecil.com\/wordpress\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/joeacecil.com\/wordpress\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/joeacecil.com\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=24"}],"version-history":[{"count":5,"href":"https:\/\/joeacecil.com\/wordpress\/index.php?rest_route=\/wp\/v2\/posts\/24\/revisions"}],"predecessor-version":[{"id":29,"href":"https:\/\/joeacecil.com\/wordpress\/index.php?rest_route=\/wp\/v2\/posts\/24\/revisions\/29"}],"wp:attachment":[{"href":"https:\/\/joeacecil.com\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=24"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/joeacecil.com\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=24"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/joeacecil.com\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=24"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}