-
Notifications
You must be signed in to change notification settings - Fork 0
/
Logger.js
140 lines (125 loc) · 3.36 KB
/
Logger.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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
const fetch = require('node-fetch');
async function log(message, type, fiberyHost, fiberyToken, fiberyProjectId) {
try {
let typeId = null;
switch (type) {
case 'warning':
typeId = '1f7a60d1-a6c7-11ed-9d94-b91e86b22cf1';
break;
case 'error':
typeId = '22157e11-a6c7-11ed-9d94-b91e86b22cf1';
break;
default:
typeId = 'db61f980-a6c6-11ed-9d94-b91e86b22cf1';
}
// const schema = await fibery.getSchema();
// let jsonSchema = JSON.stringify(schema);
// fs.writeFile('schema.json', jsonSchema, 'utf8', () => {});
// console.log(schema);
let query = `mutation {
logs {
create(
state: {
id : { is : "9e47c600-a6c7-11ed-9d94-b91e86b22cf1" }
}
project:{
id : { is : "${fiberyProjectId}" }
}
type:{
id : { is : "${typeId}" }
}
){
entities {
id
type
}
}
}
}`;
let YOUR_SPACE_ENDPOINT =
'https://' + fiberyHost + '/api/graphql/space/Project_Management';
let newLogRequest = await fetch(YOUR_SPACE_ENDPOINT, {
method: 'POST',
body: JSON.stringify({
query: query,
}),
headers: {
'Content-Type': `application/json`,
Authorization: `Token ${fiberyToken}`,
},
});
if (newLogRequest.status == 200) {
let log = await newLogRequest.json();
if (log?.data?.logs?.create?.entities?.length > 0) {
let logObj = log?.data?.logs?.create?.entities[0];
let logId = logObj.id;
let logMessageQuery = `mutation {
logs(id: {is : "${logId}"}) {
appendContentToLogMessage(value:"${message}"){message}
}
}`;
let logMessageRequest = await fetch(YOUR_SPACE_ENDPOINT, {
method: 'POST',
body: JSON.stringify({
query: logMessageQuery,
}),
headers: {
'Content-Type': `application/json`,
Authorization: `Token ${fiberyToken}`,
},
});
if (logMessageRequest.status == 200) {
return true;
} else {
console.error('Could not add Message to log');
return null;
}
} else {
console.error('Could not create log');
return null;
}
} else {
console.error('Could not connect to fibery');
return null;
}
} catch (error) {
console.log('error in log message');
console.log(error);
return null;
}
}
class Logger {
constructor(fiberyHost, fiberyToken, fiberyProjectId) {
this.fiberyHost = fiberyHost;
this.fiberyToken = fiberyToken;
this.fiberyProjectId = fiberyProjectId;
}
async info(message) {
return log(
message,
'info',
this.fiberyHost,
this.fiberyToken,
this.fiberyProjectId
);
}
async warn(message) {
return log(
message,
'warning',
this.fiberyHost,
this.fiberyToken,
this.fiberyProjectId
);
}
async error(message) {
return log(
message,
'error',
this.fiberyHost,
this.fiberyToken,
this.fiberyProjectId
);
}
}
module.exports = Logger;