This repository has been archived by the owner on Jun 17, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 136
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Use log service for console messages (#221)
* Use logService for console messages * Implement a base ConsoleLog service Use this class as a default for other services that would like to output to console. This service is overriden in CLI and Desktop to use CLI's consoleLogService and electronLogService, respectively. * Use browser-process-hrtime for timing * test LogService implementations * Ignore default import of hrtime * Clean up imports. Require ConsoleLog injection Co-authored-by: Matt Gibson <mdgibson@Matts-MBP.lan>
- Loading branch information
Showing
15 changed files
with
269 additions
and
85 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,95 @@ | ||
import { ConsoleLogService } from '../../../src/services/consoleLog.service'; | ||
|
||
const originalConsole = console; | ||
let caughtMessage: any; | ||
|
||
declare var console: any; | ||
|
||
export function interceptConsole(interceptions: any): object { | ||
console = { | ||
// tslint:disable-next-line | ||
log: function () { | ||
interceptions.log = arguments; | ||
}, | ||
// tslint:disable-next-line | ||
warn: function () { | ||
interceptions.warn = arguments; | ||
}, | ||
// tslint:disable-next-line | ||
error: function () { | ||
interceptions.error = arguments; | ||
} | ||
}; | ||
return interceptions; | ||
} | ||
|
||
export function restoreConsole() { | ||
console = originalConsole; | ||
} | ||
|
||
describe('ConsoleLogService', () => { | ||
let logService: ConsoleLogService; | ||
beforeEach(() => { | ||
caughtMessage = {}; | ||
interceptConsole(caughtMessage); | ||
logService = new ConsoleLogService(true); | ||
}); | ||
|
||
afterAll(() => { | ||
restoreConsole(); | ||
}); | ||
|
||
it('filters messages below the set threshold', () => { | ||
logService = new ConsoleLogService(true, (level) => true); | ||
logService.debug('debug'); | ||
logService.info('info'); | ||
logService.warning('warning'); | ||
logService.error('error'); | ||
|
||
expect(caughtMessage).toEqual({}); | ||
}); | ||
it('only writes debug messages in dev mode', () => { | ||
logService = new ConsoleLogService(false); | ||
|
||
logService.debug('debug message'); | ||
expect(caughtMessage.log).toBeUndefined(); | ||
}); | ||
|
||
|
||
it('writes debug/info messages to console.log', () => { | ||
logService.debug('this is a debug message'); | ||
expect(caughtMessage).toEqual({ log: jasmine.arrayWithExactContents(['this is a debug message']) }); | ||
|
||
logService.info('this is an info message'); | ||
expect(caughtMessage).toEqual({ log: jasmine.arrayWithExactContents(['this is an info message']) }); | ||
}); | ||
it('writes warning messages to console.warn', () => { | ||
logService.warning('this is a warning message'); | ||
expect(caughtMessage).toEqual({ warn: jasmine.arrayWithExactContents(['this is a warning message']) }); | ||
}); | ||
it('writes error messages to console.error', () => { | ||
logService.error('this is an error message'); | ||
expect(caughtMessage).toEqual({ error: jasmine.arrayWithExactContents(['this is an error message']) }); | ||
}); | ||
|
||
it('times with output to info', async () => { | ||
logService.time(); | ||
await new Promise(r => setTimeout(r, 250)); | ||
const duration = logService.timeEnd(); | ||
expect(duration[0]).toBe(0); | ||
expect(duration[1]).toBeGreaterThan(0); | ||
expect(duration[1]).toBeLessThan(500 * 10e6); | ||
|
||
expect(caughtMessage).toEqual(jasmine.arrayContaining([])); | ||
expect(caughtMessage.log.length).toBe(1); | ||
expect(caughtMessage.log[0]).toEqual(jasmine.stringMatching(/^default: \d+\.?\d*ms$/)); | ||
}); | ||
|
||
it('filters time output', async () => { | ||
logService = new ConsoleLogService(true, (level) => true); | ||
logService.time(); | ||
logService.timeEnd(); | ||
|
||
expect(caughtMessage).toEqual({}); | ||
}); | ||
}); |
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,9 @@ | ||
import { ElectronLogService } from '../../../src/electron/services/electronLog.service'; | ||
|
||
describe('ElectronLogService', () => { | ||
it('sets dev based on electron method', () => { | ||
process.env.ELECTRON_IS_DEV = '1'; | ||
const logService = new ElectronLogService(); | ||
expect(logService).toEqual(jasmine.objectContaining({ isDev: true }) as any); | ||
}); | ||
}); |
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,41 @@ | ||
import { ConsoleLogService } from '../../../src/cli/services/consoleLog.service'; | ||
import { interceptConsole, restoreConsole } from '../../common/services/consoleLog.service.spec'; | ||
|
||
const originalConsole = console; | ||
let caughtMessage: any = {}; | ||
|
||
describe('CLI Console log service', () => { | ||
let logService: ConsoleLogService; | ||
beforeEach(() => { | ||
caughtMessage = {}; | ||
interceptConsole(caughtMessage); | ||
logService = new ConsoleLogService(true); | ||
}); | ||
|
||
afterAll(() => { | ||
restoreConsole(); | ||
}); | ||
|
||
it('should redirect all console to error if BW_RESPONSE env is true', () => { | ||
process.env.BW_RESPONSE = 'true'; | ||
|
||
logService.debug('this is a debug message'); | ||
expect(caughtMessage).toEqual({ error: jasmine.arrayWithExactContents(['this is a debug message']) }); | ||
}); | ||
|
||
it('should not redirect console to error if BW_RESPONSE != true', () => { | ||
process.env.BW_RESPONSE = 'false'; | ||
|
||
logService.debug('debug'); | ||
logService.info('info'); | ||
logService.warning('warning'); | ||
logService.error('error'); | ||
|
||
expect(caughtMessage).toEqual({ | ||
log: jasmine.arrayWithExactContents(['info']), | ||
warn: jasmine.arrayWithExactContents(['warning']), | ||
error: jasmine.arrayWithExactContents(['error']), | ||
}); | ||
|
||
}); | ||
}); |
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,53 +1,23 @@ | ||
import { LogLevelType } from '../../enums/logLevelType'; | ||
|
||
import { LogService as LogServiceAbstraction } from '../../abstractions/log.service'; | ||
import { ConsoleLogService as BaseConsoleLogService } from '../../services/consoleLog.service'; | ||
|
||
export class ConsoleLogService implements LogServiceAbstraction { | ||
constructor(private isDev: boolean, private filter: (level: LogLevelType) => boolean = null) { } | ||
|
||
debug(message: string) { | ||
if (!this.isDev) { | ||
return; | ||
} | ||
this.write(LogLevelType.Debug, message); | ||
} | ||
|
||
info(message: string) { | ||
this.write(LogLevelType.Info, message); | ||
} | ||
|
||
warning(message: string) { | ||
this.write(LogLevelType.Warning, message); | ||
} | ||
|
||
error(message: string) { | ||
this.write(LogLevelType.Error, message); | ||
export class ConsoleLogService extends BaseConsoleLogService { | ||
constructor(isDev: boolean, filter: (level: LogLevelType) => boolean = null) { | ||
super(isDev, filter); | ||
} | ||
|
||
write(level: LogLevelType, message: string) { | ||
if (this.filter != null && this.filter(level)) { | ||
return; | ||
} | ||
|
||
switch (level) { | ||
case LogLevelType.Debug: | ||
// tslint:disable-next-line | ||
console.log(message); | ||
break; | ||
case LogLevelType.Info: | ||
// tslint:disable-next-line | ||
console.log(message); | ||
break; | ||
case LogLevelType.Warning: | ||
// tslint:disable-next-line | ||
console.warn(message); | ||
break; | ||
case LogLevelType.Error: | ||
// tslint:disable-next-line | ||
console.error(message); | ||
break; | ||
default: | ||
break; | ||
if (process.env.BW_RESPONSE === 'true') { | ||
// tslint:disable-next-line | ||
console.error(message); | ||
return; | ||
} | ||
|
||
super.write(level, message); | ||
} | ||
} |
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
Oops, something went wrong.