Skip to content

Commit ff51527

Browse files
committedJul 20, 2021
feat(client): add simple log interface
1 parent dac7873 commit ff51527

File tree

2 files changed

+96
-5
lines changed

2 files changed

+96
-5
lines changed
 

‎src/index.ts

+95-5
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,96 @@
1-
export const sum = (a: number, b: number) => {
2-
if ('development' === process.env.NODE_ENV) {
3-
console.log('boop');
1+
interface GraylogClientOptions {
2+
server: string;
3+
source: string;
4+
}
5+
6+
type StatusCallback = ({
7+
status,
8+
statusText,
9+
}: {
10+
status: number;
11+
statusText: string;
12+
}) => unknown;
13+
14+
interface Gelf {
15+
version?: string;
16+
host?: string;
17+
short_message: string;
18+
level: number;
19+
}
20+
21+
enum Level {
22+
emergency,
23+
alert,
24+
critical,
25+
error,
26+
warning,
27+
notice,
28+
info,
29+
debug,
30+
}
31+
32+
// prettier-ignore
33+
export default class GraylogClient<ExtrasType = { [key: string] : string }> {
34+
public server: string;
35+
public source: string;
36+
37+
constructor(_options: GraylogClientOptions) {
38+
if(!_options || !_options.server || !_options.source) {
39+
throw Error("GraylogClient was instantiated with insufficient parameters.")
40+
}
41+
42+
this.server = _options.server;
43+
this.source = _options.source;
444
}
5-
return a + b;
6-
};
45+
46+
private get defaultProperties() {
47+
return { version: '1.1', host: this.source };
48+
}
49+
50+
private sendRequest(gelf: Gelf, callback?: StatusCallback) {
51+
return fetch(this.server, {
52+
method: 'POST',
53+
headers: {
54+
'Content-Type': 'application/json',
55+
},
56+
body: JSON.stringify({ ...this.defaultProperties, ...gelf }),
57+
})
58+
.then(response => {
59+
if (!response.ok) throw Error('An error occured while sending a log to Graylog: ' + response.statusText);
60+
return response;
61+
})
62+
.then(response => {
63+
const { status, statusText } = response;
64+
callback && callback({ status, statusText });
65+
return response;
66+
})
67+
.catch((err: Error) => {
68+
console.error(err.stack);
69+
});
70+
}
71+
72+
emergency(message: string, extras?: ExtrasType) {
73+
return this.sendRequest({ short_message: message, level: Level.emergency, ...extras });
74+
}
75+
alert(message: string, extras?: ExtrasType) {
76+
return this.sendRequest({ short_message: message, level: Level.alert, ...extras });
77+
}
78+
critical(message: string, extras?: ExtrasType) {
79+
return this.sendRequest({ short_message: message, level: Level.critical, ...extras });
80+
}
81+
error(message: string, extras?: ExtrasType) {
82+
return this.sendRequest({ short_message: message, level: Level.error, ...extras });
83+
}
84+
warning(message: string, extras?: ExtrasType) {
85+
return this.sendRequest({ short_message: message, level: Level.warning, ...extras });
86+
}
87+
notice(message: string, extras?: ExtrasType) {
88+
return this.sendRequest({ short_message: message, level: Level.notice, ...extras });
89+
}
90+
info(message: string, extras?: ExtrasType) {
91+
return this.sendRequest({ short_message: message, level: Level.info, ...extras });
92+
}
93+
debug(message: string, extras?: ExtrasType) {
94+
return this.sendRequest({ short_message: message, level: Level.debug, ...extras });
95+
}
96+
}

‎tsconfig.json

+1
Original file line numberDiff line numberDiff line change
@@ -31,5 +31,6 @@
3131
"forceConsistentCasingInFileNames": true,
3232
// `tsdx build` ignores this option, but it is commonly used when type-checking separately with `tsc`
3333
"noEmit": true,
34+
"target": "ES5"
3435
}
3536
}

0 commit comments

Comments
 (0)
Please sign in to comment.