forked from DoSomething/lambda-papertrail
-
Notifications
You must be signed in to change notification settings - Fork 0
/
handler.js
40 lines (33 loc) · 1.27 KB
/
handler.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
'use strict';
const path = require('path');
const zlib = require('zlib');
const winston = require('winston');
// Register Papertrail transport w/ Winston.
require('winston-papertrail');
module.exports.log = (event, context, callback) => {
// Parse incoming Cloudwatch logs, which are base64-encoded & gzipped:
const payload = new Buffer(event.awslogs.data, 'base64');
zlib.gunzip(payload, (err, result) => {
if (err) {
callback(err);
} else {
const json = JSON.parse(result.toString('utf8'));
// Parse a human-readable hostname & program from the log group.
const logGroup = path.parse(json.logGroup);
// Configure the Papertrail connection.
const papertrail = new winston.transports.Papertrail({
host: process.env.PAPERTRAIL_HOST,
port: process.env.PAPERTRAIL_PORT,
hostname: logGroup.name, // e.g. 'bertly-dev-app'
program: logGroup.dir.replace('/aws/', ''), // e.g. 'lambda'
logFormat: (level, message) => message,
flushOnClose: true,
});
// Forward each of the log messages to Papertrail.
const logger = new winston.Logger({ transports: [papertrail] });
json.logEvents.forEach(log => logger.info(log.message));
logger.close();
callback(null);
}
});
};