-
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.
feat(logger.ts): create a logger for uniform log messages
- Loading branch information
Fredrik Makila
committed
Sep 13, 2020
1 parent
fd9b005
commit 564d8f9
Showing
2 changed files
with
118 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
import * as logger from '../logger'; | ||
|
||
describe('logger', () => { | ||
beforeEach(() => { | ||
process.env.SILENT = ''; | ||
}); | ||
|
||
afterEach(() => { | ||
jest.restoreAllMocks(); | ||
}); | ||
|
||
it('should print an info message', () => { | ||
const spy = jest.spyOn(global.console, 'log').mockImplementation(); | ||
|
||
logger.info('Some message'); | ||
expect(spy).toHaveBeenCalledWith('⚡️[server INFO]: Some message'); | ||
}); | ||
|
||
it('should print a warn message', () => { | ||
const spy = jest.spyOn(global.console, 'warn').mockImplementation(); | ||
|
||
logger.warn('Some message'); | ||
expect(spy).toHaveBeenCalledWith('⚡️[server WARN]: Some message'); | ||
}); | ||
|
||
it('should print an error message', () => { | ||
const spy = jest.spyOn(global.console, 'error').mockImplementation(); | ||
|
||
logger.error('Some message'); | ||
expect(spy).toHaveBeenCalledWith('⚡️[server ERROR]: Some message'); | ||
}); | ||
|
||
it('should print a trace message', () => { | ||
const spy = jest.spyOn(global.console, 'trace').mockImplementation(); | ||
|
||
logger.trace('Some message'); | ||
expect(spy).toHaveBeenCalledWith('⚡️[server ERROR]: Some message'); | ||
}); | ||
|
||
it.each([ | ||
['log', logger.info], | ||
['warn', logger.warn], | ||
['error', logger.error], | ||
['trace', logger.trace] | ||
])('should not %s if SILENT env variable is set', (method: string, fn: () => void) => { | ||
const spy = jest.spyOn(global.console, method as 'log' | 'warn' | 'error' | 'trace'); | ||
|
||
process.env.SILENT = 'true'; | ||
fn(); | ||
expect(spy).not.toHaveBeenCalled(); | ||
}); | ||
}); |
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,66 @@ | ||
export enum Level { | ||
INFO, | ||
WARN, | ||
ERROR | ||
} | ||
|
||
abstract class Logger { | ||
private level: Level; | ||
constructor(level: Level) { | ||
this.level = level; | ||
} | ||
|
||
protected abstract logMethod(message: string): void; | ||
|
||
log(...data: unknown[]): void { | ||
if (process.env.SILENT) return; | ||
|
||
const message = `⚡️[server ${Level[this.level]}]: ${data}`; | ||
this.logMethod(message); | ||
} | ||
} | ||
|
||
class InfoLogger extends Logger { | ||
constructor() { | ||
super(Level.INFO); | ||
} | ||
|
||
logMethod(message: string): void { | ||
console.log(message); | ||
} | ||
} | ||
|
||
class WarnLogger extends Logger { | ||
constructor() { | ||
super(Level.WARN); | ||
} | ||
|
||
protected logMethod(message: string): void { | ||
console.warn(message); | ||
} | ||
} | ||
|
||
class ErrorLogger extends Logger { | ||
constructor() { | ||
super(Level.ERROR); | ||
} | ||
|
||
protected logMethod(message: string): void { | ||
console.error(message); | ||
} | ||
} | ||
|
||
class TraceLogger extends Logger { | ||
constructor() { | ||
super(Level.ERROR); | ||
} | ||
|
||
protected logMethod(message: string): void { | ||
console.trace(message); | ||
} | ||
} | ||
|
||
export const info = (...data: unknown[]): void => new InfoLogger().log(data); | ||
export const warn = (...data: unknown[]): void => new WarnLogger().log(data); | ||
export const error = (...data: unknown[]): void => new ErrorLogger().log(data); | ||
export const trace = (...data: unknown[]): void => new TraceLogger().log(data); |