-
Notifications
You must be signed in to change notification settings - Fork 13
/
winston-dynamodb.js
157 lines (145 loc) · 4.27 KB
/
winston-dynamodb.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
156
157
(function() {
var AWS, DynamoDB, _, datify, hostname, util, uuid, winston,
indexOf = [].indexOf || function(item) { for (var i = 0, l = this.length; i < l; i++) { if (i in this && this[i] === item) return i; } return -1; };
winston = require("winston");
util = require("util");
AWS = require("aws-sdk");
uuid = require("node-uuid");
_ = require("lodash");
hostname = require("os").hostname();
datify = function(timestamp) {
var date, i, key, keys, len;
date = new Date(timestamp);
date = {
year: date.getFullYear(),
month: date.getMonth() + 1,
day: date.getDate(),
hour: date.getHours(),
minute: date.getMinutes(),
second: date.getSeconds(),
millisecond: date.getMilliseconds()
};
keys = _.without(Object.keys(date, "year", "month", "day"));
for (i = 0, len = keys.length; i < len; i++) {
key = keys[i];
if (date[key] < 10) {
date[key] = "0" + date[key];
}
}
return date.year + "-" + date.month + "-" + date.day + " " + date.hour + ":" + date.minute + ":" + date.second + "." + date.millisecond;
};
DynamoDB = exports.DynamoDB = function(options) {
var ref, regions;
if (options == null) {
options = {};
}
regions = ["localhost", "us-east-1", "us-west-1", "us-west-2", "eu-west-1", "eu-central-1", "ap-northeast-1", "ap-northeast-2", "ap-southeast-1", "ap-southeast-2", "sa-east-1"];
if (options.useEnvironment) {
options.accessKeyId = process.env.AWS_ACCESS_KEY_ID;
options.secretAccessKey = process.env.AWS_SECRET_ACCESS_KEY;
options.region = process.env.AWS_REGION;
}
if (options.accessKeyId == null) {
throw new Error("need accessKeyId");
}
if (options.secretAccessKey == null) {
throw new Error("need secretAccessKey");
}
if (options.region == null) {
throw new Error("need region");
}
if (ref = options.region, indexOf.call(regions, ref) < 0) {
throw new Error("unavailable region given");
}
if (options.tableName == null) {
throw new Error("need tableName");
}
if (!options.useEnvironment) {
AWS.config.update({
accessKeyId: options.accessKeyId,
secretAccessKey: options.secretAccessKey,
region: options.region
});
}
this.name = "dynamodb";
this.level = options.level || "info";
if (options.region == "localhost") {
this.db = new AWS.DynamoDB({
endpoint: new AWS.Endpoint(options.endpoint)
});
}
else {
this.db = new AWS.DynamoDB();
}
this.AWS = AWS;
this.region = options.region;
this.tableName = options.tableName;
return this.dynamoDoc = options.dynamoDoc;
};
util.inherits(DynamoDB, winston.Transport);
DynamoDB.prototype.log = function(level, msg, meta, callback) {
var dynamoDocClient, params, putCallback;
putCallback = (function(_this) {
return function(err, data) {
if (err) {
_this.emit("error", err);
if (callback) {
return callback(err, null);
}
} else {
_this.emit("logged");
if (callback) {
return callback(null, "logged");
}
}
};
})(this);
if (this.dynamoDoc === true) {
params = {
TableName: this.tableName,
Item: {
id: uuid.v4(),
level: level,
timestamp: datify(Date.now()),
msg: msg,
hostname: hostname
}
};
if (!_.isEmpty(meta)) {
params.Item.meta = meta;
}
dynamoDocClient = new this.AWS.DynamoDB.DocumentClient({
service: this.db
});
return dynamoDocClient.put(params, putCallback);
} else {
params = {
TableName: this.tableName,
Item: {
id: {
"S": uuid.v4()
},
level: {
"S": level
},
timestamp: {
"S": datify(Date.now())
},
msg: {
"S": msg
},
hostname: {
"S": hostname
}
}
};
if (!_.isEmpty(meta)) {
params.Item.meta = {
"S": JSON.stringify(meta)
};
}
return this.db.putItem(params, putCallback);
}
};
winston.transports.DynamoDB = DynamoDB;
}).call(this);