-
-
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.
setup Logger class which can help with automate logging in different …
…environments and assist in debugging (#53)
- Loading branch information
1 parent
ec2dfa7
commit cd26533
Showing
6 changed files
with
90 additions
and
4 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
import { Logger } from './logger'; | ||
|
||
describe('Logger', () => { | ||
let originalEnv: string | undefined; | ||
|
||
const consoleSpy = vi.spyOn(console, 'log'); | ||
consoleSpy.mockImplementation(() => { | ||
// no-op, prevent logging when running tests | ||
}); | ||
|
||
beforeEach(() => { | ||
originalEnv = process.env.APP_ENV; | ||
Logger.disable(); | ||
}); | ||
|
||
afterEach(() => { | ||
process.env.APP_ENV = originalEnv; | ||
consoleSpy.mockReset(); | ||
}); | ||
|
||
test('should log message when enabled', () => { | ||
Logger.enable(); | ||
Logger.log('Test message'); | ||
expect(consoleSpy).toHaveBeenCalledWith('[FIRE]:', 'Test message'); | ||
}); | ||
|
||
test('should not log message when disabled', () => { | ||
Logger.log('Test message'); | ||
expect(consoleSpy).not.toHaveBeenCalled(); | ||
}); | ||
|
||
test('should enable logging', () => { | ||
Logger.enable(); | ||
expect(Logger['isEnabled']).toBe(true); | ||
}); | ||
|
||
test('should disable logging', () => { | ||
Logger.enable(); | ||
Logger.disable(); | ||
expect(Logger['isEnabled']).toBe(false); | ||
}); | ||
|
||
test('should log message if APP_ENV is development', () => { | ||
process.env.APP_ENV = 'development'; | ||
Logger.reset(); | ||
Logger.log('Test message'); | ||
expect(consoleSpy).toHaveBeenCalledWith('[FIRE]:', 'Test message'); | ||
}); | ||
|
||
test('should not log message if APP_ENV is not development', () => { | ||
process.env.APP_ENV = 'production'; | ||
Logger.reset(); | ||
Logger.log('Test message'); | ||
expect(consoleSpy).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,21 @@ | ||
export class Logger { | ||
private static isEnabled: boolean = process.env.APP_ENV === 'development'; | ||
|
||
static log(message?: any, ...optionalParams: any[]): void { | ||
if (this.isEnabled) { | ||
console.log(`[FIRE]:`, message, ...optionalParams); | ||
} | ||
} | ||
|
||
static enable(): void { | ||
this.isEnabled = true; | ||
} | ||
|
||
static disable(): void { | ||
this.isEnabled = false; | ||
} | ||
|
||
static reset(): void { | ||
this.isEnabled = process.env.APP_ENV === 'development'; | ||
} | ||
} |
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