Skip to content
This repository was archived by the owner on Nov 17, 2024. It is now read-only.

Commit 3eebfc5

Browse files
author
Patrick Barnes
committed
feat: add LOG_LEVEL support
1 parent 6f8ab7e commit 3eebfc5

File tree

2 files changed

+20
-0
lines changed

2 files changed

+20
-0
lines changed

__tests__/logging.spec.ts

+12
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,16 @@ describe('Logging', () => {
2121
JSON.stringify({ level: 'DEFAULT', data: ['foo', 'bar'] }, null, 2),
2222
);
2323
});
24+
it('is not called because the LOG_LEVEL is set to "ERROR"', () => {
25+
process.env.LOG_LEVEL = 'ERROR';
26+
// @ts-ignore
27+
global.console = {
28+
log: jest.fn(),
29+
};
30+
Logging.log('foo', 'bar');
31+
expect(global.console.log).not.toHaveBeenCalled();
32+
process.env.LOG_LEVEL = 'DEFAULT';
33+
});
2434
});
2535
describe('error()', () => {
2636
it('logs a call as a JSON object', done => {
@@ -92,6 +102,7 @@ describe('Logging', () => {
92102
});
93103
describe('debug()', () => {
94104
it('logs a call as a JSON object', done => {
105+
process.env.LOG_LEVEL = 'DEBUG';
95106
new Logging(
96107
undefined,
97108
undefined,
@@ -105,6 +116,7 @@ describe('Logging', () => {
105116
});
106117
describe('static debug()', () => {
107118
it('logs a call as a stringified JSON object', () => {
119+
process.env.LOG_LEVEL = 'DEBUG';
108120
// @ts-ignore
109121
global.console = {
110122
info: jest.fn(),

src/logging.ts

+8
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ export class Logging {
88
readonly _log: Function;
99
readonly _error: Function;
1010
readonly _debug: Function;
11+
readonly logLevel: Level;
1112

1213
constructor(
1314
log: Function = cloudWatch(console.log),
@@ -17,6 +18,7 @@ export class Logging {
1718
this._log = log;
1819
this._error = error;
1920
this._debug = debug;
21+
this.logLevel = (process.env.LOG_LEVEL || Level.DEFAULT) as Level;
2022
}
2123

2224
static log = (...args: any[]): void => {
@@ -32,10 +34,16 @@ export class Logging {
3234
};
3335

3436
log = (...args: any[]): void => {
37+
if (this.logLevel === Level.ERROR) {
38+
return;
39+
}
3540
this._log({ level: Level.DEFAULT, data: args });
3641
};
3742

3843
debug = (...args: any[]): void => {
44+
if (this.logLevel !== Level.DEBUG) {
45+
return;
46+
}
3947
this._debug({ level: Level.DEBUG, data: args });
4048
};
4149

0 commit comments

Comments
 (0)