-
Notifications
You must be signed in to change notification settings - Fork 7
/
winston-simpledb.js
155 lines (133 loc) · 4.45 KB
/
winston-simpledb.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
// --------------------------------------------------------------------------------------------------------------------
//
// winston-simpledb.js : transport for logging to Amazon SimpleDB
//
// Author : Andrew Chilton
// Web : http://www.chilts.org/blog/
// Email : <chilts@appsattic.com>
//
// Copyright (c) : 2011 AppsAttic Ltd
// Web : http://www.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 = exports.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)
// 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;