Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

New transport for winston to Amazon SimpleDB

  • Loading branch information...
commit e4b63fead537b05648686b0341d0a5800569013f 0 parents
Andrew Chilton authored November 20, 2011
2  .gitignore
... ...
@@ -0,0 +1,2 @@
  1
+node_modules
  2
+*~
29  LICENSE
... ...
@@ -0,0 +1,29 @@
  1
+-------------------------------------------------------------------------------
  2
+
  3
+This software is published under the MIT license as published here:
  4
+
  5
+* http://opensource.org/licenses/MIT
  6
+
  7
+-------------------------------------------------------------------------------
  8
+
  9
+Copyright 2011 Apps Attic Ltd.  All rights reserved.
  10
+
  11
+Permission is hereby granted, free of charge, to any person obtaining a copy of
  12
+this software and associated documentation files (the "Software"), to deal in
  13
+the Software without restriction, including without limitation the rights to
  14
+use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
  15
+of the Software, and to permit persons to whom the Software is furnished to do
  16
+so, subject to the following conditions:
  17
+
  18
+The above copyright notice and this permission notice shall be included in all
  19
+copies or substantial portions of the Software.
  20
+
  21
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  22
+IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  23
+FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  24
+AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  25
+LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  26
+OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  27
+SOFTWARE.
  28
+
  29
+-------------------------------------------------------------------------------
20  README.md
Source Rendered
... ...
@@ -0,0 +1,20 @@
  1
+# SimpleDB Transport for Winston
  2
+
  3
+The winston-simpledb module allows you to log your winston messages to Amazon's SimpleDB.
  4
+
  5
+    var SimpleDB = require('winston-simpledb').SimpleDB;
  6
+    winston.add(SimpleDB, options);
  7
+
  8
+The SimpleDB transport takes the following options.
  9
+
  10
+    level : the log level of messages
  11
+
  12
+level: Level of messages that this transport should log.
  13
+silent: Boolean flag indicating whether to suppress output.
  14
+db: The name of the database you want to log to. [required]
  15
+collection: The name of the collection you want to store log messages in, defaults to 'log'.
  16
+safe: Boolean indicating if you want eventual consistency on your log messages, if set to true it requires an extra round trip to the server to ensure the write was committed, defaults to true.
  17
+host: The host running MongoDB, defaults to localhost.
  18
+port: The port on the host that MongoDB is running on, defaults to MongoDB's default port.
  19
+Metadata: Logged as a native JSON object.
  20
+
36  package.json
... ...
@@ -0,0 +1,36 @@
  1
+{
  2
+  "name": "winston-simpledb",
  3
+  "description": "A Winston transport for Amazon SimpleDB",
  4
+  "version": "0.1.0",
  5
+  "author": {
  6
+    "name":"Andrew Chilton",
  7
+    "email":"chilts@appsattic.com",
  8
+    "url":"http://www.appsattic.com/"
  9
+  },
  10
+  "homepage": "https://github.com/appsattic/winston-simpledb",
  11
+  "contributors": [],
  12
+  "repository": {
  13
+    "type": "git",
  14
+    "url": "git://github.com/appsattic/winston-simpledb.git"
  15
+  },
  16
+  "keywords": ["logging", "sysadmin", "tools", "winston", "amazon", "simpledb", "awssum"],
  17
+  "dependencies": {
  18
+    "awssum": ">= 0.1.0",
  19
+    "uuid-js": ">= 0.7.4"
  20
+  },
  21
+  "devDependencies": {
  22
+    "winston": ">= 0.5.6",
  23
+    "vows":">= 0.5.13"
  24
+  },
  25
+  "main": "./winston-simpledb",
  26
+  "scripts": {
  27
+    "test": "vows test/*.js --spec"
  28
+  },
  29
+  "engines": {
  30
+    "node": ">= 0.4.0"
  31
+  },
  32
+  "licenses": [{
  33
+    "type": "MIT",
  34
+    "url": "http://github.com/appsattic/winston-simpledb/raw/master/LICENSE"
  35
+  }]
  36
+}
156  winston-simpledb.js
... ...
@@ -0,0 +1,156 @@
  1
+// --------------------------------------------------------------------------------------------------------------------
  2
+//
  3
+// winston-simpledb.js - transport for logging to Amazon SimpleDB
  4
+//
  5
+// Copyright (c) 2011 AppsAttic Ltd - http://www.appsattic.com/
  6
+// Written by Andrew Chilton <chilts@appsattic.com>
  7
+//
  8
+// License: http://opensource.org/licenses/MIT
  9
+//
  10
+// --------------------------------------------------------------------------------------------------------------------
  11
+
  12
+var util = require('util');
  13
+var amazon = require('awssum/lib/amazon/amazon');
  14
+var simpledb = require('awssum/lib/amazon/simpledb');
  15
+var winston = require('winston');
  16
+var UUID = require('uuid-js');
  17
+
  18
+// --------------------------------------------------------------------------------------------------------------------
  19
+
  20
