{"id":135359,"date":"2024-09-06T11:43:24","date_gmt":"2024-09-06T06:13:24","guid":{"rendered":"https:\/\/www.vskills.in\/certification\/tutorial\/?page_id=135359"},"modified":"2024-09-06T11:43:25","modified_gmt":"2024-09-06T06:13:25","slug":"python-logging-loggers-handlers-and-formatters","status":"publish","type":"page","link":"https:\/\/www.vskills.in\/certification\/tutorial\/python-logging-loggers-handlers-and-formatters\/","title":{"rendered":"Python Logging: Loggers, Handlers, and Formatters"},"content":{"rendered":"\n<p>Logging is a crucial aspect of any software application, providing valuable insights into its behavior and helping to diagnose and troubleshoot issues. Python&#8217;s built-in <code>logging<\/code> module offers a flexible and efficient way to implement logging in your FastAPI applications. This guide will explore the key components of Python logging: loggers, handlers, and formatters.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Loggers<\/strong><\/h2>\n\n\n\n<p>A logger is an object that represents a logging channel. It&#8217;s used to emit log messages at different levels of severity, such as DEBUG, INFO, WARNING, ERROR, and CRITICAL. You can create loggers using the <code>getLogger<\/code> function:<\/p>\n\n\n\n<p>Python<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import logging\n\nlogger = logging.getLogger(__name__)\n<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Handlers<\/strong><\/h2>\n\n\n\n<p>Handlers are responsible for sending log messages to specific destinations, such as the console, a file, or a network socket. You can create handlers using classes like <code>StreamHandler<\/code> (for console output), <code>FileHandler<\/code> (for file output), and <code>SMTPHandler<\/code> (for email output).<\/p>\n\n\n\n<p><strong>Example:<\/strong><\/p>\n\n\n\n<p>Python<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>handler = logging.StreamHandler()\nlogger.addHandler(handler)\n<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Formatters<\/strong><\/h2>\n\n\n\n<p>Formatters control the appearance of log messages. They define the layout and content of the messages. You can create formatters using the <code>Formatter<\/code> class:<\/p>\n\n\n\n<p>Python<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')\nhandler.setFormatter(formatter)\n<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Logging Levels<\/strong><\/h2>\n\n\n\n<p>Python&#8217;s logging module defines five logging levels:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>DEBUG<\/code>: Most detailed level, typically used for debugging.<\/li>\n\n\n\n<li><code>INFO<\/code>: Provides general information about the application&#8217;s behavior.<\/li>\n\n\n\n<li><code>WARNING<\/code>: Indicates potential problems or unexpected events.<\/li>\n\n\n\n<li><code>ERROR<\/code>: Signals an error that doesn&#8217;t prevent the application from continuing.<\/li>\n\n\n\n<li><code>CRITICAL<\/code>: Indicates a serious error that might prevent the application from continuing.<\/li>\n<\/ul>\n\n\n\n<p>To set the logging level for a logger, use the <code>setLevel<\/code> method:<\/p>\n\n\n\n<p>Python<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>logger.setLevel(logging.DEBUG)\n<\/code><\/pre>\n\n\n\n<p><strong>Example:<\/strong><\/p>\n\n\n\n<p>Python<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import logging\n\nlogger = logging.getLogger(__name__)\nlogger.setLevel(logging.DEBUG)\n\nhandler = logging.StreamHandler()\nformatter = logging.Formatter('%(asctime)s &nbsp;  - %(name)s - %(levelname)s - %(message)s')\nhandler.setFormatter(formatter)\nlogger.addHandler(handler) &nbsp; \n\nlogger.debug(\"This is a debug message\")\nlogger.info(\"This is an info message\")\nlogger.warning(\"This is a warning message\")\nlogger.error(\"This is an error message\")\nlogger.critical(\"This is a critical &nbsp;  message\")\n<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Logging is a crucial aspect of any software application, providing valuable insights into its behavior and helping to diagnose and troubleshoot issues. Python&#8217;s built-in logging module offers a flexible and efficient way to implement logging in your FastAPI applications. This guide will explore the key components of Python logging: loggers, handlers, and formatters. Loggers A&#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-135359","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>Python Logging: Loggers, Handlers, and Formatters - 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\/python-logging-loggers-handlers-and-formatters\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Python Logging: Loggers, Handlers, and Formatters - Tutorial\" \/>\n<meta property=\"og:description\" content=\"Logging is a crucial aspect of any software application, providing valuable insights into its behavior and helping to diagnose and troubleshoot issues. Python&#8217;s built-in logging module offers a flexible and efficient way to implement logging in your FastAPI applications. This guide will explore the key components of Python logging: loggers, handlers, and formatters. Loggers A...\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.vskills.in\/certification\/tutorial\/python-logging-loggers-handlers-and-formatters\/\" \/>\n<meta property=\"og:site_name\" content=\"Tutorial\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/vskills.in\/\" \/>\n<meta property=\"article:modified_time\" content=\"2024-09-06T06:13:25+00:00\" \/>\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\/python-logging-loggers-handlers-and-formatters\/\",\"url\":\"https:\/\/www.vskills.in\/certification\/tutorial\/python-logging-loggers-handlers-and-formatters\/\",\"name\":\"Python Logging: Loggers, Handlers, and Formatters - Tutorial\",\"isPartOf\":{\"@id\":\"https:\/\/www.vskills.in\/certification\/tutorial\/#website\"},\"datePublished\":\"2024-09-06T06:13:24+00:00\",\"dateModified\":\"2024-09-06T06:13:25+00:00\",\"breadcrumb\":{\"@id\":\"https:\/\/www.vskills.in\/certification\/tutorial\/python-logging-loggers-handlers-and-formatters\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.vskills.in\/certification\/tutorial\/python-logging-loggers-handlers-and-formatters\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.vskills.in\/certification\/tutorial\/python-logging-loggers-handlers-and-formatters\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/www.vskills.in\/certification\/tutorial\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Python Logging: Loggers, Handlers, and Formatters\"}]},{\"@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":"Python Logging: Loggers, Handlers, and Formatters - 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\/python-logging-loggers-handlers-and-formatters\/","og_locale":"en_US","og_type":"article","og_title":"Python Logging: Loggers, Handlers, and Formatters - Tutorial","og_description":"Logging is a crucial aspect of any software application, providing valuable insights into its behavior and helping to diagnose and troubleshoot issues. Python&#8217;s built-in logging module offers a flexible and efficient way to implement logging in your FastAPI applications. This guide will explore the key components of Python logging: loggers, handlers, and formatters. Loggers A...","og_url":"https:\/\/www.vskills.in\/certification\/tutorial\/python-logging-loggers-handlers-and-formatters\/","og_site_name":"Tutorial","article_publisher":"https:\/\/www.facebook.com\/vskills.in\/","article_modified_time":"2024-09-06T06:13:25+00:00","twitter_misc":{"Est. reading time":"1 minute"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/www.vskills.in\/certification\/tutorial\/python-logging-loggers-handlers-and-formatters\/","url":"https:\/\/www.vskills.in\/certification\/tutorial\/python-logging-loggers-handlers-and-formatters\/","name":"Python Logging: Loggers, Handlers, and Formatters - Tutorial","isPartOf":{"@id":"https:\/\/www.vskills.in\/certification\/tutorial\/#website"},"datePublished":"2024-09-06T06:13:24+00:00","dateModified":"2024-09-06T06:13:25+00:00","breadcrumb":{"@id":"https:\/\/www.vskills.in\/certification\/tutorial\/python-logging-loggers-handlers-and-formatters\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.vskills.in\/certification\/tutorial\/python-logging-loggers-handlers-and-formatters\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.vskills.in\/certification\/tutorial\/python-logging-loggers-handlers-and-formatters\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.vskills.in\/certification\/tutorial\/"},{"@type":"ListItem","position":2,"name":"Python Logging: Loggers, Handlers, and Formatters"}]},{"@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\/135359","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=135359"}],"version-history":[{"count":2,"href":"https:\/\/www.vskills.in\/certification\/tutorial\/wp-json\/wp\/v2\/pages\/135359\/revisions"}],"predecessor-version":[{"id":135400,"href":"https:\/\/www.vskills.in\/certification\/tutorial\/wp-json\/wp\/v2\/pages\/135359\/revisions\/135400"}],"wp:attachment":[{"href":"https:\/\/www.vskills.in\/certification\/tutorial\/wp-json\/wp\/v2\/media?parent=135359"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.vskills.in\/certification\/tutorial\/wp-json\/wp\/v2\/categories?post=135359"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.vskills.in\/certification\/tutorial\/wp-json\/wp\/v2\/tags?post=135359"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}