{"id":6110,"date":"2012-07-30T16:50:32","date_gmt":"2012-07-30T21:50:32","guid":{"rendered":"http:\/\/bobbeaty.com\/wp\/?p=6110"},"modified":"2012-07-31T09:00:23","modified_gmt":"2012-07-31T14:00:23","slug":"lovely-little-ruby-logger","status":"publish","type":"post","link":"https:\/\/bobbeaty.com\/wp\/archives\/6110","title":{"rendered":"Lovely Little Ruby Logger"},"content":{"rendered":"<p><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/bobbeaty.com\/wp\/wp-content\/uploads\/2012\/07\/Ruby.jpg\" alt=\"Ruby\" title=\"Ruby.jpg\" border=\"0\" width=\"90\" height=\"90\" style=\"float:right;\" \/><\/p>\n<p>This afternoon I wanted to create a log4j-like logger for the ruby app we're working on. I was <em>hoping<\/em> to find a gem that did it - singleton, thread-safe, the works. What I found was that the default logger: \"logger\" is really pretty close, and it's easy to make it what we need.<\/p>\n<p>First, it's thread-safe. Doesn't say that in the docs, but the code has <tt>synchronize<\/tt> blocks on a mutex, and that <em>appears<\/em> to be working, so while I can't <strong><em>guarantee<\/em><\/strong> that it's implemented <em>correctly<\/em>, it <em>appears<\/em> that it is, and that's good enough for now.<\/p>\n<p>Second, it's not a singleton, but that's easy to fix:<\/p>\n<pre class=\"ruby\" style=\"font-family:monospace;\">  <span style=\"color:#CC0066; font-weight:bold;\">require<\/span> <span style=\"color:#996600;\">&quot;singleton&quot;<\/span>\n  <span style=\"color:#CC0066; font-weight:bold;\">require<\/span> <span style=\"color:#996600;\">&quot;logger&quot;<\/span>\n  <span style=\"color:#CC0066; font-weight:bold;\">require<\/span> <span style=\"color:#996600;\">&quot;app_config&quot;<\/span>\n&nbsp;\n  <span style=\"color:#9966CC; font-weight:bold;\">class<\/span> AppLog\n    <span style=\"color:#9966CC; font-weight:bold;\">include<\/span> SIngleton\n&nbsp;\n    <span style=\"color:#9966CC; font-weight:bold;\">def<\/span> initialize<span style=\"color:#006600; font-weight:bold;\">&#40;<\/span><span style=\"color:#006600; font-weight:bold;\">&#41;<\/span>\n      <span style=\"color:#008000; font-style:italic;\"># get the location to log to and the level<\/span>\n      where = AppConfig.<span style=\"color:#9900CC;\">application<\/span>.<span style=\"color:#9900CC;\">log_to<\/span> <span style=\"color:#006600; font-weight:bold;\">||<\/span> <span style=\"color:#996600;\">&quot;stout&quot;<\/span>\n      level = AppConfig.<span style=\"color:#9900CC;\">application<\/span>.<span style=\"color:#9900CC;\">log_level<\/span> <span style=\"color:#006600; font-weight:bold;\">||<\/span> <span style=\"color:#996600;\">&quot;WARN&quot;<\/span>\n      <span style=\"color:#008000; font-style:italic;\"># now make the logger for our singleton<\/span>\n      <span style=\"color:#9966CC; font-weight:bold;\">case<\/span>\n      <span style=\"color:#9966CC; font-weight:bold;\">when<\/span> where == <span style=\"color:#996600;\">&quot;stdout&quot;<\/span>\n        <span style=\"color:#0000FF; font-weight:bold;\">self<\/span>.<span style=\"color:#9900CC;\">log<\/span> = <span style=\"color:#CC00FF; font-weight:bold;\">Logger<\/span>.<span style=\"color:#9900CC;\">new<\/span><span style=\"color:#006600; font-weight:bold;\">&#40;<\/span>$stdout<span style=\"color:#006600; font-weight:bold;\">&#41;<\/span>\n      <span style=\"color:#9966CC; font-weight:bold;\">when<\/span> where == <span style=\"color:#996600;\">&quot;stderr&quot;<\/span>\n        <span style=\"color:#0000FF; font-weight:bold;\">self<\/span>.<span style=\"color:#9900CC;\">log<\/span> = <span style=\"color:#CC00FF; font-weight:bold;\">Logger<\/span>.<span style=\"color:#9900CC;\">new<\/span><span style=\"color:#006600; font-weight:bold;\">&#40;<\/span>$stderr<span style=\"color:#006600; font-weight:bold;\">&#41;<\/span>\n      <span style=\"color:#9966CC; font-weight:bold;\">else<\/span>\n        <span style=\"color:#0000FF; font-weight:bold;\">self<\/span>.<span style=\"color:#9900CC;\">log<\/span> = <span style=\"color:#CC00FF; font-weight:bold;\">Logger<\/span>.<span style=\"color:#9900CC;\">new<\/span><span style=\"color:#006600; font-weight:bold;\">&#40;<\/span>where<span style=\"color:#006600; font-weight:bold;\">&#41;<\/span>\n      <span style=\"color:#9966CC; font-weight:bold;\">end<\/span>\n      <span style=\"color:#008000; font-style:italic;\"># now set the log level<\/span>\n      <span style=\"color:#9966CC; font-weight:bold;\">case<\/span>\n        where level == <span style=\"color:#996600;\">&quot;FATAL&quot;<\/span>\n          <span style=\"color:#0000FF; font-weight:bold;\">self<\/span>.<span style=\"color:#9900CC;\">log<\/span>.<span style=\"color:#9900CC;\">level<\/span> = <span style=\"color:#CC00FF; font-weight:bold;\">Logger<\/span>::FATAL\n        where level == <span style=\"color:#996600;\">&quot;ERROR&quot;<\/span>\n          <span style=\"color:#0000FF; font-weight:bold;\">self<\/span>.<span style=\"color:#9900CC;\">log<\/span>.<span style=\"color:#9900CC;\">level<\/span> = <span style=\"color:#CC00FF; font-weight:bold;\">Logger<\/span>::ERROR\n        where level == <span style=\"color:#996600;\">&quot;WARN&quot;<\/span>\n          <span style=\"color:#0000FF; font-weight:bold;\">self<\/span>.<span style=\"color:#9900CC;\">log<\/span>.<span style=\"color:#9900CC;\">level<\/span> = <span style=\"color:#CC00FF; font-weight:bold;\">Logger<\/span>::WARN\n        where level == <span style=\"color:#996600;\">&quot;INFO&quot;<\/span>\n          <span style=\"color:#0000FF; font-weight:bold;\">self<\/span>.<span style=\"color:#9900CC;\">log<\/span>.<span style=\"color:#9900CC;\">level<\/span> = <span style=\"color:#CC00FF; font-weight:bold;\">Logger<\/span>::INFO\n        where level == <span style=\"color:#996600;\">&quot;DEBUG&quot;<\/span>\n          <span style=\"color:#0000FF; font-weight:bold;\">self<\/span>.<span style=\"color:#9900CC;\">log<\/span>.<span style=\"color:#9900CC;\">level<\/span> = <span style=\"color:#CC00FF; font-weight:bold;\">Logger<\/span>::DEBUG\n      <span style=\"color:#9966CC; font-weight:bold;\">end<\/span>\n    <span style=\"color:#9966CC; font-weight:bold;\">end<\/span>\n  <span style=\"color:#9966CC; font-weight:bold;\">end<\/span><\/pre>\n<p>This can then be placed in all our code and we get singleton-based logging for next to nothing. Very nice. At the same time, we can control the logging location and level very easily in the app config file.<\/p>\n<p>I then took this and integrated it into the code I was working on for matching up the demand data with the merchants. It's something that we really need to do to <em>productionize<\/em> our app, and this is an important step.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>This afternoon I wanted to create a log4j-like logger for the ruby app we&#8217;re working on. I was hoping to find a gem that did it &#8211; singleton, thread-safe, the works. What I found was that the default logger: &#8220;logger&#8221; is really pretty close, and it&#8217;s easy to make it what we need. First, it&#8217;s [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3,4],"tags":[],"class_list":["post-6110","post","type-post","status-publish","format-standard","hentry","category-coding","category-open-source-software"],"_links":{"self":[{"href":"https:\/\/bobbeaty.com\/wp\/wp-json\/wp\/v2\/posts\/6110","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/bobbeaty.com\/wp\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/bobbeaty.com\/wp\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/bobbeaty.com\/wp\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/bobbeaty.com\/wp\/wp-json\/wp\/v2\/comments?post=6110"}],"version-history":[{"count":2,"href":"https:\/\/bobbeaty.com\/wp\/wp-json\/wp\/v2\/posts\/6110\/revisions"}],"predecessor-version":[{"id":6112,"href":"https:\/\/bobbeaty.com\/wp\/wp-json\/wp\/v2\/posts\/6110\/revisions\/6112"}],"wp:attachment":[{"href":"https:\/\/bobbeaty.com\/wp\/wp-json\/wp\/v2\/media?parent=6110"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/bobbeaty.com\/wp\/wp-json\/wp\/v2\/categories?post=6110"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/bobbeaty.com\/wp\/wp-json\/wp\/v2\/tags?post=6110"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}