{"id":4490,"date":"2016-08-19T08:15:20","date_gmt":"2016-08-19T13:15:20","guid":{"rendered":"http:\/\/www.carnaghan.com\/?p=4490"},"modified":"2019-09-22T22:18:01","modified_gmt":"2019-09-23T02:18:01","slug":"logging-drupal-8","status":"publish","type":"post","link":"https:\/\/www.carnaghan.com\/logging-drupal-8\/","title":{"rendered":"Logging in Drupal 8"},"content":{"rendered":"\n

Logging is very important in any production site because we don’t have the same ability to debug code that we have in our development environments. Drupal<\/span> 7 had a very robust logging system called watchdog(), which has been replaced by logger in Drupal<\/span> 8. This post summarizes a basic use case for logging in<\/span> Drupal<\/span> 8 continuing the Drupal<\/span> 8 Activity Cards series on Day 13<\/a>.<\/p>\n\n\n\n

Amber Matz breaks down logging in<\/span> Drupal<\/span> 8 concisely in How to Log Messages in Drupal 8<\/a>. In her article, she breaks down the code needed to implement logger and provides solid explanations. The examples provided involve procedural coding in the .module<\/span> file<\/span> as does the example I provide in this article. If you are writing controllers<\/span> or forms that extend ControllerBase or FormBase then it would be better to use the service container to inject logger into your code.<\/p>\n\n\n\n

Basic Example of Logging<\/span> Updates<\/h2>\n\n\n\n

For a basic example, I created a custom module<\/span> that simply logged a message any time a node was updated<\/span>. In order to do this, I first referred to the available hooks in Drupal 8<\/a> and soon realized the best way to accomplish this would be through hook_entity_update<\/a>. To get started I issued the command<\/span>:<\/p>\n\n\n\n

drupal generate:module<\/pre>\n\n\n\n

I provided the information requested and scaffolded out boilerplate code<\/span> for a new custom module<\/span> called Day 13. For more information on generating boilerplate code<\/span>, see Creating a Custom Content Entity Type<\/a>.<\/p>\n\n\n\n

Generated or updated files<\/span><\/span>\n  Site path: \/var\/www\/drupal\/<\/span>drupal8<\/span>\n  1 - modules\/custom\/day13\/day13.info.yml<\/span>\n  2 - modules\/custom\/day13\/day13.module<\/span><\/pre>\n\n\n\n

After the boilerplate code<\/span> was generated I edited the .module file<\/span> and implemented hook_entity_update.<\/p>\n\n\n\n

function day13_entity_update(Drupal\\Core\\Entity\\EntityInterface $entity) {\n    \\Drupal::logger('Node Updates')->notice('Node with title %title of type @type hs been updated',\n    array(\n        '@type' => $entity->bundle(),\n        '%title' => $entity->label(),\n    ));\n}<\/code><\/pre>\n\n\n\n

To test, I enabled the new module and made a content update<\/span>. After reviewing logs<\/span> I found the new entry generated by the module.<\/p>\n\n\n\n

\"log\"<\/a><\/figure>\n\n\n\n

This is just a basic example using<\/span> procedural code directly in the .module<\/span> file<\/span>. A better use of logger would be via the services container<\/span> as mentioned earlier and instead of using<\/span> a hook, we could use a event<\/span>. Some of this I will cover at a later time, but for now this overview provides the basics of getting up and running with the new Drupal<\/span> 8 logging system.<\/p>\n","protected":false},"excerpt":{"rendered":"

Logging is very important in any production site because we don’t have the same ability to debug code that we have in our development environments. Drupal 7 had a very robust logging system called watchdog(), which has been replaced by logger in Drupal 8. This post summarizes a basic use case for logging in Drupal […]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[24,7129],"tags":[405],"post_series":[406],"yoast_head":"\nLogging in Drupal 8 - Ian Carnaghan<\/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.carnaghan.com\/logging-drupal-8\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Logging in Drupal 8 - Ian Carnaghan\" \/>\n<meta property=\"og:description\" content=\"Logging is very important in any production site because we don’t have the same ability to debug code that we have in our development environments. Drupal 7 had a very robust logging system called watchdog(), which has been replaced by logger in Drupal 8. This post summarizes a basic use case for logging in Drupal […]\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.carnaghan.com\/logging-drupal-8\/\" \/>\n<meta property=\"og:site_name\" content=\"Ian Carnaghan\" \/>\n<meta property=\"article:published_time\" content=\"2016-08-19T13:15:20+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2019-09-23T02:18:01+00:00\" \/>\n<meta property=\"og:image\" content=\"http:\/\/www.carnaghan.com\/wp-content\/uploads\/2016\/08\/log-1024x721.jpg\" \/>\n<meta name=\"author\" content=\"Ian Carnaghan\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@icarnaghan\" \/>\n<meta name=\"twitter:site\" content=\"@icarnaghan\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Ian Carnaghan\" \/>\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:\/\/www.carnaghan.com\/logging-drupal-8\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/www.carnaghan.com\/logging-drupal-8\/\"},\"author\":{\"name\":\"Ian Carnaghan\",\"@id\":\"https:\/\/www.carnaghan.com\/#\/schema\/person\/c689c24d516c51968a88b628860740a5\"},\"headline\":\"Logging in Drupal 8\",\"datePublished\":\"2016-08-19T13:15:20+00:00\",\"dateModified\":\"2019-09-23T02:18:01+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/www.carnaghan.com\/logging-drupal-8\/\"},\"wordCount\":345,\"commentCount\":0,\"publisher\":{\"@id\":\"https:\/\/www.carnaghan.com\/#\/schema\/person\/c689c24d516c51968a88b628860740a5\"},\"image\":{\"@id\":\"https:\/\/www.carnaghan.com\/logging-drupal-8\/#primaryimage\"},\"thumbnailUrl\":\"http:\/\/www.carnaghan.com\/wp-content\/uploads\/2016\/08\/log-1024x721.jpg\",\"keywords\":[\"Drupal\"],\"articleSection\":[\"Coding\",\"Content Management Systems\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\/\/www.carnaghan.com\/logging-drupal-8\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.carnaghan.com\/logging-drupal-8\/\",\"url\":\"https:\/\/www.carnaghan.com\/logging-drupal-8\/\",\"name\":\"Logging in Drupal 8 - Ian Carnaghan\",\"isPartOf\":{\"@id\":\"https:\/\/www.carnaghan.com\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/www.carnaghan.com\/logging-drupal-8\/#primaryimage\"},\"image\":{\"@id\":\"https:\/\/www.carnaghan.com\/logging-drupal-8\/#primaryimage\"},\"thumbnailUrl\":\"http:\/\/www.carnaghan.com\/wp-content\/uploads\/2016\/08\/log-1024x721.jpg\",\"datePublished\":\"2016-08-19T13:15:20+00:00\",\"dateModified\":\"2019-09-23T02:18:01+00:00\",\"breadcrumb\":{\"@id\":\"https:\/\/www.carnaghan.com\/logging-drupal-8\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.carnaghan.com\/logging-drupal-8\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/www.carnaghan.com\/logging-drupal-8\/#primaryimage\",\"url\":\"http:\/\/www.carnaghan.com\/wp-content\/uploads\/2016\/08\/log-1024x721.jpg\",\"contentUrl\":\"http:\/\/www.carnaghan.com\/wp-content\/uploads\/2016\/08\/log-1024x721.jpg\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.carnaghan.com\/logging-drupal-8\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/www.carnaghan.com\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Logging in Drupal 8\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/www.carnaghan.com\/#website\",\"url\":\"https:\/\/www.carnaghan.com\/\",\"name\":\"Ian Carnaghan\",\"description\":\"Software Developer, Blogger, Educator\",\"publisher\":{\"@id\":\"https:\/\/www.carnaghan.com\/#\/schema\/person\/c689c24d516c51968a88b628860740a5\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/www.carnaghan.com\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"en-US\"},{\"@type\":[\"Person\",\"Organization\"],\"@id\":\"https:\/\/www.carnaghan.com\/#\/schema\/person\/c689c24d516c51968a88b628860740a5\",\"name\":\"Ian Carnaghan\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/www.carnaghan.com\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/f2aa5baca80c2be728de43a975185d91?s=96&d=retro&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/f2aa5baca80c2be728de43a975185d91?s=96&d=retro&r=g\",\"caption\":\"Ian Carnaghan\"},\"logo\":{\"@id\":\"https:\/\/www.carnaghan.com\/#\/schema\/person\/image\/\"},\"description\":\"I am a software developer and online educator who likes to keep up with all the latest in technology. I also manage cloud infrastructure, continuous monitoring, DevOps processes, security, and continuous integration and deployment.\",\"sameAs\":[\"http:\/\/www.carnaghan.com\",\"https:\/\/x.com\/icarnaghan\"],\"url\":\"https:\/\/www.carnaghan.com\/author\/icarnaghan\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Logging in Drupal 8 - Ian Carnaghan","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.carnaghan.com\/logging-drupal-8\/","og_locale":"en_US","og_type":"article","og_title":"Logging in Drupal 8 - Ian Carnaghan","og_description":"Logging is very important in any production site because we don’t have the same ability to debug code that we have in our development environments. Drupal 7 had a very robust logging system called watchdog(), which has been replaced by logger in Drupal 8. This post summarizes a basic use case for logging in Drupal […]","og_url":"https:\/\/www.carnaghan.com\/logging-drupal-8\/","og_site_name":"Ian Carnaghan","article_published_time":"2016-08-19T13:15:20+00:00","article_modified_time":"2019-09-23T02:18:01+00:00","og_image":[{"url":"http:\/\/www.carnaghan.com\/wp-content\/uploads\/2016\/08\/log-1024x721.jpg"}],"author":"Ian Carnaghan","twitter_card":"summary_large_image","twitter_creator":"@icarnaghan","twitter_site":"@icarnaghan","twitter_misc":{"Written by":"Ian Carnaghan","Est. reading time":"2 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/www.carnaghan.com\/logging-drupal-8\/#article","isPartOf":{"@id":"https:\/\/www.carnaghan.com\/logging-drupal-8\/"},"author":{"name":"Ian Carnaghan","@id":"https:\/\/www.carnaghan.com\/#\/schema\/person\/c689c24d516c51968a88b628860740a5"},"headline":"Logging in Drupal 8","datePublished":"2016-08-19T13:15:20+00:00","dateModified":"2019-09-23T02:18:01+00:00","mainEntityOfPage":{"@id":"https:\/\/www.carnaghan.com\/logging-drupal-8\/"},"wordCount":345,"commentCount":0,"publisher":{"@id":"https:\/\/www.carnaghan.com\/#\/schema\/person\/c689c24d516c51968a88b628860740a5"},"image":{"@id":"https:\/\/www.carnaghan.com\/logging-drupal-8\/#primaryimage"},"thumbnailUrl":"http:\/\/www.carnaghan.com\/wp-content\/uploads\/2016\/08\/log-1024x721.jpg","keywords":["Drupal"],"articleSection":["Coding","Content Management Systems"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/www.carnaghan.com\/logging-drupal-8\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/www.carnaghan.com\/logging-drupal-8\/","url":"https:\/\/www.carnaghan.com\/logging-drupal-8\/","name":"Logging in Drupal 8 - Ian Carnaghan","isPartOf":{"@id":"https:\/\/www.carnaghan.com\/#website"},"primaryImageOfPage":{"@id":"https:\/\/www.carnaghan.com\/logging-drupal-8\/#primaryimage"},"image":{"@id":"https:\/\/www.carnaghan.com\/logging-drupal-8\/#primaryimage"},"thumbnailUrl":"http:\/\/www.carnaghan.com\/wp-content\/uploads\/2016\/08\/log-1024x721.jpg","datePublished":"2016-08-19T13:15:20+00:00","dateModified":"2019-09-23T02:18:01+00:00","breadcrumb":{"@id":"https:\/\/www.carnaghan.com\/logging-drupal-8\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.carnaghan.com\/logging-drupal-8\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.carnaghan.com\/logging-drupal-8\/#primaryimage","url":"http:\/\/www.carnaghan.com\/wp-content\/uploads\/2016\/08\/log-1024x721.jpg","contentUrl":"http:\/\/www.carnaghan.com\/wp-content\/uploads\/2016\/08\/log-1024x721.jpg"},{"@type":"BreadcrumbList","@id":"https:\/\/www.carnaghan.com\/logging-drupal-8\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.carnaghan.com\/"},{"@type":"ListItem","position":2,"name":"Logging in Drupal 8"}]},{"@type":"WebSite","@id":"https:\/\/www.carnaghan.com\/#website","url":"https:\/\/www.carnaghan.com\/","name":"Ian Carnaghan","description":"Software Developer, Blogger, Educator","publisher":{"@id":"https:\/\/www.carnaghan.com\/#\/schema\/person\/c689c24d516c51968a88b628860740a5"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.carnaghan.com\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"en-US"},{"@type":["Person","Organization"],"@id":"https:\/\/www.carnaghan.com\/#\/schema\/person\/c689c24d516c51968a88b628860740a5","name":"Ian Carnaghan","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.carnaghan.com\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/f2aa5baca80c2be728de43a975185d91?s=96&d=retro&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/f2aa5baca80c2be728de43a975185d91?s=96&d=retro&r=g","caption":"Ian Carnaghan"},"logo":{"@id":"https:\/\/www.carnaghan.com\/#\/schema\/person\/image\/"},"description":"I am a software developer and online educator who likes to keep up with all the latest in technology. I also manage cloud infrastructure, continuous monitoring, DevOps processes, security, and continuous integration and deployment.","sameAs":["http:\/\/www.carnaghan.com","https:\/\/x.com\/icarnaghan"],"url":"https:\/\/www.carnaghan.com\/author\/icarnaghan\/"}]}},"views":599,"_links":{"self":[{"href":"https:\/\/www.carnaghan.com\/wp-json\/wp\/v2\/posts\/4490"}],"collection":[{"href":"https:\/\/www.carnaghan.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.carnaghan.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.carnaghan.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.carnaghan.com\/wp-json\/wp\/v2\/comments?post=4490"}],"version-history":[{"count":0,"href":"https:\/\/www.carnaghan.com\/wp-json\/wp\/v2\/posts\/4490\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.carnaghan.com\/wp-json\/wp\/v2\/media?parent=4490"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.carnaghan.com\/wp-json\/wp\/v2\/categories?post=4490"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.carnaghan.com\/wp-json\/wp\/v2\/tags?post=4490"},{"taxonomy":"post_series","embeddable":true,"href":"https:\/\/www.carnaghan.com\/wp-json\/wp\/v2\/post_series?post=4490"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}