-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathwinston-dynamodb.coffee
127 lines (105 loc) · 2.83 KB
/
winston-dynamodb.coffee
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
winston = require "winston"
util = require "util"
AWS = require "aws-sdk"
uuid = require("node-uuid")
_ = require "lodash"
hostname = require("os").hostname()
# Return timestamp with YYYY-MM-DD HH:mm:ss
datify = (timestamp) ->
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"
date[key] = "0" + date[key] for key in keys when date[key] < 10
"#{date.year}-#{date.month}-#{date.day} #{date.hour}:#{date.minute}:#{date.second}.#{date.millisecond}"
DynamoDB = exports.DynamoDB = (options = {}) ->
regions = [
"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
unless options.accessKeyId?
throw new Error "need accessKeyId"
unless options.secretAccessKey?
throw new Error "need secretAccessKey"
unless options.region?
throw new Error "need region"
unless options.region in regions
throw new Error "unavailable region given"
unless options.tableName?
throw new Error "need tableName"
unless options.useEnvironment
AWS.config.update
accessKeyId: options.accessKeyId
secretAccessKey: options.secretAccessKey
region: options.region
# Winston Options
@.name = "dynamodb"
@.level = options.level or "info"
# DynamoDB Options=
@.db = new AWS.DynamoDB()
@.AWS = AWS
@.region = options.region
# a-z, A-Z, 0-9, _ (underscore), - (hyphen) and . (period)
@.tableName = options.tableName
@.dynamoDoc = options.dynamoDoc
util.inherits DynamoDB, winston.Transport
DynamoDB::log = (level, msg, meta, callback) ->
putCallback = (err, data) =>
if err
@.emit "error", err
callback err, null if callback
else
@.emit "logged"
callback null, "logged" if callback
if @.dynamoDoc == true
params =
TableName: @.tableName
Item:
id: uuid.v4()
level: level
timestamp: datify Date.now()
msg: msg
hostname: hostname
unless _.isEmpty meta
params.Item.meta = meta
dynamoDocClient = new @.AWS.DynamoDB.DocumentClient({
service: @.db
})
dynamoDocClient.put params, putCallback
else
params =
TableName: @.tableName
Item:
id:
"S": uuid.v4()
level:
"S": level
timestamp:
"S": datify Date.now()
msg:
"S": msg
hostname:
"S": hostname
unless _.isEmpty meta
params.Item.meta = "S": JSON.stringify meta
@.db.putItem params, putCallback
# Add DynamoDB to the transports by winston
winston.transports.DynamoDB = DynamoDB