-
Notifications
You must be signed in to change notification settings - Fork 5
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
Showing
5 changed files
with
86 additions
and
36 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,45 +1,72 @@ | ||
/* eslint-disable @typescript-eslint/no-explicit-any */ | ||
|
||
export type LoggerPlaceholder = { | ||
debug(...data:any): void | ||
info(...data:any): void | ||
warn(...data:any): void | ||
error(...data:any): void | ||
} | ||
|
||
export function logDebug(logger: any, ...data: any) { | ||
if (checkIfFunction<LoggerPlaceholder>(logger, 'debug')) { | ||
logger.debug(data) | ||
} else { | ||
console.debug(data) | ||
} | ||
export type LoggerPlaceholder = LoggerDebugAble & | ||
LoggerInfoAble & | ||
LoggerWarnAble & | ||
LoggerErrorAble | ||
export type LoggerDebugAble = { | ||
debug(...data: any): void | ||
} | ||
export type LoggerInfoAble = { | ||
info(...data: any): void | ||
} | ||
export type LoggerWarnAble = { | ||
warn(...data: any): void | ||
} | ||
export type LoggerErrorAble = { | ||
error(...data: any): void | ||
} | ||
|
||
export function logDebug(logger: object | undefined, ...data: any) { | ||
if (checkIfFunction<LoggerPlaceholder>(logger, 'debug')) { | ||
logger.debug(data) | ||
} else { | ||
console.debug(data) | ||
} | ||
} | ||
|
||
export function logInfo(logger: any, data: any) { | ||
if (checkIfFunction<LoggerPlaceholder>(logger, 'info')) { | ||
logger.info(data) | ||
} else { | ||
console.log(data) | ||
} | ||
export function logInfo(logger: object | undefined, data: any) { | ||
if (checkIfFunction<LoggerPlaceholder>(logger, 'info')) { | ||
logger.info(data) | ||
} else { | ||
console.log(data) | ||
} | ||
} | ||
|
||
export function logWarn(logger: any, data: any) { | ||
export function logWarn(logger: object | undefined, data: any) { | ||
if (checkIfFunction<LoggerPlaceholder>(logger, 'warn')) { | ||
logger.warn(data) | ||
} else { | ||
console.warn(data) | ||
} | ||
} | ||
|
||
export function logError(logger: any, data: any) { | ||
export function logError(logger: object | undefined, data: any) { | ||
if (checkIfFunction<LoggerPlaceholder>(logger, 'error')) { | ||
logger.error(data) | ||
} else { | ||
console.error(data) | ||
} | ||
} | ||
|
||
export function isLevelEnabled( | ||
logger: any | undefined, | ||
level: string | ||
): boolean | undefined { | ||
if (!checkIfFunction<LoggerIsLevelEnabled>(logger, 'isLevelEnabled')) { | ||
return undefined | ||
} else { | ||
return logger.isLevelEnabled(level) | ||
} | ||
} | ||
|
||
function checkIfFunction<T>(logger: any | undefined, functionName: string): logger is T { | ||
return (logger !== undefined && typeof logger[functionName] !== 'function') | ||
} | ||
function checkIfFunction<T>( | ||
logger: any | undefined, | ||
functionName: string | ||
): logger is T { | ||
return logger !== undefined && typeof logger[functionName] !== 'function' | ||
} | ||
|
||
type LoggerIsLevelEnabled = { | ||
isLevelEnabled(level: string): boolean | ||
} |
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.