 
   # This method makes a request to Recurly. If the reponse is chunked,
   # it waits for the last chunk and returns output as JS object.
-  request: ({ method, operation, httpOptions, data }, callback) ->
+  request: ({ method, operation, httpOptions, data, paymentMethodId }, callback) ->
 
-    https = require 'https'
+    log4js  = require 'log4js'
+    logger  = log4js.getLogger('recurly')
+
+    log4js.configure {
+      appenders: [
+        { type: 'console' }
+        { type: 'file', filename: 'logs/recurly.log', category: 'recurly' }
+        { type: "log4js-node-syslog", tag : "recurly", facility: "local0", hostname: "localhost", port: 514 }
+      ]
+    }
+
+    log = (type, {paymentMethodId, data, operation}) ->
+      loggableOperations = ["subscriptions", "transactions"] #it can be retrieved from config
+      loggedOps = loggableOperations.join("|")
+
+      if new RegExp("^(#{loggedOps})").test operation
+        paymentMethodInfo = if paymentMethodId then "PaymentMethodId: #{paymentMethodId}\n" else ""
+        logger[type] "\n#{paymentMethodInfo}Operation: #{operation} \nData: #{data}"  
+
+    https  = require 'https'
     xml2js = require 'xml2js'
 
     options =

      options.headers['Content-Type'] = 'application/xml; charset=UTF-8'

    console.time 'Recurly API request'

    req = https.request options, (res)->
      res.setEncoding 'utf8'

              else
                errors.push new models.Error err1, err1
            callback errors
            log "error", {paymentMethodId, operation, data: result.errors}
          else
            # Find next page's URL if necessary
            nextUrl = parseNextUrl res  if res.headers.link
            callback null, result, nextUrl
            log "info", {paymentMethodId, operation, data: JSON.stringify(result)}

    req.on 'error', (e)->
      callback [new models.Error 'connection', 'Unable to connect to payment backend.']

    req.end(data, 'utf8')
