{"id":7151,"date":"2014-08-07T15:01:27","date_gmt":"2014-08-07T12:01:27","guid":{"rendered":"http:\/\/railsware.com\/blog\/?p=7151"},"modified":"2021-08-12T17:35:40","modified_gmt":"2021-08-12T14:35:40","slug":"rails-logging-into-several-backends","status":"publish","type":"post","link":"https:\/\/railsware.com\/blog\/rails-logging-into-several-backends\/","title":{"rendered":"Rails logging into several backends"},"content":{"rendered":"\n<p>Sometimes, it&#8217;s required to setup Rails logger for storing logs into several backends. In our case we decided to use <a href=\"http:\/\/www.loggly.com\/\">Loggly<\/a>, but also wanted to leave old file-based logging.<\/p>\n\n\n\n<p>Despite the actual solution is very simple, it&#8217;s hard to find it. The <a href=\"http:\/\/stackoverflow.com\/questions\/6407141\/how-can-i-have-ruby-logger-log-output-to-stdout-as-well-as-file\">best match on StackOverflow<\/a> suggests to create some custom proxy class.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Solution<\/h2>\n\n\n\n<p>Now with Rails 4 it&#8217;s a real no-brainer. Let&#8217;s solve a task from SO &#8211; add alternative logging into file:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted lang:ruby\">file_logger = Logger.new(Rails.root.join(\"log\/alternative-output.log\"))\nRails.logger.extend(ActiveSupport::Logger.broadcast(file_logger))\n<\/pre>\n\n\n\n<p>Setting Loggly as additional backend is not much harder:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted lang:ruby\"># using gem 'logglier'\nloggly = Logglier.new('https:\/\/logs.loggly.com\/inputs\/\/tag\/ruby', threaded: true)\n\nRails.logger.extend(ActiveSupport::Logger.broadcast(loggly))\n<\/pre>\n\n\n\n<p>All you need is to provide object which implements <span class=\"lang:ruby crayon-inline\">Logger<\/span> interface for <span class=\"lang:ruby crayon-inline\">ActiveSupport::Logger.broadcast<\/span>. In this way, you can add as much logging backends, as you wish.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Rails 3<\/h2>\n\n\n\n<p>If you are on Rails 3, just copy original <a href=\"https:\/\/github.com\/rails\/rails\/blob\/6329d9fa8b2f86a178151be264cccdb805bfaaac\/activesupport\/lib\/active_support\/logger.rb\">active_support\/logger.rb<\/a> into <span class=\"crayon-inline\">config\/initializers\/active_support_logger.rb<\/span>. Also, remove two lines, which don&#8217;t work in Rails 3 environment: line 2 with <span class=\"lang:ruby crayon-inline\">require &#8216;active_support\/logger_silence&#8217;<\/span>, and line 7 with <span class=\"lang:ruby crayon-inline\">include LoggerSilence<\/span>.<\/p>\n\n\n\n<p>Now it can be used as in previous recipes.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Sometimes, it&#8217;s required to setup Rails logger for storing logs into several backends. In our case we decided to use Loggly, but also wanted to leave old file-based logging. Despite the actual solution is very simple, it&#8217;s hard to find it. The best match on StackOverflow suggests to create some custom proxy class. Solution Now&#8230;<\/p>\n","protected":false},"author":25,"featured_media":9463,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[3],"tags":[],"coauthors":["Sergii Boiko"],"class_list":["post-7151","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-development"],"acf":[],"aioseo_notices":[],"categories_data":[{"name":"Engineering","link":"https:\/\/railsware.com\/blog?category=development"}],"post_thumbnails":"https:\/\/railsware.com\/blog\/wp-content\/themes\/railsware\/vendors\/images\/article-thumbnail-default.jpg","amp_enabled":true,"_links":{"self":[{"href":"https:\/\/railsware.com\/blog\/wp-json\/wp\/v2\/posts\/7151","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/railsware.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/railsware.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/railsware.com\/blog\/wp-json\/wp\/v2\/users\/25"}],"replies":[{"embeddable":true,"href":"https:\/\/railsware.com\/blog\/wp-json\/wp\/v2\/comments?post=7151"}],"version-history":[{"count":32,"href":"https:\/\/railsware.com\/blog\/wp-json\/wp\/v2\/posts\/7151\/revisions"}],"predecessor-version":[{"id":14014,"href":"https:\/\/railsware.com\/blog\/wp-json\/wp\/v2\/posts\/7151\/revisions\/14014"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/railsware.com\/blog\/wp-json\/wp\/v2\/media\/9463"}],"wp:attachment":[{"href":"https:\/\/railsware.com\/blog\/wp-json\/wp\/v2\/media?parent=7151"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/railsware.com\/blog\/wp-json\/wp\/v2\/categories?post=7151"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/railsware.com\/blog\/wp-json\/wp\/v2\/tags?post=7151"},{"taxonomy":"author","embeddable":true,"href":"https:\/\/railsware.com\/blog\/wp-json\/wp\/v2\/coauthors?post=7151"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}