-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
111 lines (93 loc) · 2.89 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
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
const Transport = require('winston-transport')
const { struct } = require('pb-util')
const { MESSAGE } = require('triple-beam')
const { YCLogClient } = require('@1xtr/yc-log-client')
/**
* @typedef {Object} YCLogServiceOptions
* @property {string | undefined} [folderId=undefined] folderId
* @property {string | undefined} [logGroupId=undefined] logGroupId
* @property {string | undefined} [streamName=undefined] streamName
* @property {string | undefined} [resourceType=undefined] resourceType
* @property {string | undefined} [resourceId=undefined] resourceId
* @property {string | undefined} [tokenUrl=undefined] Custom url for get token
*/
class YCLoggingTransport extends Transport {
/**
* @param {YCLogServiceOptions} opts
*/
constructor(opts) {
super(opts)
if (!opts.folderId && !opts.logGroupId) {
throw new Error('folderId or logGroupId is required')
}
this.folderId = opts.folderId
this.logGroupId = opts.logGroupId
this.streamName = opts.streamName
this.resourceType = opts.resourceType
this.resourceId = opts.resourceId
this.YCLogger = opts.tokenUrl ? new YCLogClient(opts.tokenUrl) : new YCLogClient()
}
async log(info, callback) {
setImmediate(() => {
this.emit('logged', info)
})
if (!this.YCLogger.ready) {
console.log('Token expired')
await this.YCLogger.init()
}
let meta = {}
if (info[MESSAGE]) {
try {
meta = JSON.parse(info[MESSAGE])
} catch (error) {
console.log('error json parse')
}
}
let level = info.level.toUpperCase()
if (['verbose', 'http', 'silly'].includes(info.level)) {
level = 'TRACE'
}
const timestamp = info.timestamp ? Number(info.timestamp) : Date.now()
const writeRequest = {
destination: {
// folder_id: undefined, // highest priority than logGroupId
// log_group_id: undefined,
},
resource: {
// type: undefined,
// id: undefined,
},
defaults: {
// stream_name: undefined,
// json_payload: undefined,
},
entries: [],
}
if (this.folderId) {
writeRequest.destination.folder_id = this.folderId
} else if (this.logGroupId) {
writeRequest.destination.log_group_id = this.logGroupId
}
if (this.streamName) {
writeRequest.defaults.stream_name = this.streamName
}
if (this.resourceType) {
writeRequest.resource.type = this.resourceType
}
if (this.resourceId) {
writeRequest.resource.id = this.resourceId
}
writeRequest.entries.push({
level,
timestamp: {
seconds: Math.trunc(timestamp / 1000),
nanos: (timestamp % 1000) * 1e6,
},
message: info.message || info.msg || 'Empty message',
json_payload: struct.encode(meta),
})
this.YCLogger.write(writeRequest)
callback()
}
}
module.exports = { YCLoggingTransport }