-
Notifications
You must be signed in to change notification settings - Fork 75
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: Move default logger to demo app (#1004)
When a logger is not specified as a prop of the entry point Hyperview component, we currently instantiate a default logger that will emit log lines to the console. This can causes performance issues for devs that don't implement a logger, and is noticed in the current demo app. This PR removes the default logger from core, and implement it explicitly in the demo app. The default log level in the demo app is set to `log`, to avoid logging behavior logs, which contribute to a more sluggish UX. --------- Co-authored-by: flochtililoch <[email protected]>
- Loading branch information
1 parent
7e86786
commit 42e1534
Showing
7 changed files
with
71 additions
and
69 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 |
---|---|---|
@@ -0,0 +1,2 @@ | ||
export * from './misc'; | ||
export * from './logger'; |
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 |
---|---|---|
@@ -0,0 +1,62 @@ | ||
// eslint-disable-next-line no-shadow | ||
enum Level { | ||
info = 0, | ||
log = 1, | ||
warn = 2, | ||
error = 3, | ||
} | ||
|
||
const Levels = [Level.info, Level.log, Level.warn, Level.error]; | ||
const LogFunctions = { | ||
[Level.info]: console.info, | ||
[Level.log]: console.log, | ||
[Level.warn]: console.warn, | ||
[Level.error]: console.error, | ||
}; | ||
|
||
/** | ||
* Handles deferredToString objects by calling toString() prior to forwarding to console | ||
*/ | ||
const convertDeferred = (m?: unknown): string | unknown => { | ||
try { | ||
return m?.toString(); | ||
} catch (e) { | ||
return m; | ||
} | ||
}; | ||
|
||
export class Logger { | ||
public static Level = Level; | ||
|
||
protected level: number; | ||
|
||
constructor(level: Level) { | ||
this.level = Levels.indexOf(level); | ||
} | ||
|
||
public log = (m?: unknown, ...p: unknown[]): void => { | ||
this.emitter(Level.log, m, ...p); | ||
}; | ||
|
||
public info = (m?: unknown, ...p: unknown[]): void => { | ||
this.emitter(Level.info, m, ...p); | ||
}; | ||
|
||
public warn = (m?: unknown, ...p: unknown[]): void => { | ||
this.emitter(Level.warn, m, ...p); | ||
}; | ||
|
||
public error = (m?: unknown, ...p: unknown[]): void => { | ||
this.emitter(Level.error, m, ...p); | ||
}; | ||
|
||
protected emitter = (level: Level, m?: unknown, ...p: unknown[]): void => { | ||
if (this.level > Levels.indexOf(level)) { | ||
return; | ||
} | ||
LogFunctions[level]( | ||
convertDeferred(m), | ||
p.map(param => convertDeferred(param)), | ||
); | ||
}; | ||
} |
File renamed without changes.
This file was deleted.
Oops, something went wrong.
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,20 +1,19 @@ | ||
/* eslint-disable @typescript-eslint/no-explicit-any */ | ||
|
||
import { DefaultLogger } from './default-logger'; | ||
import type { Logger } from './types'; | ||
|
||
let logger: Logger = new DefaultLogger(); | ||
let logger: Logger | null; | ||
|
||
export function initialize(loggerInstance: Logger | undefined): void { | ||
if (loggerInstance) { | ||
logger = loggerInstance; | ||
} | ||
} | ||
|
||
export const log = (m?: any, ...p: any[]): void => logger.log(m, ...p); | ||
export const info = (m?: any, ...p: any[]): void => logger.info(m, ...p); | ||
export const warn = (m?: any, ...p: any[]): void => logger.warn(m, ...p); | ||
export const error = (m?: any, ...p: any[]): void => logger.error(m, ...p); | ||
export const log = (m?: any, ...p: any[]): void => logger?.log(m, ...p); | ||
export const info = (m?: any, ...p: any[]): void => logger?.info(m, ...p); | ||
export const warn = (m?: any, ...p: any[]): void => logger?.warn(m, ...p); | ||
export const error = (m?: any, ...p: any[]): void => logger?.error(m, ...p); | ||
|
||
export type { Logger } from './types'; | ||
export { deferredToString } from './tostring-helper'; |
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