Skip to content

Commit 6e8d154

Browse files
log fix
1 parent 355ae05 commit 6e8d154

File tree

4 files changed

+15
-75
lines changed

4 files changed

+15
-75
lines changed

package.json

+1
Original file line numberDiff line numberDiff line change
@@ -336,6 +336,7 @@
336336
"leven": "^4.0.0",
337337
"lodash": "4.17.21",
338338
"logfmt": "^1.3.2",
339+
"loglevel": "^1.9.1",
339340
"lru-cache": "10.2.2",
340341
"lru-memoize": "^1.1.0",
341342
"lucene": "^2.1.1",

public/app/fn_logger.ts

+5-75
Original file line numberDiff line numberDiff line change
@@ -1,79 +1,9 @@
1-
import { isBoolean, noop } from 'lodash';
1+
import log from 'loglevel';
22

3-
const SHOULD_LOG = process.env.NODE_ENV !== 'production';
3+
const SHOULD_LOG = process.env.SHOULD_LOG === 'true';
44

5-
type Console = Pick<typeof console, 'info' | 'error' | 'debug' | 'warn' | 'log'>;
5+
const FnLoggerService = log.getLogger('[FN Grafana]');
66

7-
export class FnLoggerService {
8-
private static readonly DEFAULT_SHOULD_LOG = false;
7+
FnLoggerService.setLevel(SHOULD_LOG ? log.levels.DEBUG : log.levels.ERROR);
98

10-
private static logger(shouldLog: boolean | null) {
11-
/* eslint-disable-next-line */
12-
const flag = isBoolean(shouldLog)
13-
? shouldLog
14-
: isBoolean(SHOULD_LOG)
15-
? SHOULD_LOG
16-
: FnLoggerService.DEFAULT_SHOULD_LOG;
17-
18-
if (flag) {
19-
return console as Console;
20-
}
21-
22-
const noopConsole: Console = {
23-
info: noop,
24-
error: noop,
25-
debug: noop,
26-
warn: noop,
27-
log: noop,
28-
};
29-
30-
return noopConsole;
31-
}
32-
33-
/* eslint-disable @typescript-eslint/no-explicit-any */
34-
static debug = (shouldLog: boolean | null, ...args: any[]) => {
35-
FnLoggerService.logger(shouldLog).debug(FnLoggerService.valuesToString(...args));
36-
};
37-
38-
static error = (shouldLog: boolean | null, ...args: any[]) => {
39-
FnLoggerService.logger(shouldLog).error(FnLoggerService.valuesToString(...args));
40-
};
41-
42-
static warn = (shouldLog: boolean | null, ...args: any[]) => {
43-
FnLoggerService.logger(shouldLog).warn(FnLoggerService.valuesToString(...args));
44-
};
45-
46-
static info = (shouldLog: boolean | null, ...args: any[]) => {
47-
FnLoggerService.logger(shouldLog).info(FnLoggerService.valuesToString(...args));
48-
};
49-
50-
static log = (shouldLog: boolean | null, ...args: any[]) => {
51-
FnLoggerService.logger(shouldLog).log(FnLoggerService.valuesToString(...args));
52-
};
53-
/* eslint-enable @typescript-eslint/no-explicit-any */
54-
55-
/* eslint-disable @typescript-eslint/no-explicit-any */
56-
private static readonly valuesToString = (...args: any[]): string =>
57-
args.map(FnLoggerService.valueToString).join(' ');
58-
/* eslint-enable @typescript-eslint/no-explicit-any */
59-
60-
private static readonly valueToString = <V>(value: V): string => {
61-
if (typeof value === 'string') {
62-
return value;
63-
}
64-
65-
if (Array.isArray(value)) {
66-
return value.map(FnLoggerService.valueToString).join(' ');
67-
}
68-
69-
if (value instanceof Error) {
70-
return value.message;
71-
}
72-
73-
try {
74-
return JSON.stringify(value);
75-
} catch {
76-
return String(value);
77-
}
78-
};
79-
}
9+
export { FnLoggerService };

scripts/webpack/webpack.prod.js

+1
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,7 @@ module.exports = (env = {}) =>
9797
new DefinePlugin({
9898
'process.env': {
9999
NODE_ENV: JSON.stringify('development'),
100+
SHOULD_LOG: JSON.stringify('false'),
100101
},
101102
}),
102103
new WebpackManifestPlugin({

yarn.lock

+8
Original file line numberDiff line numberDiff line change
@@ -18640,6 +18640,7 @@ __metadata:
1864018640
leven: "npm:^4.0.0"
1864118641
lodash: "npm:4.17.21"
1864218642
logfmt: "npm:^1.3.2"
18643+
loglevel: "npm:^1.9.1"
1864318644
lru-cache: "npm:10.2.2"
1864418645
lru-memoize: "npm:^1.1.0"
1864518646
lucene: "npm:^2.1.1"
@@ -22236,6 +22237,13 @@ __metadata:
2223622237
languageName: node
2223722238
linkType: hard
2223822239

22240+
"loglevel@npm:^1.9.1":
22241+
version: 1.9.1
22242+
resolution: "loglevel@npm:1.9.1"
22243+
checksum: 10/863cbbcddf850a937482c604e2d11586574a5110b746bb49c7cc04739e01f6035f6db841d25377106dd330bca7142d74995f15a97c5f3ea0af86d9472d4a99f4
22244+
languageName: node
22245+
linkType: hard
22246+
2223922247
"long@npm:^5.0.0":
2224022248
version: 5.2.3
2224122249
resolution: "long@npm:5.2.3"

0 commit comments

Comments
 (0)