{"id":135374,"date":"2024-09-06T11:42:58","date_gmt":"2024-09-06T06:12:58","guid":{"rendered":"https:\/\/www.vskills.in\/certification\/tutorial\/?page_id=135374"},"modified":"2024-09-06T11:42:58","modified_gmt":"2024-09-06T06:12:58","slug":"adding-json-formatted-log-files","status":"publish","type":"page","link":"https:\/\/www.vskills.in\/certification\/tutorial\/adding-json-formatted-log-files\/","title":{"rendered":"Adding JSON-Formatted Log Files"},"content":{"rendered":"\n<p>In addition to plain text log files, you can also log information in JSON format. JSON-formatted logs offer several advantages, including:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Structured Data:<\/strong> JSON provides a structured format that is easier to parse and analyze using tools like Elasticsearch or Kibana.<\/li>\n\n\n\n<li><strong>Machine Readability:<\/strong> JSON is machine-readable, making it suitable for automated processing and analysis.<\/li>\n\n\n\n<li><strong>Flexibility:<\/strong> JSON can represent a wide range of data types and structures.<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Creating a JSON Formatter<\/strong><\/h2>\n\n\n\n<p><strong>Import the JSON Formatter:<\/strong><\/p>\n\n\n\n<p>Python<\/p>\n\n\n\n<p>import logging.json<\/p>\n\n\n\n<p><strong>Create a JSON Formatter:<\/strong><\/p>\n\n\n\n<p>Python<\/p>\n\n\n\n<p>json_formatter = logging.json.JSONFormatter(<br>&#8216;%(asctime)s %(name)s %(levelname)s %(message)s&#8217;,<br>ensure_ascii=False<br>)<\/p>\n\n\n\n<p>This creates a JSON formatter that includes the timestamp, logger name, log level, and message in the JSON output. The <code class=\"\">ensure_ascii<\/code> parameter ensures that non-ASCII characters are properly encoded.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Adding the JSON Formatter to a Handler<\/strong><\/h2>\n\n\n\n<p><strong>Create a File Handler:<\/strong><\/p>\n\n\n\n<p>Python<\/p>\n\n\n\n<p>file_handler = logging.FileHandler(&#8216;app.json&#8217;)<\/p>\n\n\n\n<p><strong>Set the Formatter:<\/strong><\/p>\n\n\n\n<p>Python<\/p>\n\n\n\n<p>file_handler.setFormatter(json_formatter)<\/p>\n\n\n\n<p><strong>Add the Handler to the Logger:<\/strong><\/p>\n\n\n\n<p>Python<\/p>\n\n\n\n<p>logger.addHandler(file_handler)<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Logging JSON Objects<\/strong><\/h2>\n\n\n\n<p>To log JSON objects directly, use the <code class=\"\">json.dumps<\/code> function:<\/p>\n\n\n\n<p>Python<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import json\n\ndata = {\"message\": \"This is a JSON object\"}\nlogger.info(json.dumps(data))\n<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Additional Considerations<\/strong><\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Logging Level:<\/strong> Adjust the logging level for the file handler to control which messages are written to the log file.<\/li>\n\n\n\n<li><strong>Log Rotation:<\/strong> Consider using rotating file handlers to manage log file size.<\/li>\n\n\n\n<li><strong>Log Analysis Tools:<\/strong> Use tools like Elasticsearch or Kibana to analyze JSON-formatted logs.<\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>In addition to plain text log files, you can also log information in JSON format. JSON-formatted logs offer several advantages, including: Creating a JSON Formatter Import the JSON Formatter: Python import logging.json Create a JSON Formatter: Python json_formatter = logging.json.JSONFormatter(&#8216;%(asctime)s %(name)s %(levelname)s %(message)s&#8217;,ensure_ascii=False) This creates a JSON formatter that includes the timestamp, logger name, log&#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-135374","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>Adding JSON-Formatted Log Files - 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\/adding-json-formatted-log-files\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Adding JSON-Formatted Log Files - Tutorial\" \/>\n<meta property=\"og:description\" content=\"In addition to plain text log files, you can also log information in JSON format. JSON-formatted logs offer several advantages, including: Creating a JSON Formatter Import the JSON Formatter: Python import logging.json Create a JSON Formatter: Python json_formatter = logging.json.JSONFormatter(&#8216;%(asctime)s %(name)s %(levelname)s %(message)s&#8217;,ensure_ascii=False) This creates a JSON formatter that includes the timestamp, logger name, log...\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.vskills.in\/certification\/tutorial\/adding-json-formatted-log-files\/\" \/>\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\/adding-json-formatted-log-files\/\",\"url\":\"https:\/\/www.vskills.in\/certification\/tutorial\/adding-json-formatted-log-files\/\",\"name\":\"Adding JSON-Formatted Log Files - Tutorial\",\"isPartOf\":{\"@id\":\"https:\/\/www.vskills.in\/certification\/tutorial\/#website\"},\"datePublished\":\"2024-09-06T06:12:58+00:00\",\"breadcrumb\":{\"@id\":\"https:\/\/www.vskills.in\/certification\/tutorial\/adding-json-formatted-log-files\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.vskills.in\/certification\/tutorial\/adding-json-formatted-log-files\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.vskills.in\/certification\/tutorial\/adding-json-formatted-log-files\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/www.vskills.in\/certification\/tutorial\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Adding JSON-Formatted Log Files\"}]},{\"@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":"Adding JSON-Formatted Log Files - 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\/adding-json-formatted-log-files\/","og_locale":"en_US","og_type":"article","og_title":"Adding JSON-Formatted Log Files - Tutorial","og_description":"In addition to plain text log files, you can also log information in JSON format. JSON-formatted logs offer several advantages, including: Creating a JSON Formatter Import the JSON Formatter: Python import logging.json Create a JSON Formatter: Python json_formatter = logging.json.JSONFormatter(&#8216;%(asctime)s %(name)s %(levelname)s %(message)s&#8217;,ensure_ascii=False) This creates a JSON formatter that includes the timestamp, logger name, log...","og_url":"https:\/\/www.vskills.in\/certification\/tutorial\/adding-json-formatted-log-files\/","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\/adding-json-formatted-log-files\/","url":"https:\/\/www.vskills.in\/certification\/tutorial\/adding-json-formatted-log-files\/","name":"Adding JSON-Formatted Log Files - Tutorial","isPartOf":{"@id":"https:\/\/www.vskills.in\/certification\/tutorial\/#website"},"datePublished":"2024-09-06T06:12:58+00:00","breadcrumb":{"@id":"https:\/\/www.vskills.in\/certification\/tutorial\/adding-json-formatted-log-files\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.vskills.in\/certification\/tutorial\/adding-json-formatted-log-files\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.vskills.in\/certification\/tutorial\/adding-json-formatted-log-files\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.vskills.in\/certification\/tutorial\/"},{"@type":"ListItem","position":2,"name":"Adding JSON-Formatted Log Files"}]},{"@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\/135374","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=135374"}],"version-history":[{"count":2,"href":"https:\/\/www.vskills.in\/certification\/tutorial\/wp-json\/wp\/v2\/pages\/135374\/revisions"}],"predecessor-version":[{"id":135383,"href":"https:\/\/www.vskills.in\/certification\/tutorial\/wp-json\/wp\/v2\/pages\/135374\/revisions\/135383"}],"wp:attachment":[{"href":"https:\/\/www.vskills.in\/certification\/tutorial\/wp-json\/wp\/v2\/media?parent=135374"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.vskills.in\/certification\/tutorial\/wp-json\/wp\/v2\/categories?post=135374"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.vskills.in\/certification\/tutorial\/wp-json\/wp\/v2\/tags?post=135374"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}