title: How to log from a NPM package, without forcing a logging library
body: <p>I'm writing a company npm package (in typescript), and porting existing code into it. In the existing code, there are some <code>console.log</code>, <code>console.warn</code> and <code>console.error</code> statements. Like this:</p>
  
  <pre><code>try {
      const foo = getFoo("bar");
      return callback(undefined, foo);
  } catch (e) {
      console.error(e);
      return cb(new httpErrors.BadRequest("Some message"));
  }
  </code></pre>
  
  <p>So what is happening is:</p>
  
  <ul>
  <li>log the error</li>
  <li>call a callback with another error, not passing in the original error</li>
  </ul>
  
  <p>This is the behavior we want, as we don't want to return the original error message, but we do want it in our logs.</p>
  
  <p>Now that I'm moving to a package, I want to apply some best practices and remove the <code>console</code> calls. This is a tslint rule and I agree that it isn't up to the package author to fill the logs of the package user.</p>
  
  <p>So what would be the best approach here? </p>
  
  <p>I've read about the <code>debug</code> package, but that's for development purposes only. Plus, that would mean we wouldn't have our log messages unless we set <code>DEBUG=...</code></p>
  
  <p>I'm wondering if using a singleton <code>EventEmitter</code> in my package is an option. But then again, singleton is regarded as an antipattern.</p>
  
  <p>And I don't want to add a logging library as a dependency, because as the package author, it's not up to me to say which logging library to use.</p>
  
  <p>A last option I've thought about is to allow passing in a logging function that I can then call, but it feels a bit crummy to me.</p>
  
  <p>So what is the preferred technique to allow messages to be logged from a (TypeScript/npm) library? </p>
  
  <p>(If there is one at all; I'm fairly new to the Node/TypeScript ecosystem).</p>