title: Azure Functions - shared objects between different invocations
body: <p>I am using Azure Functions with Node.Js. </p>
  
  <p>I created a custom Node.js logger module which uses azure function's context object and write to the function's log which is displayed in the Azure portal UI. Since we're using it in multiple places we defined it as singleton object and we injected the function's context object to it.</p>
  
  <p>Then, we found out that all the other functions executions were using the same logger object... and were not logging the data to their own context log. This is very strange for us since we expected that each function invocation is isolated and there are no shared modules between different invocations. </p>
  
  <p>This is the azure function code:</p>
  
  <pre><code>var Logger = require( '../custom_modules/logger/customLogger.js');
  
  module.exports = function (azureContext) {
      function infoFn(msg){
          azureContext ? azureContext.log(msg) : console.log(msg);
      }
      function errorFn(msg){
          azureContext ? azureContext.log(msg) : console.error(msg);
      }
  
      var logger = Logger(infoFn, errorFn);
  
      logger.info('function was triggered');
      azureContext.done();
  
    }
  </code></pre>
  
  <p>And this is the logger module code:</p>
  
  <pre><code>var _customLogger;
  
  var CustomLogger = function (info, error) {
      this.error = error;
      this.info = info;
  };
  
  function logger(info, error) {
      if (!_customLogger) {
          _customLogger = new CustomLogger(info, error);
      } else {
          if(info){
              _customLogger.info = info;
          }
          if(error){
              _customLogger.error = error;
          }
      }
      return _customLogger;
  }
  
  module.exports = logger;
  </code></pre>
  
  <p>Is it suppose to be like this? We expected that each function invocation will be isolated. </p>