{"id":183228,"date":"2019-06-28T13:25:10","date_gmt":"2019-06-28T13:25:10","guid":{"rendered":"https:\/\/webkul.com\/blog\/?p=183228"},"modified":"2019-06-28T13:25:11","modified_gmt":"2019-06-28T13:25:11","slug":"create-a-custom-log-file-in-magento-2-using-monolog-and-logging-data-into-it","status":"publish","type":"post","link":"https:\/\/webkul.com\/blog\/create-a-custom-log-file-in-magento-2-using-monolog-and-logging-data-into-it\/","title":{"rendered":"Create a custom log file in Magento 2 using Monolog and logging data into it."},"content":{"rendered":"\n<p>Hello everyone, Today we will learn to create a custom log file programmatically and log data into the custom log file.<\/p>\n\n\n\n<p>First of all we need to create handlers and loggers for custom log file. For that, create a Logger.php class in Logger folder in your custom Module as shown below- <\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\">&lt;?php\nnamespace YourNamespace\\YourModule\\Logger;\n\nclass Logger extends \\Monolog\\Logger\n{\n}\n?&gt;<\/pre>\n\n\n\n<p>After this, Create Handler class, in which we will define the custom Log file name as shown below-<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\">&lt;?php\nnamespace YourNamespace\\YourModule\\Logger;\n\nuse Monolog\\Logger;\n\nclass Handler extends \\Magento\\Framework\\Logger\\Handler\\Base\n{\n    protected $loggerType = Logger::INFO;\n\n    protected $fileName = &#039;\/var\/log\/customLog.log&#039;;\n}<\/pre>\n\n\n\n<p>Last step is to define the monolog in di.xml in etc folder of your custom Module &#8211;<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\">&lt;?xml version=&quot;1.0&quot;?&gt;\n&lt;config xmlns:xsi=&quot;http:\/\/www.w3.org\/2001\/XMLSchema-instance&quot; \n    xsi:noNamespaceSchemaLocation=&quot;..\/..\/..\/..\/..\/lib\/internal\/Magento\/Framework\/ObjectManager\/etc\/config.xsd&quot;&gt;\n    &lt;type name=&quot;YourNamespace\\YourModule\\Logger\\Handler&quot;&gt;\n        &lt;arguments&gt;\n            &lt;argument name=&quot;filesystem&quot; xsi:type=&quot;object&quot;&gt;Magento\\Framework\\Filesystem\\Driver\\File&lt;\/argument&gt;\n        &lt;\/arguments&gt;\n    &lt;\/type&gt;\n    &lt;type name=&quot;YourNamespace\\YourModule\\Logger\\Logger&quot;&gt;\n        &lt;arguments&gt;\n            &lt;argument name=&quot;name&quot; xsi:type=&quot;string&quot;&gt;myLoggerName&lt;\/argument&gt;\n            &lt;argument name=&quot;handlers&quot;  xsi:type=&quot;array&quot;&gt;\n                &lt;item name=&quot;system&quot; xsi:type=&quot;object&quot;&gt;YourNamespace\\YourModule\\Logger\\Handler&lt;\/item&gt;\n            &lt;\/argument&gt;\n        &lt;\/arguments&gt;\n    &lt;\/type&gt;\n&lt;\/config&gt;<\/pre>\n\n\n\n<p>This is it, now just use the logger in your Magento 2 file, to print the logs in the custom created log file with the following code-<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\">&lt;?php\nnamespace YourNamespace\\YourModule\\Model;\n\nclass TestModel\n{\n    protected $_logger;\n\n    public function __construct(\n        \\YourNamespace\\YourModule\\Logger\\Logger $logger\n    ) {\n        $this-&gt;_logger = $logger;\n    }\n\n    public function testFunction()\n    {\n        $this-&gt;_logger-&gt;info(&#039;This is test Data to be print in customLog.log file&#039;);\n    }\n}<\/pre>\n\n\n\n<p>Now you can find the above printed data in var\/log\/customLog.log file. <\/p>\n\n\n\n<p>You can use this for logging any required data in your custom Module.<\/p>\n\n\n\n<p>This is all for now, I ll be back soon.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Hello everyone, Today we will learn to create a custom log file programmatically and log data into the custom log file. First of all we need to create handlers and loggers for custom log file. For that, create a Logger.php class in Logger folder in your custom Module as shown below- After this, Create Handler <a href=\"https:\/\/webkul.com\/blog\/create-a-custom-log-file-in-magento-2-using-monolog-and-logging-data-into-it\/\">[&#8230;]<\/a><\/p>\n","protected":false},"author":167,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-183228","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v24.5 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Create a custom log file in Magento 2 using Monolog and logging data into it. - Webkul Blog<\/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:\/\/webkul.com\/blog\/create-a-custom-log-file-in-magento-2-using-monolog-and-logging-data-into-it\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Create a custom log file in Magento 2 using Monolog and logging data into it. - Webkul Blog\" \/>\n<meta property=\"og:description\" content=\"Hello everyone, Today we will learn to create a custom log file programmatically and log data into the custom log file. First of all we need to create handlers and loggers for custom log file. For that, create a Logger.php class in Logger folder in your custom Module as shown below- After this, Create Handler [...]\" \/>\n<meta property=\"og:url\" content=\"https:\/\/webkul.com\/blog\/create-a-custom-log-file-in-magento-2-using-monolog-and-logging-data-into-it\/\" \/>\n<meta property=\"og:site_name\" content=\"Webkul Blog\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/webkul\/\" \/>\n<meta property=\"article:published_time\" content=\"2019-06-28T13:25:10+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2019-06-28T13:25:11+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/cdnblog.webkul.com\/blog\/wp-content\/uploads\/2021\/08\/webkul-og.png\" \/>\n\t<meta property=\"og:image:width\" content=\"1200\" \/>\n\t<meta property=\"og:image:height\" content=\"630\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/png\" \/>\n<meta name=\"author\" content=\"Hemant Jain\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@webkul\" \/>\n<meta name=\"twitter:site\" content=\"@webkul\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Hemant Jain\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"2 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/webkul.com\/blog\/create-a-custom-log-file-in-magento-2-using-monolog-and-logging-data-into-it\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/webkul.com\/blog\/create-a-custom-log-file-in-magento-2-using-monolog-and-logging-data-into-it\/\"},\"author\":{\"name\":\"Hemant Jain\",\"@id\":\"https:\/\/webkul.com\/blog\/#\/schema\/person\/a824ac6c5c14d32569263100b85f47c6\"},\"headline\":\"Create a custom log file in Magento 2 using Monolog and logging data into it.\",\"datePublished\":\"2019-06-28T13:25:10+00:00\",\"dateModified\":\"2019-06-28T13:25:11+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/webkul.com\/blog\/create-a-custom-log-file-in-magento-2-using-monolog-and-logging-data-into-it\/\"},\"wordCount\":165,\"commentCount\":0,\"publisher\":{\"@id\":\"https:\/\/webkul.com\/blog\/#organization\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\/\/webkul.com\/blog\/create-a-custom-log-file-in-magento-2-using-monolog-and-logging-data-into-it\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/webkul.com\/blog\/create-a-custom-log-file-in-magento-2-using-monolog-and-logging-data-into-it\/\",\"url\":\"https:\/\/webkul.com\/blog\/create-a-custom-log-file-in-magento-2-using-monolog-and-logging-data-into-it\/\",\"name\":\"Create a custom log file in Magento 2 using Monolog and logging data into it. - Webkul Blog\",\"isPartOf\":{\"@id\":\"https:\/\/webkul.com\/blog\/#website\"},\"datePublished\":\"2019-06-28T13:25:10+00:00\",\"dateModified\":\"2019-06-28T13:25:11+00:00\",\"breadcrumb\":{\"@id\":\"https:\/\/webkul.com\/blog\/create-a-custom-log-file-in-magento-2-using-monolog-and-logging-data-into-it\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/webkul.com\/blog\/create-a-custom-log-file-in-magento-2-using-monolog-and-logging-data-into-it\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/webkul.com\/blog\/create-a-custom-log-file-in-magento-2-using-monolog-and-logging-data-into-it\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/webkul.com\/blog\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Create a custom log file in Magento 2 using Monolog and logging data into it.\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/webkul.com\/blog\/#website\",\"url\":\"https:\/\/webkul.com\/blog\/\",\"name\":\"Webkul Blog\",\"description\":\"\",\"publisher\":{\"@id\":\"https:\/\/webkul.com\/blog\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/webkul.com\/blog\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/webkul.com\/blog\/#organization\",\"name\":\"WebKul Software Private Limited\",\"url\":\"https:\/\/webkul.com\/blog\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/webkul.com\/blog\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/cdnblog.webkul.com\/blog\/wp-content\/uploads\/2021\/08\/webkul-logo-accent-sq.png\",\"contentUrl\":\"https:\/\/cdnblog.webkul.com\/blog\/wp-content\/uploads\/2021\/08\/webkul-logo-accent-sq.png\",\"width\":380,\"height\":380,\"caption\":\"WebKul Software Private Limited\"},\"image\":{\"@id\":\"https:\/\/webkul.com\/blog\/#\/schema\/logo\/image\/\"},\"sameAs\":[\"https:\/\/www.facebook.com\/webkul\/\",\"https:\/\/x.com\/webkul\",\"https:\/\/www.instagram.com\/webkul\/\",\"https:\/\/www.linkedin.com\/company\/webkul\",\"https:\/\/www.youtube.com\/user\/webkul\/\"]},{\"@type\":\"Person\",\"@id\":\"https:\/\/webkul.com\/blog\/#\/schema\/person\/a824ac6c5c14d32569263100b85f47c6\",\"name\":\"Hemant Jain\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/webkul.com\/blog\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/184358b1c25c580a3e32952605156294dedfff2b8b75bdb2a719c8e6a5d318b6?s=96&d=https%3A%2F%2Fcdnblog.webkul.com%2Fblog%2Fwp-content%2Fuploads%2F2019%2F10%2Fmike.png&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/184358b1c25c580a3e32952605156294dedfff2b8b75bdb2a719c8e6a5d318b6?s=96&d=https%3A%2F%2Fcdnblog.webkul.com%2Fblog%2Fwp-content%2Fuploads%2F2019%2F10%2Fmike.png&r=g\",\"caption\":\"Hemant Jain\"},\"url\":\"https:\/\/webkul.com\/blog\/author\/hemant-jain591\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Create a custom log file in Magento 2 using Monolog and logging data into it. - Webkul Blog","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:\/\/webkul.com\/blog\/create-a-custom-log-file-in-magento-2-using-monolog-and-logging-data-into-it\/","og_locale":"en_US","og_type":"article","og_title":"Create a custom log file in Magento 2 using Monolog and logging data into it. - Webkul Blog","og_description":"Hello everyone, Today we will learn to create a custom log file programmatically and log data into the custom log file. First of all we need to create handlers and loggers for custom log file. For that, create a Logger.php class in Logger folder in your custom Module as shown below- After this, Create Handler [...]","og_url":"https:\/\/webkul.com\/blog\/create-a-custom-log-file-in-magento-2-using-monolog-and-logging-data-into-it\/","og_site_name":"Webkul Blog","article_publisher":"https:\/\/www.facebook.com\/webkul\/","article_published_time":"2019-06-28T13:25:10+00:00","article_modified_time":"2019-06-28T13:25:11+00:00","og_image":[{"width":1200,"height":630,"url":"https:\/\/cdnblog.webkul.com\/blog\/wp-content\/uploads\/2021\/08\/webkul-og.png","type":"image\/png"}],"author":"Hemant Jain","twitter_card":"summary_large_image","twitter_creator":"@webkul","twitter_site":"@webkul","twitter_misc":{"Written by":"Hemant Jain","Est. reading time":"2 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/webkul.com\/blog\/create-a-custom-log-file-in-magento-2-using-monolog-and-logging-data-into-it\/#article","isPartOf":{"@id":"https:\/\/webkul.com\/blog\/create-a-custom-log-file-in-magento-2-using-monolog-and-logging-data-into-it\/"},"author":{"name":"Hemant Jain","@id":"https:\/\/webkul.com\/blog\/#\/schema\/person\/a824ac6c5c14d32569263100b85f47c6"},"headline":"Create a custom log file in Magento 2 using Monolog and logging data into it.","datePublished":"2019-06-28T13:25:10+00:00","dateModified":"2019-06-28T13:25:11+00:00","mainEntityOfPage":{"@id":"https:\/\/webkul.com\/blog\/create-a-custom-log-file-in-magento-2-using-monolog-and-logging-data-into-it\/"},"wordCount":165,"commentCount":0,"publisher":{"@id":"https:\/\/webkul.com\/blog\/#organization"},"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/webkul.com\/blog\/create-a-custom-log-file-in-magento-2-using-monolog-and-logging-data-into-it\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/webkul.com\/blog\/create-a-custom-log-file-in-magento-2-using-monolog-and-logging-data-into-it\/","url":"https:\/\/webkul.com\/blog\/create-a-custom-log-file-in-magento-2-using-monolog-and-logging-data-into-it\/","name":"Create a custom log file in Magento 2 using Monolog and logging data into it. - Webkul Blog","isPartOf":{"@id":"https:\/\/webkul.com\/blog\/#website"},"datePublished":"2019-06-28T13:25:10+00:00","dateModified":"2019-06-28T13:25:11+00:00","breadcrumb":{"@id":"https:\/\/webkul.com\/blog\/create-a-custom-log-file-in-magento-2-using-monolog-and-logging-data-into-it\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/webkul.com\/blog\/create-a-custom-log-file-in-magento-2-using-monolog-and-logging-data-into-it\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/webkul.com\/blog\/create-a-custom-log-file-in-magento-2-using-monolog-and-logging-data-into-it\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/webkul.com\/blog\/"},{"@type":"ListItem","position":2,"name":"Create a custom log file in Magento 2 using Monolog and logging data into it."}]},{"@type":"WebSite","@id":"https:\/\/webkul.com\/blog\/#website","url":"https:\/\/webkul.com\/blog\/","name":"Webkul Blog","description":"","publisher":{"@id":"https:\/\/webkul.com\/blog\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/webkul.com\/blog\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/webkul.com\/blog\/#organization","name":"WebKul Software Private Limited","url":"https:\/\/webkul.com\/blog\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/webkul.com\/blog\/#\/schema\/logo\/image\/","url":"https:\/\/cdnblog.webkul.com\/blog\/wp-content\/uploads\/2021\/08\/webkul-logo-accent-sq.png","contentUrl":"https:\/\/cdnblog.webkul.com\/blog\/wp-content\/uploads\/2021\/08\/webkul-logo-accent-sq.png","width":380,"height":380,"caption":"WebKul Software Private Limited"},"image":{"@id":"https:\/\/webkul.com\/blog\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/webkul\/","https:\/\/x.com\/webkul","https:\/\/www.instagram.com\/webkul\/","https:\/\/www.linkedin.com\/company\/webkul","https:\/\/www.youtube.com\/user\/webkul\/"]},{"@type":"Person","@id":"https:\/\/webkul.com\/blog\/#\/schema\/person\/a824ac6c5c14d32569263100b85f47c6","name":"Hemant Jain","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/webkul.com\/blog\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/184358b1c25c580a3e32952605156294dedfff2b8b75bdb2a719c8e6a5d318b6?s=96&d=https%3A%2F%2Fcdnblog.webkul.com%2Fblog%2Fwp-content%2Fuploads%2F2019%2F10%2Fmike.png&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/184358b1c25c580a3e32952605156294dedfff2b8b75bdb2a719c8e6a5d318b6?s=96&d=https%3A%2F%2Fcdnblog.webkul.com%2Fblog%2Fwp-content%2Fuploads%2F2019%2F10%2Fmike.png&r=g","caption":"Hemant Jain"},"url":"https:\/\/webkul.com\/blog\/author\/hemant-jain591\/"}]}},"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/webkul.com\/blog\/wp-json\/wp\/v2\/posts\/183228","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/webkul.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/webkul.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/webkul.com\/blog\/wp-json\/wp\/v2\/users\/167"}],"replies":[{"embeddable":true,"href":"https:\/\/webkul.com\/blog\/wp-json\/wp\/v2\/comments?post=183228"}],"version-history":[{"count":3,"href":"https:\/\/webkul.com\/blog\/wp-json\/wp\/v2\/posts\/183228\/revisions"}],"predecessor-version":[{"id":183243,"href":"https:\/\/webkul.com\/blog\/wp-json\/wp\/v2\/posts\/183228\/revisions\/183243"}],"wp:attachment":[{"href":"https:\/\/webkul.com\/blog\/wp-json\/wp\/v2\/media?parent=183228"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/webkul.com\/blog\/wp-json\/wp\/v2\/categories?post=183228"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/webkul.com\/blog\/wp-json\/wp\/v2\/tags?post=183228"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}