-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathlogging.js
67 lines (54 loc) · 2.22 KB
/
logging.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
import { yellow, red, blue, grey } from 'chalk'
const formatId = message => `{${yellow(message.properties.correlationId.slice(0, 8))}}`
export const formatEvent = routingKey => `Event '${blue(routingKey)}' published`
export const formatOutgoingRequest = (correlationId, routingKey, appId) => [
`{${yellow(correlationId.slice(0, 8))}}`,
`Request '${blue(routingKey)}'`,
`sent by '${blue(appId)}'`
].join(' ')
export const formatIncomingRequest = message => [
`${formatId(message)}`,
`Received request '${yellow(message.fields.routingKey)}'`,
`from '${yellow(message.properties.appId)}'`
].join(' ')
export const formatIncomingEvent = message => [
`Received event '${yellow(message.fields.routingKey)}'`,
`published by '${yellow(message.properties.appId)}'`
].join(' ')
export const formatIncomingMessage = message => (message.properties.correlationId ?
formatIncomingRequest(message) :
formatIncomingEvent(message))
export const formatOutgoingError = (message, error) => [
`${formatId(message)}`,
`Error '${red(error.toString())}'`,
`sent to '${red(message.properties.replyTo)}'`,
`as response to '${red(message.properties.appId)}'`
].join(' ')
export const formatSubscriptionError = (message, error) => [
`Error '${red(error.toString())}'`,
`on event '${yellow(message.fields.routingKey)}'`,
`emitted by '${yellow(message.properties.appId)}'`
].join(' ')
export const formatOutgoingResponse = (message, error) => (error ?
formatOutgoingError(message, error) :
[
`${formatId(message)}`,
`Response for '${blue(message.properties.appId)}'`,
`sent to '${blue(message.properties.replyTo)}'`
].join(' '))
export const formatIncomingError = (message, error) => [
`${formatId(message)}`,
`Error '${red(error.toString())}'`,
`received from '${red(message.properties.appId)}'`
].join(' ')
export const formatIncomingResponse = (message, error) => (error ?
formatIncomingError(message, error) :
[
`${formatId(message)}`,
`Received response '${yellow(message.fields.routingKey)}'`,
`from '${yellow(message.properties.appId)}'`
].join(' '))
export const formatMeta = (source, message) => {
const time = new Date().toTimeString().split(' ')[0]
return `[${grey(time)}] [${source}] ${message}`
}