title: JavaScript OOP: Implementation of Logging
body: <p>I have written the following code to implement logging in a separate js file logger.js by using OOP. </p>
  
  <pre><code>var console;
  
  function Logger() {
      init();
  }
  
  var init = function() {
      if(!window.console){ 
          console = {
              log: function(message){},
              info: function(message){},
              warn: function(message){},
              error: function(message){}
          }; 
      } else {
          console = window.console;
      }
  };
  
  Logger.prototype.log = function(message) {
      console.log(message);    
  }
  
  Logger.prototype.logInfo = function(message) {
      console.info(message);
  }
  
  Logger.prototype.logWarn = function(message) {
      console.warn(message);
  }
  
  Logger.prototype.logError = function(message) {
      console.error(message);
  }
  </code></pre>
  
  <p>I am using it from another js file, site.js as:</p>
  
  <pre><code>var logger = new Logger(); //global variable
  
  var getComponentById = function(id) {
      var component = null;
  
      if(id) {
          try {
              component = AdfPage.PAGE.findComponentByAbsoluteId(id);
          }catch(e){
              logger.logError(e);
          }
      }
  
      return component;
  }
  </code></pre>
  
  <p>I was wondering </p>
  
  <ul>
  <li>If I have implemented the <code>Logger</code> class in proper way, by maintaining OOP of JavaScript.</li>
  <li>Will it handle the scenario where the browser don't have any console?</li>
  <li>How can I make <code>init()</code> method inaccessible from other js file or method? I mean how can I make it <code>private</code>?</li>
  </ul>
  
  <p>Any pointer would be very helpful to me.</p>
  
  <h2>Update</h2>
  
  <p>From another <a href="https://stackoverflow.com/questions/55611/javascript-private-methods">SO thread</a> I found information about private method and I changed my approach: </p>
  
  <pre><code>function Logger() {
      init();
  }
  
  Logger.prototype = (function() {
      var console;
  
      var init = function() {
          if(!window.console){ 
              this.console = {
                  log: function(message){},
                  info: function(message){},
                  warn: function(message){},
                  error: function(message){}
              }; 
          } else {
              this.console = window.console;
          }
      };
  
      return {
          constructor: Logger,
  
          log: function(message) {
              this.console.log(message);    
          },
  
          logInfo: function(message) {
              this.console.info(message);
          },
  
          logWarn: function(message) {
              this.console.warn(message);
          },
  
          logError: function(message) {
              this.console.error(message);
          }
      };
  })();
  </code></pre>
  
  <p>But in this case I am getting error that <code>init</code> is not defined.</p>