Permalink
Browse files

Enable / Disable Logging

    invoke loggging by from sailthruClient object
        client = sailthru.createClient(apiKey, apiSecret);
        client.disableLogging()
    By default logging is enabled
    Logging format: <Datetime> <sailthru-client> <version> <log message>
  • Loading branch information...
1 parent f108cc9 commit 996db499a49a3b4b516e1371344518e7ef0237be @infynyxx infynyxx committed Dec 12, 2011
Showing with 60 additions and 12 deletions.
  1. +24 −6 lib/sailthru.js
  2. +6 −1 lib/sailthru_util.js
  3. +26 −5 src/sailthru.coffee
  4. +4 −0 src/sailthru_util.coffee
View

Some generated files are not rendered by default. Learn more.

Oops, something went wrong.
View
@@ -1,6 +1,8 @@
(function() {
- var SailthruUtil, crypto;
+ var SailthruUtil, VERSION, crypto, log;
crypto = require('crypto');
+ log = require('util').log;
+ VERSION = require('./sailthru').VERSION;
exports.SailthruUtil = SailthruUtil = (function() {
function SailthruUtil() {}
SailthruUtil.getSignatureHash = function(params, secret) {
@@ -35,4 +37,7 @@
};
return SailthruUtil;
})();
+ exports.log = function(string) {
+ return log('sailthru-client ' + VERSION + ' - ' + string);
+ };
}).call(this);
View
@@ -1,10 +1,19 @@
http = require 'http'
https = require 'https'
url = require 'url'
-{log} = require 'util'
querystring = require 'querystring'
-{SailthruUtil} = require './sailthru_util'
+###
+ API client version
+###
+exports.VERSION = '1.0.2'
+
+LOGGING = true
+
+{SailthruUtil, log} = require './sailthru_util'
+
+log2 = (string) ->
+ log string if LOGGING is true
class SailthruRequest
valid_methods = ['GET', 'POST', 'DELETE']
@@ -18,7 +27,7 @@ class SailthruRequest
method: method
query: data
headers:
- 'User-Agent': 'Sailthru API Node/Javascript Client'
+ 'User-Agent': 'Sailthru API Node/JavaScript Client'
Host: parse_uri.host
http_protocol = if options.port is 443 then https else http
@@ -37,12 +46,12 @@ class SailthruRequest
# handle error
rerurn false
- log method + ' Request'
+ log2 method + ' Request'
req = http_protocol.request options, (res) ->
body = ''
res.setEncoding 'utf8'
statusCode = res.statusCode
- log 'Status Code: ' + res.statusCode
+ log2 'Status Code: ' + res.statusCode
res.on 'data', (chunk) ->
body += chunk
res.on 'end', ->
@@ -64,6 +73,11 @@ class SailthruRequest
return @_http_request uri, data, request_method, callback
class SailthruClient
+ ###
+ By default enable logging
+ ###
+ @logging = true
+
constructor: (@api_key, @api_secret, @api_url = false) ->
@api_url = 'https://api.sailthru.com' if @api_url is false
@request = new SailthruRequest
@@ -82,6 +96,12 @@ class SailthruClient
json_payload = @_json_payload data
return @request._api_request _url.href + action, json_payload, method, callback
+ enableLogging: ->
+ LOGGING = true
+
+ disableLogging: ->
+ LOGGING = false
+
# Native API methods: GET< DELETE and POST
apiGet: (action, data, callback) ->
@_apiRequest action, data, 'GET', callback
@@ -296,3 +316,4 @@ exports.createSailthruClient = (args...) ->
exports.createClient = (args...) ->
new SailthruClient args...
+
View
@@ -1,4 +1,6 @@
crypto = require 'crypto'
+{log} = require 'util'
+{VERSION} = require './sailthru'
exports.SailthruUtil = class SailthruUtil
@getSignatureHash: (params, secret) ->
@@ -27,3 +29,5 @@ exports.SailthruUtil = class SailthruUtil
values = values.concat(SailthruUtil.extractParamValues(v))
return values
+exports.log = (string) ->
+ return log 'sailthru-client ' + VERSION + ' - ' + string

0 comments on commit 996db49

Please sign in to comment.