-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
66 lines (61 loc) · 1.54 KB
/
app.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
require('dotenv').config()
require('spm-agent-nodejs')
const express = require('express')
const app = express()
const winston = require('winston')
const morgan = require('morgan')
const json = require('morgan-json')
const format = json({
method: ':method',
url: ':url',
status: ':status',
contentLength: ':res[content-length]',
responseTime: ':response-time'
})
const Logsene = require('winston-logsene')
const logger = winston.createLogger({
transports: [new Logsene({
token: process.env.LOGS_TOKEN,
level: 'info',
type: 'api_logs',
url: 'https://logsene-receiver.sematext.com/_bulk'
})]
})
const httpLogger = morgan(format, {
stream: {
write: (message) => {
const m = JSON.parse(message)
const pm = {
method: m.method,
url: m.url,
status: Number(m.status),
contentLength: m.contentLength,
responseTime: Number(m.responseTime)
}
console.log(pm)
logger.info('HTTP LOG', pm)
}
}
})
app.use(httpLogger)
app.get('/api', (req, res, next) => {
logger.info('Api Works.')
res.status(200).send('Api Works.')
})
app.get('/api/fast', (req, res, next) => {
res.status(200).send('Fast response!')
})
app.get('/api/slow', (req, res, next) => {
setTimeout(() => {
res.status(200).send('Slow response...')
}, 1000)
})
app.get('/api/error', (req, res, next) => {
try {
throw new Error('Something broke...')
} catch (error) {
logger.error(error)
res.status(500).send(error)
}
})
app.listen(3000, () => console.log('Server is running on port 3000'))