{"id":135370,"date":"2024-09-06T11:43:16","date_gmt":"2024-09-06T06:13:16","guid":{"rendered":"https:\/\/www.vskills.in\/certification\/tutorial\/?page_id=135370"},"modified":"2024-09-06T11:43:16","modified_gmt":"2024-09-06T06:13:16","slug":"setting-up-multiple-loggers-in-the-logging-module","status":"publish","type":"page","link":"https:\/\/www.vskills.in\/certification\/tutorial\/setting-up-multiple-loggers-in-the-logging-module\/","title":{"rendered":"Setting Up Multiple Loggers in the Logging Module"},"content":{"rendered":"\n<p>In larger FastAPI applications, it can be beneficial to create multiple loggers to organize and manage log messages more effectively. This allows you to log different types of information to separate destinations or with different levels of detail.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Creating Multiple Loggers<\/strong><\/h2>\n\n\n\n<p><strong>Create a Logging Module:<\/strong> <\/p>\n\n\n\n<p>Python<\/p>\n\n\n\n<p>import logging<\/p>\n\n\n\n<p>def get_logger(name):<br>logger = logging.getLogger(name)<br>logger.setLevel(logging.DEBUG)<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code># Create a handler and formatter\nhandler = logging.StreamHandler()\nformatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')\nhandler.setFormatter(formatter)\n\nlogger.addHandler(handler)\nreturn logger<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Use the Logging Module in Your Application:<\/strong> <\/h2>\n\n\n\n<p>Python<\/p>\n\n\n\n<p>from app.logging import get_logger<\/p>\n\n\n\n<p>logger = get_logger(<strong>name<\/strong>)<\/p>\n\n\n\n<p>@app.get(&#8220;\/&#8221;)<br>def read_root():<br>logger.info(&#8220;Received a GET request to \/&#8221;)<br>return {&#8220;Hello&#8221;: &#8220;World&#8221;}<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Creating Separate Loggers for Different Modules<\/strong><\/h2>\n\n\n\n<p>To create separate loggers for different modules, use unique names when calling <code>get_logger<\/code>:<\/p>\n\n\n\n<p>Python<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code># In module1.py\nlogger1 = get_logger(\"app.module1\")\n\n# In module2.py\nlogger2 = get_logger(\"app.module2\")\n<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Controlling Logging Levels<\/strong><\/h2>\n\n\n\n<p>You can set different logging levels for each logger to control which messages are logged:<\/p>\n\n\n\n<p>Python<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>logger1.setLevel(logging.DEBUG)\nlogger2.setLevel(logging.INFO)\n<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Using Logger Hierarchies<\/strong><\/h2>\n\n\n\n<p>Logger hierarchies allow you to control logging for entire groups of modules. For example, if you have a logger named <code>app.module1<\/code>, all loggers with names starting with <code>app.module1<\/code> will inherit its logging level.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In larger FastAPI applications, it can be beneficial to create multiple loggers to organize and manage log messages more effectively. This allows you to log different types of information to separate destinations or with different levels of detail. Creating Multiple Loggers Create a Logging Module: Python import logging def get_logger(name):logger = logging.getLogger(name)logger.setLevel(logging.DEBUG) Use the Logging&#8230;<\/p>\n","protected":false},"author":16,"featured_media":0,"parent":0,"menu_order":0,"comment_status":"closed","ping_status":"closed","template":"","meta":{"footnotes":""},"categories":[],"tags":[],"class_list":["post-135370","page","type-page","status-publish","hentry"],"acf":[],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v24.5 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Setting Up Multiple Loggers in the Logging Module - Tutorial<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.vskills.in\/certification\/tutorial\/setting-up-multiple-loggers-in-the-logging-module\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Setting Up Multiple Loggers in the Logging Module - Tutorial\" \/>\n<meta property=\"og:description\" content=\"In larger FastAPI applications, it can be beneficial to create multiple loggers to organize and manage log messages more effectively. This allows you to log different types of information to separate destinations or with different levels of detail. Creating Multiple Loggers Create a Logging Module: Python import logging def get_logger(name):logger = logging.getLogger(name)logger.setLevel(logging.DEBUG) Use the Logging...\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.vskills.in\/certification\/tutorial\/setting-up-multiple-loggers-in-the-logging-module\/\" \/>\n<meta property=\"og:site_name\" content=\"Tutorial\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/vskills.in\/\" \/>\n<meta name=\"twitter:label1\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data1\" content=\"1 minute\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.vskills.in\/certification\/tutorial\/setting-up-multiple-loggers-in-the-logging-module\/\",\"url\":\"https:\/\/www.vskills.in\/certification\/tutorial\/setting-up-multiple-loggers-in-the-logging-module\/\",\"name\":\"Setting Up Multiple Loggers in the Logging Module - Tutorial\",\"isPartOf\":{\"@id\":\"https:\/\/www.vskills.in\/certification\/tutorial\/#website\"},\"datePublished\":\"2024-09-06T06:13:16+00:00\",\"breadcrumb\":{\"@id\":\"https:\/\/www.vskills.in\/certification\/tutorial\/setting-up-multiple-loggers-in-the-logging-module\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.vskills.in\/certification\/tutorial\/setting-up-multiple-loggers-in-the-logging-module\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.vskills.in\/certification\/tutorial\/setting-up-multiple-loggers-in-the-logging-module\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/www.vskills.in\/certification\/tutorial\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Setting Up Multiple Loggers in the Logging Module\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/www.vskills.in\/certification\/tutorial\/#website\",\"url\":\"https:\/\/www.vskills.in\/certification\/tutorial\/\",\"name\":\"Tutorial\",\"description\":\"Vskills - A initiative in elearning and certification\",\"publisher\":{\"@id\":\"https:\/\/www.vskills.in\/certification\/tutorial\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/www.vskills.in\/certification\/tutorial\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/www.vskills.in\/certification\/tutorial\/#organization\",\"name\":\"Vskills\",\"url\":\"https:\/\/www.vskills.in\/certification\/tutorial\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/www.vskills.in\/certification\/tutorial\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/www.vskills.in\/certification\/tutorial\/wp-content\/uploads\/2017\/07\/vskills-min-logo.jpg\",\"contentUrl\":\"https:\/\/www.vskills.in\/certification\/tutorial\/wp-content\/uploads\/2017\/07\/vskills-min-logo.jpg\",\"width\":73,\"height\":55,\"caption\":\"Vskills\"},\"image\":{\"@id\":\"https:\/\/www.vskills.in\/certification\/tutorial\/#\/schema\/logo\/image\/\"},\"sameAs\":[\"https:\/\/www.facebook.com\/vskills.in\/\",\"https:\/\/x.com\/vskills_in\",\"https:\/\/www.linkedin.com\/company-beta\/1371554\/\",\"https:\/\/www.youtube.com\/channel\/UCMWnscxPwRF_PqXo9B7q_Tw\"]}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Setting Up Multiple Loggers in the Logging Module - Tutorial","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.vskills.in\/certification\/tutorial\/setting-up-multiple-loggers-in-the-logging-module\/","og_locale":"en_US","og_type":"article","og_title":"Setting Up Multiple Loggers in the Logging Module - Tutorial","og_description":"In larger FastAPI applications, it can be beneficial to create multiple loggers to organize and manage log messages more effectively. This allows you to log different types of information to separate destinations or with different levels of detail. Creating Multiple Loggers Create a Logging Module: Python import logging def get_logger(name):logger = logging.getLogger(name)logger.setLevel(logging.DEBUG) Use the Logging...","og_url":"https:\/\/www.vskills.in\/certification\/tutorial\/setting-up-multiple-loggers-in-the-logging-module\/","og_site_name":"Tutorial","article_publisher":"https:\/\/www.facebook.com\/vskills.in\/","twitter_misc":{"Est. reading time":"1 minute"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/www.vskills.in\/certification\/tutorial\/setting-up-multiple-loggers-in-the-logging-module\/","url":"https:\/\/www.vskills.in\/certification\/tutorial\/setting-up-multiple-loggers-in-the-logging-module\/","name":"Setting Up Multiple Loggers in the Logging Module - Tutorial","isPartOf":{"@id":"https:\/\/www.vskills.in\/certification\/tutorial\/#website"},"datePublished":"2024-09-06T06:13:16+00:00","breadcrumb":{"@id":"https:\/\/www.vskills.in\/certification\/tutorial\/setting-up-multiple-loggers-in-the-logging-module\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.vskills.in\/certification\/tutorial\/setting-up-multiple-loggers-in-the-logging-module\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.vskills.in\/certification\/tutorial\/setting-up-multiple-loggers-in-the-logging-module\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.vskills.in\/certification\/tutorial\/"},{"@type":"ListItem","position":2,"name":"Setting Up Multiple Loggers in the Logging Module"}]},{"@type":"WebSite","@id":"https:\/\/www.vskills.in\/certification\/tutorial\/#website","url":"https:\/\/www.vskills.in\/certification\/tutorial\/","name":"Tutorial","description":"Vskills - A initiative in elearning and certification","publisher":{"@id":"https:\/\/www.vskills.in\/certification\/tutorial\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.vskills.in\/certification\/tutorial\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/www.vskills.in\/certification\/tutorial\/#organization","name":"Vskills","url":"https:\/\/www.vskills.in\/certification\/tutorial\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.vskills.in\/certification\/tutorial\/#\/schema\/logo\/image\/","url":"https:\/\/www.vskills.in\/certification\/tutorial\/wp-content\/uploads\/2017\/07\/vskills-min-logo.jpg","contentUrl":"https:\/\/www.vskills.in\/certification\/tutorial\/wp-content\/uploads\/2017\/07\/vskills-min-logo.jpg","width":73,"height":55,"caption":"Vskills"},"image":{"@id":"https:\/\/www.vskills.in\/certification\/tutorial\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/vskills.in\/","https:\/\/x.com\/vskills_in","https:\/\/www.linkedin.com\/company-beta\/1371554\/","https:\/\/www.youtube.com\/channel\/UCMWnscxPwRF_PqXo9B7q_Tw"]}]}},"_links":{"self":[{"href":"https:\/\/www.vskills.in\/certification\/tutorial\/wp-json\/wp\/v2\/pages\/135370","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.vskills.in\/certification\/tutorial\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/www.vskills.in\/certification\/tutorial\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/www.vskills.in\/certification\/tutorial\/wp-json\/wp\/v2\/users\/16"}],"replies":[{"embeddable":true,"href":"https:\/\/www.vskills.in\/certification\/tutorial\/wp-json\/wp\/v2\/comments?post=135370"}],"version-history":[{"count":2,"href":"https:\/\/www.vskills.in\/certification\/tutorial\/wp-json\/wp\/v2\/pages\/135370\/revisions"}],"predecessor-version":[{"id":135395,"href":"https:\/\/www.vskills.in\/certification\/tutorial\/wp-json\/wp\/v2\/pages\/135370\/revisions\/135395"}],"wp:attachment":[{"href":"https:\/\/www.vskills.in\/certification\/tutorial\/wp-json\/wp\/v2\/media?parent=135370"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.vskills.in\/certification\/tutorial\/wp-json\/wp\/v2\/categories?post=135370"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.vskills.in\/certification\/tutorial\/wp-json\/wp\/v2\/tags?post=135370"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}