{"id":130407,"date":"2023-05-11T10:35:06","date_gmt":"2023-05-11T05:05:06","guid":{"rendered":"https:\/\/www.vskills.in\/certification\/tutorial\/?page_id=130407"},"modified":"2024-04-12T14:34:00","modified_gmt":"2024-04-12T09:04:00","slug":"logging-module","status":"publish","type":"page","link":"https:\/\/www.vskills.in\/certification\/tutorial\/logging-module\/","title":{"rendered":"Logging module in Python"},"content":{"rendered":"\n<p><strong>Logging module in Python<\/strong><\/p>\n\n\n\n<p>The <code>logging<\/code> module in Python provides a flexible and powerful way to log messages from your Python code. It allows you to easily record information about the behavior of your application and makes it easier to diagnose problems when they occur.<\/p>\n\n\n\n<p>Here are some key concepts and features of the <code>logging<\/code> module:<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li>Loggers: A logger is the main object in the logging module. It is used to emit log messages. You can create multiple loggers to organize your log messages into different categories.<\/li>\n\n\n\n<li>Handlers: A handler is responsible for taking the log messages emitted by a logger and processing them. The logging module provides several built-in handlers, such as <code>StreamHandler<\/code>, <code>FileHandler<\/code>, <code>RotatingFileHandler<\/code>, and <code>SMTPHandler<\/code>.<\/li>\n\n\n\n<li>Formatters: A formatter is used to specify the format of the log messages emitted by a handler. The logging module provides several built-in formatters, such as <code>Formatter<\/code> and <code>JSONFormatter<\/code>.<\/li>\n\n\n\n<li>Levels: Loggers and handlers have levels associated with them. When a logger or handler is created, you can specify a level. Only log messages with a level that is greater than or equal to the level of the logger or handler will be emitted. The logging module provides several built-in levels, such as <code>DEBUG<\/code>, <code>INFO<\/code>, <code>WARNING<\/code>, <code>ERROR<\/code>, and <code>CRITICAL<\/code>.<\/li>\n<\/ol>\n\n\n\n<p>Here is an example of how to use the <code>logging<\/code> module to emit log messages:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">pythonCopy code<code>import logging\n\n# create a logger\nlogger = logging.getLogger('my_logger')\nlogger.setLevel(logging.DEBUG)\n\n# create a handler\nhandler = logging.StreamHandler()\nhandler.setLevel(logging.DEBUG)\n\n# create a formatter\nformatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')\nhandler.setFormatter(formatter)\n\n# add the handler to the logger\nlogger.addHandler(handler)\n\n# emit log messages\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 message')\n<\/code><\/pre>\n\n\n\n<p>This code creates a logger named <code>my_logger<\/code> and sets its level to <code>DEBUG<\/code>, which means that it will emit all log messages. It also creates a <code>StreamHandler<\/code> that sends log messages to the console, sets its level to <code>DEBUG<\/code>, and sets a formatter that includes the time stamp, logger name, log level, and message. Finally, the code adds the handler to the logger and emits some log messages with different levels.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Apply for Python Certification!<\/strong><\/h3>\n\n\n\n<p><a href=\"https:\/\/www.vskills.in\/certification\/certified-python-developer\">https:\/\/www.vskills.in\/certification\/certified-python-developer<\/a><\/p>\n\n\n\n<h4 class=\"wp-block-heading\"><strong><a href=\"https:\/\/www.vskills.in\/certification\/tutorial\/python-developer\/\" target=\"_blank\" rel=\"noreferrer noopener\">Back to Tutorials<\/a><\/strong><\/h4>\n","protected":false},"excerpt":{"rendered":"<p>Logging module in Python The logging module in Python provides a flexible and powerful way to log messages from your Python code. It allows you to easily record information about the behavior of your application and makes it easier to diagnose problems when they occur. Here are some key concepts and features of the logging&#8230;<\/p>\n","protected":false},"author":23,"featured_media":0,"parent":0,"menu_order":0,"comment_status":"closed","ping_status":"closed","template":"","meta":{"footnotes":""},"categories":[],"tags":[],"class_list":["post-130407","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>Logging module in Python - Tutorial<\/title>\n<meta name=\"description\" content=\"logging module\" \/>\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\/logging-module\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Logging module in Python - Tutorial\" \/>\n<meta property=\"og:description\" content=\"logging module\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.vskills.in\/certification\/tutorial\/logging-module\/\" \/>\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-04-12T09:04:00+00:00\" \/>\n<meta name=\"twitter:label1\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data1\" content=\"2 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.vskills.in\/certification\/tutorial\/logging-module\/\",\"url\":\"https:\/\/www.vskills.in\/certification\/tutorial\/logging-module\/\",\"name\":\"Logging module in Python - Tutorial\",\"isPartOf\":{\"@id\":\"https:\/\/www.vskills.in\/certification\/tutorial\/#website\"},\"datePublished\":\"2023-05-11T05:05:06+00:00\",\"dateModified\":\"2024-04-12T09:04:00+00:00\",\"description\":\"logging module\",\"breadcrumb\":{\"@id\":\"https:\/\/www.vskills.in\/certification\/tutorial\/logging-module\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.vskills.in\/certification\/tutorial\/logging-module\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.vskills.in\/certification\/tutorial\/logging-module\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/www.vskills.in\/certification\/tutorial\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Logging module in Python\"}]},{\"@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":"Logging module in Python - Tutorial","description":"logging module","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\/logging-module\/","og_locale":"en_US","og_type":"article","og_title":"Logging module in Python - Tutorial","og_description":"logging module","og_url":"https:\/\/www.vskills.in\/certification\/tutorial\/logging-module\/","og_site_name":"Tutorial","article_publisher":"https:\/\/www.facebook.com\/vskills.in\/","article_modified_time":"2024-04-12T09:04:00+00:00","twitter_misc":{"Est. reading time":"2 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/www.vskills.in\/certification\/tutorial\/logging-module\/","url":"https:\/\/www.vskills.in\/certification\/tutorial\/logging-module\/","name":"Logging module in Python - Tutorial","isPartOf":{"@id":"https:\/\/www.vskills.in\/certification\/tutorial\/#website"},"datePublished":"2023-05-11T05:05:06+00:00","dateModified":"2024-04-12T09:04:00+00:00","description":"logging module","breadcrumb":{"@id":"https:\/\/www.vskills.in\/certification\/tutorial\/logging-module\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.vskills.in\/certification\/tutorial\/logging-module\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.vskills.in\/certification\/tutorial\/logging-module\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.vskills.in\/certification\/tutorial\/"},{"@type":"ListItem","position":2,"name":"Logging module in Python"}]},{"@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\/130407","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\/23"}],"replies":[{"embeddable":true,"href":"https:\/\/www.vskills.in\/certification\/tutorial\/wp-json\/wp\/v2\/comments?post=130407"}],"version-history":[{"count":2,"href":"https:\/\/www.vskills.in\/certification\/tutorial\/wp-json\/wp\/v2\/pages\/130407\/revisions"}],"predecessor-version":[{"id":130413,"href":"https:\/\/www.vskills.in\/certification\/tutorial\/wp-json\/wp\/v2\/pages\/130407\/revisions\/130413"}],"wp:attachment":[{"href":"https:\/\/www.vskills.in\/certification\/tutorial\/wp-json\/wp\/v2\/media?parent=130407"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.vskills.in\/certification\/tutorial\/wp-json\/wp\/v2\/categories?post=130407"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.vskills.in\/certification\/tutorial\/wp-json\/wp\/v2\/tags?post=130407"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}