+//
  21
+// ### function SimpleDB (options)
  22
+// Constructor for the SimpleDB transport object.
  23
+//
  24
+var SimpleDB = function (options) {
  25
+    options = options || {};
  26
+
  27
+    // need the accessKeyId
  28
+    if (!options.accessKeyId) {
  29
+        throw new Error("Required: accessKeyId for the Amazon account to log for.");
  30
+    }
  31
+
  32
+    // need the secretAccessKey
  33
+    if (!options.secretAccessKey) {
  34
+        throw new Error("Required: secretAccessKey for the Amazon account being used.");
  35
+    }
  36
+
  37
+    // need the awsAccountId
  38
+    if (!options.awsAccountId) {
  39
+        throw new Error("Required: awsAccountId for the Amazon account being used.");
  40
+    }
  41
+
  42
+    // need the domainName
  43
+    if (!options.domainName) {
  44
+        throw new Error("Required: domainName (or domainName generator) to log to.");
  45
+    }
  46
+
  47
+    // need the region
  48
+    if (!options.region) {
  49
+        throw new Error("Required: region the domain is in.");
  50
+    }
  51
+
  52
+    // Winston Options
  53
+    this.name  = 'simpledb';
  54
+    this.level = options.level || 'info';
  55
+
  56
+    // SimpleDB Options
  57
+    if (options.domainName) {
  58
+        this.domainName = options.domainName;
  59
+    }
  60
+    this.itemName = this.itemName || 'timestamp';
  61
+    if (options.itemName) {
  62
+        this.itemName = options.itemName;
  63
+    }
  64
+
  65
+    // create the SimpleDB instance
  66
+    this.sdb = new simpledb.SimpleDB(
  67
+        options.accessKeyId,
  68
+        options.secretAccessKey,
  69
+        options.awsAccountId,
  70
+        options.region
  71
+    );
  72
+};
  73
+
  74
+//
  75
+// Inherit from `winston.Transport` to take advantage of base functionality.
  76
+//
  77
+util.inherits(SimpleDB, winston.Transport);
  78
+
  79
+//
  80
+// ### function log (level, msg, [meta], callback)
  81
+// #### @level {string} Level at which to log the message.
  82
+// #### @msg {string} Message to log
  83
+// #### @meta {Object} **Optional** Additional metadata to attach
  84
+// #### @callback {function} Continuation to respond to when complete.
  85
+// Core logging method exposed to Winston. Metadata is optional.
  86
+//
  87
+SimpleDB.prototype.log = function (level, msg, meta, callback) {
  88
+    var self = this;
  89
+
  90
+    // console.log('RIGHT HERE - logging some stuff');
  91
+
  92
+    // create the domainName
  93
+    var domainName;
  94
+    if ( typeof this.domainName === 'function' ) {
  95
+        domainName = this.domainName();
  96
+    }
  97
+    else {
  98
+        domainName = this.domainName;
  99
+    }
  100
+    // console.log('domainName=' + domainName);
  101
+
  102
+    // create the itemName
  103
+    var itemName;
  104
+    if ( typeof this.itemName === 'function' ) {
  105
+        itemName = this.itemName();
  106
+    }
  107
+    else if ( this.itemName === 'uuid' ) {
  108
+        itemName = UUID.create().hex;
  109
+    }
  110
+    else if ( this.itemName === 'epoch' ) {
  111
+        itemName = Date.now();
  112
+    }
  113
+    else if ( this.itemName === 'timestamp' ) {
  114
+        itemName = (new Date()).toISOString();
  115
+    }
  116
+    // else, nothing (should never be here)
  117
+
  118
+    // console.log('domainName:', domainName);
  119
+    // console.log('itemName:', itemName);
  120
+    // console.log('meta:', meta);
  121
+
  122
+    // create the data to log
  123
+    var data = {
  124
+        level     : level,
  125
+        msg       : msg,
  126
+        inserted  : (new Date()).toISOString(),
  127
+    };
  128
+
  129
+    // add the meta information if there is any
  130
+    if ( meta ) {
  131
+        data.meta = JSON.stringify(meta);
  132
+    }
  133
+
  134
+    // store the message
  135
+    this.sdb.putAttributes({
  136
+        domainName : domainName,
  137
+        itemName   : itemName,
  138
+        data       : data,
  139
+    }, function(err, data) {
  140
+        // console.log('Error: ', util.inspect(err, true, null));
  141
+        // console.log('Data: ', util.inspect(data, true, null));
  142
+        if (err) {
  143
+            self.emit('error', err);
  144
+        }
  145
+
  146
+        self.emit('logged');
  147
+    });
  148
+
  149
+    // intially, tell the caller that everything was fine
  150
+    callback(null, true);
  151
+};
  152
+
  153
+//
  154
+// Add SimpleDB to the transports defined by winston.
  155
+//
  156
+winston.transports.SimpleDB = SimpleDB;

0 notes on commit e4b63fe

Please sign in to comment.
Something went wrong with that request. Please try again.