forked from grafana/grafana
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
538f96a
commit d4ead5f
Showing
7 changed files
with
35 additions
and
105 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,79 +1,9 @@ | ||
import { isBoolean, noop } from 'lodash'; | ||
import log from 'loglevel'; | ||
|
||
const SHOULD_LOG = process.env.NODE_ENV !== 'production'; | ||
const SHOULD_LOG = process.env.SHOULD_LOG === 'true'; | ||
|
||
type Console = Pick<typeof console, 'info' | 'error' | 'debug' | 'warn' | 'log'>; | ||
const FnLoggerService = log.getLogger('[FN Grafana]'); | ||
|
||
export class FnLoggerService { | ||
private static readonly DEFAULT_SHOULD_LOG = false; | ||
FnLoggerService.setLevel(SHOULD_LOG ? log.levels.DEBUG : log.levels.ERROR); | ||
|
||
private static logger(shouldLog: boolean | null) { | ||
/* eslint-disable-next-line */ | ||
const flag = isBoolean(shouldLog) | ||
? shouldLog | ||
: isBoolean(SHOULD_LOG) | ||
? SHOULD_LOG | ||
: FnLoggerService.DEFAULT_SHOULD_LOG; | ||
|
||
if (flag) { | ||
return console as Console; | ||
} | ||
|
||
const noopConsole: Console = { | ||
info: noop, | ||
error: noop, | ||
debug: noop, | ||
warn: noop, | ||
log: noop, | ||
}; | ||
|
||
return noopConsole; | ||
} | ||
|
||
/* eslint-disable @typescript-eslint/no-explicit-any */ | ||
static debug = (shouldLog: boolean | null, ...args: any[]) => { | ||
FnLoggerService.logger(shouldLog).debug(FnLoggerService.valuesToString(...args)); | ||
}; | ||
|
||
static error = (shouldLog: boolean | null, ...args: any[]) => { | ||
FnLoggerService.logger(shouldLog).error(FnLoggerService.valuesToString(...args)); | ||
}; | ||
|
||
static warn = (shouldLog: boolean | null, ...args: any[]) => { | ||
FnLoggerService.logger(shouldLog).warn(FnLoggerService.valuesToString(...args)); | ||
}; | ||
|
||
static info = (shouldLog: boolean | null, ...args: any[]) => { | ||
FnLoggerService.logger(shouldLog).info(FnLoggerService.valuesToString(...args)); | ||
}; | ||
|
||
static log = (shouldLog: boolean | null, ...args: any[]) => { | ||
FnLoggerService.logger(shouldLog).log(FnLoggerService.valuesToString(...args)); | ||
}; | ||
/* eslint-enable @typescript-eslint/no-explicit-any */ | ||
|
||
/* eslint-disable @typescript-eslint/no-explicit-any */ | ||
private static readonly valuesToString = (...args: any[]): string => | ||
args.map(FnLoggerService.valueToString).join(' '); | ||
/* eslint-enable @typescript-eslint/no-explicit-any */ | ||
|
||
private static readonly valueToString = <V>(value: V): string => { | ||
if (typeof value === 'string') { | ||
return value; | ||
} | ||
|
||
if (Array.isArray(value)) { | ||
return value.map(FnLoggerService.valueToString).join(' '); | ||
} | ||
|
||
if (value instanceof Error) { | ||
return value.message; | ||
} | ||
|
||
try { | ||
return JSON.stringify(value); | ||
} catch { | ||
return String(value); | ||
} | ||
}; | ||
} | ||
export { FnLoggerService }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters