-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
64 lines (59 loc) · 1.38 KB
/
index.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
const bunyan = require('bunyan');
const BunyanSlack = require('bunyan-slack');
const createSerializers = require('./serializers');
const formatter = require('./formatter');
const level = process.env.LOG_LEVEL || 'info';
const src = process.env.LOG_SRC || false;
const webhook_url = process.env.SLACK_WEBHOOK_URL;
const channel = process.env.SLACK_CHANNEL;
const username = process.env.SLACK_USERNAME;
function getTestStreams() {
return process.env.NODE_ENV === 'test' ? [] : null;
}
function getDevelopmentStreams() {
return process.env.NODE_ENV === 'development'
? [
{
level,
stream: process.stdout
},
{
level: 'error',
stream: process.stderr
}
]
: null;
}
function getDefaultStreams() {
const streams = [
{
level,
stream: process.stdout
},
{
level: 'error',
stream: process.stderr
}
];
if (webhook_url) {
streams.push({
level: 'error',
stream: new BunyanSlack({
webhook_url,
channel,
username,
customFormatter: formatter
})
});
}
return streams;
}
module.exports = (pkg, serializers, streams) => bunyan.createLogger({
name: pkg.name,
version: pkg.version,
src,
serializers: createSerializers(serializers),
streams: (getTestStreams()
|| getDevelopmentStreams()
|| getDefaultStreams()).concat(streams || [])
});