Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

New transport for winston to Amazon SimpleDB

  • Loading branch information...
commit e4b63fead537b05648686b0341d0a5800569013f 0 parents
@chilts authored
2  .gitignore
@@ -0,0 +1,2 @@
+node_modules
+*~
29 LICENSE
@@ -0,0 +1,29 @@
+-------------------------------------------------------------------------------
+
+This software is published under the MIT license as published here:
+
+* http://opensource.org/licenses/MIT
+
+-------------------------------------------------------------------------------
+
+Copyright 2011 Apps Attic Ltd. All rights reserved.
+
+Permission is hereby granted, free of charge, to any person obtaining a copy of
+this software and associated documentation files (the "Software"), to deal in
+the Software without restriction, including without limitation the rights to
+use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
+of the Software, and to permit persons to whom the Software is furnished to do
+so, subject to the following conditions:
+
+The above copyright notice and this permission notice shall be included in all
+copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+SOFTWARE.
+
+-------------------------------------------------------------------------------
20 README.md
@@ -0,0 +1,20 @@
+# SimpleDB Transport for Winston
+
+The winston-simpledb module allows you to log your winston messages to Amazon's SimpleDB.
+
+ var SimpleDB = require('winston-simpledb').SimpleDB;
+ winston.add(SimpleDB, options);
+
+The SimpleDB transport takes the following options.
+
+ level : the log level of messages
+
+level: Level of messages that this transport should log.
+silent: Boolean flag indicating whether to suppress output.
+db: The name of the database you want to log to. [required]
+collection: The name of the collection you want to store log messages in, defaults to 'log'.
+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.
+host: The host running MongoDB, defaults to localhost.
+port: The port on the host that MongoDB is running on, defaults to MongoDB's default port.
+Metadata: Logged as a native JSON object.
+
36 package.json
@@ -0,0 +1,36 @@
+{
+ "name": "winston-simpledb",
+ "description": "A Winston transport for Amazon SimpleDB",
+ "version": "0.1.0",
+ "author": {
+ "name":"Andrew Chilton",
+ "email":"chilts@appsattic.com",
+ "url":"http://www.appsattic.com/"
+ },
+ "homepage": "https://github.com/appsattic/winston-simpledb",
+ "contributors": [],
+ "repository": {
+ "type": "git",
+ "url": "git://github.com/appsattic/winston-simpledb.git"
+ },
+ "keywords": ["logging", "sysadmin", "tools", "winston", "amazon", "simpledb", "awssum"],
+ "dependencies": {
+ "awssum": ">= 0.1.0",
+ "uuid-js": ">= 0.7.4"
+ },
+ "devDependencies": {
+ "winston": ">= 0.5.6",
+ "vows":">= 0.5.13"
+ },
+ "main": "./winston-simpledb",
+ "scripts": {
+ "test": "vows test/*.js --spec"
+ },
+ "engines": {
+ "node": ">= 0.4.0"
+ },
+ "licenses": [{
+ "type": "MIT",
+ "url": "http://github.com/appsattic/winston-simpledb/raw/master/LICENSE"
+ }]
+}
156 winston-simpledb.js
@@ -0,0 +1,156 @@
+// --------------------------------------------------------------------------------------------------------------------
+//
+// winston-simpledb.js - transport for logging to Amazon SimpleDB
+//
+// Copyright (c) 2011 AppsAttic Ltd - http://www.appsattic.com/
+// Written by Andrew Chilton <chilts@appsattic.com>
+//
+// License: http://opensource.org/licenses/MIT
+//
+// --------------------------------------------------------------------------------------------------------------------
+
+var util = require('util');
+var amazon = require('awssum/lib/amazon/amazon');
+var simpledb = require('awssum/lib/amazon/simpledb');
+var winston = require('winston');
+var UUID = require('uuid-js');
+
+// --------------------------------------------------------------------------------------------------------------------
+
+//
+// ### function SimpleDB (options)
+// Constructor for the SimpleDB transport object.
+//
+var SimpleDB = function (options) {
+ options = options || {};
+
+ // need the accessKeyId
+ if (!options.accessKeyId) {
+ throw new Error("Required: accessKeyId for the Amazon account to log for.");
+ }
+
+ // need the secretAccessKey
+ if (!options.secretAccessKey) {
+ throw new Error("Required: secretAccessKey for the Amazon account being used.");
+ }
+
+ // need the awsAccountId
+ if (!options.awsAccountId) {
+ throw new Error("Required: awsAccountId for the Amazon account being used.");
+ }
+
+ // need the domainName
+ if (!options.domainName) {
+ throw new Error("Required: domainName (or domainName generator) to log to.");
+ }
+
+ // need the region
+ if (!options.region) {
+ throw new Error("Required: region the domain is in.");
+ }
+
+ // Winston Options
+ this.name = 'simpledb';
+ this.level = options.level || 'info';
+
+ // SimpleDB Options
+ if (options.domainName) {
+ this.domainName = options.domainName;
+ }
+ this.itemName = this.itemName || 'timestamp';
+ if (options.itemName) {
+ this.itemName = options.itemName;
+ }
+
+ // create the SimpleDB instance
+ this.sdb = new simpledb.SimpleDB(
+ options.accessKeyId,
+ options.secretAccessKey,
+ options.awsAccountId,
+ options.region
+ );
+};
+
+//
+// Inherit from `winston.Transport` to take advantage of base functionality.
+//
+util.inherits(SimpleDB, winston.Transport);
+
+//
+// ### function log (level, msg, [meta], callback)
+// #### @level {string} Level at which to log the message.
+// #### @msg {string} Message to log
+// #### @meta {Object} **Optional** Additional metadata to attach
+// #### @callback {function} Continuation to respond to when complete.
+// Core logging method exposed to Winston. Metadata is optional.
+//
+SimpleDB.prototype.log = function (level, msg, meta, callback) {
+ var self = this;
+
+ // console.log('RIGHT HERE - logging some stuff');
+
+ // create the domainName
+ var domainName;
+ if ( typeof this.domainName === 'function' ) {
+ domainName = this.domainName();
+ }
+ else {
+ domainName = this.domainName;
+ }
+ // console.log('domainName=' + domainName);
+
+ // create the itemName
+ var itemName;
+ if ( typeof this.itemName === 'function' ) {
+ itemName = this.itemName();
+ }
+ else if ( this.itemName === 'uuid' ) {
+ itemName = UUID.create().hex;
+ }
+ else if ( this.itemName === 'epoch' ) {
+ itemName = Date.now();
+ }
+ else if ( this.itemName === 'timestamp' ) {
+ itemName = (new Date()).toISOString();
+ }
+ // else, nothing (should never be here)
+
+ // console.log('domainName:', domainName);
+ // console.log('itemName:', itemName);
+ // console.log('meta:', meta);
+
+ // create the data to log
+ var data = {
+ level : level,
+ msg : msg,
+ inserted : (new Date()).toISOString(),
+ };
+
+ // add the meta information if there is any
+ if ( meta ) {
+ data.meta = JSON.stringify(meta);
+ }
+
+ // store the message
+ this.sdb.putAttributes({
+ domainName : domainName,
+ itemName : itemName,
+ data : data,
+ }, function(err, data) {
+ // console.log('Error: ', util.inspect(err, true, null));
+ // console.log('Data: ', util.inspect(data, true, null));
+ if (err) {
+ self.emit('error', err);
+ }
+
+ self.emit('logged');
+ });
+
+ // intially, tell the caller that everything was fine
+ callback(null, true);
+};
+
+//
+// Add SimpleDB to the transports defined by winston.
+//
+winston.transports.SimpleDB = SimpleDB;
Please sign in to comment.
Something went wrong with that request. Please try again.