-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
`JSON.stringify(1n)` throws a exception Restructure logging so that the stream can be overriden rather than disabled. This allows the logger to continue working so it can be tested
- Loading branch information
Showing
14 changed files
with
124 additions
and
47 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
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,7 @@ | ||
import { LogConfig } from '../log'; | ||
|
||
export const LogSpy = jest.spyOn(LogConfig.getOutputStream(), 'write').mockImplementation(); | ||
|
||
beforeEach(() => { | ||
LogSpy.mockClear(); | ||
}); |
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,5 +1,41 @@ | ||
import * as pino from 'pino'; | ||
import { Writable } from 'stream'; | ||
|
||
// Disable logging while jest is doing it's testing thing | ||
const isEnabled = process.env['JEST_WORKER_ID'] == null; | ||
export const Logger = pino({ level: 'debug', enabled: isEnabled }); | ||
let currentLog: pino.Logger; | ||
|
||
/** | ||
* Expose log type so functions that do not have direct access to pino have access to the log type | ||
*/ | ||
export type LogType = pino.Logger; | ||
|
||
/** | ||
* Encapsulate the logger so that it can be swapped out | ||
*/ | ||
export const LogConfig = { | ||
/** | ||
* Getting access to the current log stream is hard, | ||
* | ||
* Pino uses Symbols for all its internal functions, | ||
* getting access to them without knowing the logger is a pino logger is difficult | ||
* | ||
* **Used for testing** | ||
* To allow overwriting of the .write() to get access to the output logs | ||
*/ | ||
getOutputStream(): Writable { | ||
// There are no types for pino.symbols | ||
const streamSym = (pino as any).symbols.streamSym; | ||
// there is no type for pino['Symbol(stream)'] | ||
return LogConfig.get()[streamSym] as any; | ||
}, | ||
|
||
/** Get the currently configured logger */ | ||
get: (): pino.Logger => { | ||
if (currentLog == null) { | ||
currentLog = pino({ level: 'debug' }); | ||
} | ||
return currentLog; | ||
}, | ||
set: (log: pino.Logger): void => { | ||
currentLog = log; | ||
}, | ||
}; |
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
Oops, something went wrong.