From 5a92740221961fc9a33b2633bf316e19975f675b Mon Sep 17 00:00:00 2001 From: Anton Kosykh Date: Wed, 24 May 2023 21:04:17 +0300 Subject: [PATCH 1/9] Add whitelist mode --- src/index.ts | 24 +++++++++++------------- 1 file changed, 11 insertions(+), 13 deletions(-) diff --git a/src/index.ts b/src/index.ts index c8b4024..1753a31 100644 --- a/src/index.ts +++ b/src/index.ts @@ -18,9 +18,11 @@ import { getNode, getName, locToString } from './lib'; const ignored = new Set(); const forceLog = new Set(); const fxRunning = new Map(); +let mode = 'blacklist'; // default mode is blacklist -export function attachLogger(config: { scope?: Scope; name?: string } = {}): () => void { +export function attachLogger(config: { scope?: Scope; name?: string, mode?: 'blacklist' | 'whitelist' } = {}): () => void { const name = config.name || (config.scope ? `scope: ${getNode(config.scope).id}` : ''); + mode = config.mode || 'blacklist'; const logDeclarations = createDeclarationsLogger({ name, @@ -30,16 +32,12 @@ export function attachLogger(config: { scope?: Scope; name?: string } = {}): () const uninspect = inspect({ scope: config.scope || undefined, fn: (m) => { - if ( - /** - * Log only non-derived units by default - */ - (isLoggable(m) && !ignored.has(m.id)) || - /** - * Log any units if they are forced to be logged - */ - forceLog.has(m.id) - ) { + const shouldLogBlacklist = isLoggable(m) && !ignored.has(m.id); + const shouldLogWhitelist = forceLog.has(m.id); + + const shouldLog = mode === 'blacklist' ? shouldLogBlacklist : shouldLogWhitelist; + + if (shouldLog) { log(m, name); } }, @@ -65,7 +63,7 @@ export function configure( ): void { const units = Array.isArray(unitOrUnits) ? unitOrUnits : [unitOrUnits]; - if (config.log === 'disabled') { + if (config.log === 'disabled' && mode === 'blacklist') { units.forEach((unit) => { ignored.add(getNode(unit).id); @@ -74,7 +72,7 @@ export function configure( } }); } - if (config.log === 'enabled') { + if (config.log === 'enabled' && mode === 'whitelist') { units.forEach((unit) => { forceLog.add(getNode(unit).id); From c1245e3dec2eb3454fa456840d575e2a3b4add1e Mon Sep 17 00:00:00 2001 From: Anton Kosykh Date: Wed, 24 May 2023 21:09:03 +0300 Subject: [PATCH 2/9] Update index.ts --- src/index.ts | 25 ++++++++++++++++--------- 1 file changed, 16 insertions(+), 9 deletions(-) diff --git a/src/index.ts b/src/index.ts index 1753a31..a29d26c 100644 --- a/src/index.ts +++ b/src/index.ts @@ -20,9 +20,11 @@ const forceLog = new Set(); const fxRunning = new Map(); let mode = 'blacklist'; // default mode is blacklist -export function attachLogger(config: { scope?: Scope; name?: string, mode?: 'blacklist' | 'whitelist' } = {}): () => void { +export function attachLogger(config: { scope?: Scope; name?: string; mode?: 'blacklist' | 'whitelist' } = {}): () => void { + // update mode if provided + mode = config.mode || mode; + const name = config.name || (config.scope ? `scope: ${getNode(config.scope).id}` : ''); - mode = config.mode || 'blacklist'; const logDeclarations = createDeclarationsLogger({ name, @@ -32,13 +34,14 @@ export function attachLogger(config: { scope?: Scope; name?: string, mode?: 'bla const uninspect = inspect({ scope: config.scope || undefined, fn: (m) => { - const shouldLogBlacklist = isLoggable(m) && !ignored.has(m.id); - const shouldLogWhitelist = forceLog.has(m.id); - - const shouldLog = mode === 'blacklist' ? shouldLogBlacklist : shouldLogWhitelist; - - if (shouldLog) { - log(m, name); + if (mode === 'blacklist') { + if ((isLoggable(m) && !ignored.has(m.id)) || forceLog.has(m.id)) { + log(m, name); + } + } else if (mode === 'whitelist') { + if (forceLog.has(m.id)) { + log(m, name); + } } }, }); @@ -66,18 +69,22 @@ export function configure( if (config.log === 'disabled' && mode === 'blacklist') { units.forEach((unit) => { ignored.add(getNode(unit).id); + forceLog.delete(getNode(unit).id); if (is.effect(unit)) { ignored.add(getNode(unit.finally).id); + forceLog.delete(getNode(unit.finally).id); } }); } if (config.log === 'enabled' && mode === 'whitelist') { units.forEach((unit) => { forceLog.add(getNode(unit).id); + ignored.delete(getNode(unit).id); if (is.effect(unit)) { forceLog.add(getNode(unit.finally).id); + ignored.delete(getNode(unit.finally).id); } }); } From 16cd78730ce5403f64891c9b91fad5019c8252f3 Mon Sep 17 00:00:00 2001 From: Anton Kosykh Date: Wed, 24 May 2023 21:10:29 +0300 Subject: [PATCH 3/9] Update index.ts --- src/index.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/index.ts b/src/index.ts index a29d26c..18b8628 100644 --- a/src/index.ts +++ b/src/index.ts @@ -66,7 +66,7 @@ export function configure( ): void { const units = Array.isArray(unitOrUnits) ? unitOrUnits : [unitOrUnits]; - if (config.log === 'disabled' && mode === 'blacklist') { + if (config.log === 'disabled') { units.forEach((unit) => { ignored.add(getNode(unit).id); forceLog.delete(getNode(unit).id); @@ -77,7 +77,7 @@ export function configure( } }); } - if (config.log === 'enabled' && mode === 'whitelist') { + if (config.log === 'enabled') { units.forEach((unit) => { forceLog.add(getNode(unit).id); ignored.delete(getNode(unit).id); From d75fab257df4178ce70f30674b9763bd182a1508 Mon Sep 17 00:00:00 2001 From: Anton Kosykh Date: Wed, 24 May 2023 21:14:05 +0300 Subject: [PATCH 4/9] Update basic.test.ts --- src/basic.test.ts | 99 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 99 insertions(+) diff --git a/src/basic.test.ts b/src/basic.test.ts index 4cfc0bc..3797dae 100644 --- a/src/basic.test.ts +++ b/src/basic.test.ts @@ -509,4 +509,103 @@ describe('logger tests', () => { unlogger(); }); }); + + test('configure can log whitelisted entities', () => { + const $abc = createStore(0); + const inc = createEvent(); + $abc.on(inc, (state) => state + 1); + const myFx = createEffect(() => 42); + sample({ + clock: inc, + target: myFx, + }); + + const derivedInc = inc.map((x) => 'LOGGED!!!!'); + + const logged = vi.fn(); + + vi.stubGlobal('console', { + log: logged, + warn: logged, + error: logged, + info: logged, + group: logged, + groupEnd: logged, + groupCollapsed: logged, + }); + + // Whitelist 'inc' logs + configure(inc, { log: 'enabled' }); + + const unlogger = attachLogger({ mode: 'whitelist' }); + + inc(); + + // Only inc event should be logged + expect(logged.mock.calls.length).toBe(3); // initial logs of logger installation + single update of derivedInc + expect(logged.mock.calls).toMatchInlineSnapshot(` + [ + [ + "%c%s%c  %c%s%c  %c%s%c  %c%o  %c%s  %c%s", + "padding-left: 4px; padding-right: 4px; font-weight: normal; line-height:1.5; color: #000; font-family: \\"Apple Emoji Font\\"; font-weight: normal !important;", + "☄️", + "padding-left: 4px; padding-right: 4px; font-weight: normal; line-height:1.5; color: #000; font-family: \\"Apple Emoji Font\\"; font-weight: normal !important;", + "padding-left: 4px; padding-right: 4px; font-weight: normal; font-family: Menlo, monospace;", + "effector", + "padding-left: 4px; padding-right: 4px; font-weight: normal; font-family: Menlo, monospace;", + "padding-left: 4px; padding-right: 4px; font-weight: normal; background-color: #9ccc65; color: #000", + "55 → *", + "color: currentColor; background-color: transparent;", + "padding-left: 4px; padding-right: 4px; font-weight: normal; padding-left: 4px;", + "LOGGED!!!!", + "padding-left: 4px; padding-right: 4px; font-weight: normal; color: #9e9e9e; padding-left: 20px;", + "", + "padding-left: 4px; padding-right: 4px; font-weight: normal; color: #9e9e9e; padding-left: 20px;", + "55 → *", + ], + [ + "%c%s%c  %c%s%c  %c%s%c  %c%s  %c%o  %c%s  %c%s", + "padding-left: 4px; padding-right: 4px; font-weight: normal; line-height:1.5; color: #000; font-family: \\"Apple Emoji Font\\"; font-weight: normal !important;", + "☄️", + "padding-left: 4px; padding-right: 4px; font-weight: normal; line-height:1.5; color: #000; font-family: \\"Apple Emoji Font\\"; font-weight: normal !important;", + "padding-left: 4px; padding-right: 4px; font-weight: normal; font-family: Menlo, monospace;", + "effector", + "padding-left: 4px; padding-right: 4px; font-weight: normal; font-family: Menlo, monospace;", + "padding-left: 4px; padding-right: 4px; font-weight: normal; background-color: #7e57c2; color: #fff", + "56.inFlight", + "color: currentColor; background-color: transparent;", + "padding-left: 4px; padding-right: 4px; font-weight: normal; ", + "-> ", + "padding-left: 4px; padding-right: 4px; font-weight: normal; ", + 1, + "padding-left: 4px; padding-right: 4px; font-weight: normal; color: #9e9e9e; padding-left: 20px;", + "", + "padding-left: 4px; padding-right: 4px; font-weight: normal; color: #9e9e9e; padding-left: 20px;", + "56.inFlight", + ], + [ + "%c%s%c  %c%s%c  %c%s%c  %c%s  %c%o  %c%s  %c%s", + "padding-left: 4px; padding-right: 4px; font-weight: normal; line-height:1.5; color: #000; font-family: \\"Apple Emoji Font\\"; font-weight: normal !important;", + "☄️", + "padding-left: 4px; padding-right: 4px; font-weight: normal; line-height:1.5; color: #000; font-family: \\"Apple Emoji Font\\"; font-weight: normal !important;", + "padding-left: 4px; padding-right: 4px; font-weight: normal; font-family: Menlo, monospace;", + "effector", + "padding-left: 4px; padding-right: 4px; font-weight: normal; font-family: Menlo, monospace;", + "padding-left: 4px; padding-right: 4px; font-weight: normal; background-color: #7e57c2; color: #fff", + "56.inFlight", + "color: currentColor; background-color: transparent;", + "padding-left: 4px; padding-right: 4px; font-weight: normal; ", + "-> ", + "padding-left: 4px; padding-right: 4px; font-weight: normal; ", + 0, + "padding-left: 4px; padding-right: 4px; font-weight: normal; color: #9e9e9e; padding-left: 20px;", + "", + "padding-left: 4px; padding-right: 4px; font-weight: normal; color: #9e9e9e; padding-left: 20px;", + "56.inFlight", + ], + ] + `); + + unlogger(); + }); }); From b4d5ff1f402ca975c638e694ad70b31a01926ccd Mon Sep 17 00:00:00 2001 From: Anton Kosykh Date: Wed, 24 May 2023 21:18:54 +0300 Subject: [PATCH 5/9] refactor: remove global --- src/index.ts | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/src/index.ts b/src/index.ts index 18b8628..bfe108f 100644 --- a/src/index.ts +++ b/src/index.ts @@ -18,11 +18,13 @@ import { getNode, getName, locToString } from './lib'; const ignored = new Set(); const forceLog = new Set(); const fxRunning = new Map(); -let mode = 'blacklist'; // default mode is blacklist +let defaultMode = 'blacklist'; -export function attachLogger(config: { scope?: Scope; name?: string; mode?: 'blacklist' | 'whitelist' } = {}): () => void { +export function attachLogger( + config: { scope?: Scope; name?: string; mode?: 'blacklist' | 'whitelist' } = {}, +): () => void { // update mode if provided - mode = config.mode || mode; + const mode = config.mode || defaultMode; const name = config.name || (config.scope ? `scope: ${getNode(config.scope).id}` : ''); From 08a93d44b3f3dae5ea3ab04623ab7eeee978b414 Mon Sep 17 00:00:00 2001 From: Anton Kosykh Date: Wed, 24 May 2023 21:19:06 +0300 Subject: [PATCH 6/9] test: new snapshot --- src/basic.test.ts | 48 ++++------------------------------------------- 1 file changed, 4 insertions(+), 44 deletions(-) diff --git a/src/basic.test.ts b/src/basic.test.ts index 3797dae..51f5ec8 100644 --- a/src/basic.test.ts +++ b/src/basic.test.ts @@ -542,7 +542,7 @@ describe('logger tests', () => { inc(); // Only inc event should be logged - expect(logged.mock.calls.length).toBe(3); // initial logs of logger installation + single update of derivedInc + expect(logged.mock.calls.length).toBe(1); // single update of derivedInc expect(logged.mock.calls).toMatchInlineSnapshot(` [ [ @@ -554,54 +554,14 @@ describe('logger tests', () => { "effector", "padding-left: 4px; padding-right: 4px; font-weight: normal; font-family: Menlo, monospace;", "padding-left: 4px; padding-right: 4px; font-weight: normal; background-color: #9ccc65; color: #000", - "55 → *", + "73", "color: currentColor; background-color: transparent;", "padding-left: 4px; padding-right: 4px; font-weight: normal; padding-left: 4px;", - "LOGGED!!!!", - "padding-left: 4px; padding-right: 4px; font-weight: normal; color: #9e9e9e; padding-left: 20px;", - "", - "padding-left: 4px; padding-right: 4px; font-weight: normal; color: #9e9e9e; padding-left: 20px;", - "55 → *", - ], - [ - "%c%s%c  %c%s%c  %c%s%c  %c%s  %c%o  %c%s  %c%s", - "padding-left: 4px; padding-right: 4px; font-weight: normal; line-height:1.5; color: #000; font-family: \\"Apple Emoji Font\\"; font-weight: normal !important;", - "☄️", - "padding-left: 4px; padding-right: 4px; font-weight: normal; line-height:1.5; color: #000; font-family: \\"Apple Emoji Font\\"; font-weight: normal !important;", - "padding-left: 4px; padding-right: 4px; font-weight: normal; font-family: Menlo, monospace;", - "effector", - "padding-left: 4px; padding-right: 4px; font-weight: normal; font-family: Menlo, monospace;", - "padding-left: 4px; padding-right: 4px; font-weight: normal; background-color: #7e57c2; color: #fff", - "56.inFlight", - "color: currentColor; background-color: transparent;", - "padding-left: 4px; padding-right: 4px; font-weight: normal; ", - "-> ", - "padding-left: 4px; padding-right: 4px; font-weight: normal; ", - 1, - "padding-left: 4px; padding-right: 4px; font-weight: normal; color: #9e9e9e; padding-left: 20px;", - "", - "padding-left: 4px; padding-right: 4px; font-weight: normal; color: #9e9e9e; padding-left: 20px;", - "56.inFlight", - ], - [ - "%c%s%c  %c%s%c  %c%s%c  %c%s  %c%o  %c%s  %c%s", - "padding-left: 4px; padding-right: 4px; font-weight: normal; line-height:1.5; color: #000; font-family: \\"Apple Emoji Font\\"; font-weight: normal !important;", - "☄️", - "padding-left: 4px; padding-right: 4px; font-weight: normal; line-height:1.5; color: #000; font-family: \\"Apple Emoji Font\\"; font-weight: normal !important;", - "padding-left: 4px; padding-right: 4px; font-weight: normal; font-family: Menlo, monospace;", - "effector", - "padding-left: 4px; padding-right: 4px; font-weight: normal; font-family: Menlo, monospace;", - "padding-left: 4px; padding-right: 4px; font-weight: normal; background-color: #7e57c2; color: #fff", - "56.inFlight", - "color: currentColor; background-color: transparent;", - "padding-left: 4px; padding-right: 4px; font-weight: normal; ", - "-> ", - "padding-left: 4px; padding-right: 4px; font-weight: normal; ", - 0, + undefined, "padding-left: 4px; padding-right: 4px; font-weight: normal; color: #9e9e9e; padding-left: 20px;", "", "padding-left: 4px; padding-right: 4px; font-weight: normal; color: #9e9e9e; padding-left: 20px;", - "56.inFlight", + "73", ], ] `); From 8e01eccbf596388ab66d41b8b9c44568bce7212e Mon Sep 17 00:00:00 2001 From: Anton Kosykh Date: Wed, 24 May 2023 21:21:36 +0300 Subject: [PATCH 7/9] Update README.md --- README.md | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/README.md b/README.md index d18aaac..d7cd458 100755 --- a/README.md +++ b/README.md @@ -155,6 +155,23 @@ configure(mappedMounted, { log: 'enabled' }); configure([$data, loadDataFx], { log: 'enabled' }); ``` +### Whitelist mode + +If you wan't to disable logs by default and only log necessary units, you can use `mode` option of `createLogger` + +```ts +import { createEvent } from 'effector'; +import { configure } from 'effector-logger'; + +// In whitelist mode, units don't log by default +attachLogger({ mode: 'whitelist' }) + +const pageMounted = createEvent(); + +// Enable logging for a specific unit +configure(pageMounted, { log: 'enabled' }); +``` + ## Redux DevTools support Redux DevTools is moved to a different package. From f3d3bcc2de168a0f23905022de4cb6167bdb7259 Mon Sep 17 00:00:00 2001 From: Anton Kosykh Date: Wed, 24 May 2023 21:22:52 +0300 Subject: [PATCH 8/9] Update README.md --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index d7cd458..23aa149 100755 --- a/README.md +++ b/README.md @@ -161,7 +161,7 @@ If you wan't to disable logs by default and only log necessary units, you can us ```ts import { createEvent } from 'effector'; -import { configure } from 'effector-logger'; +import { configure, attachLogger } from 'effector-logger'; // In whitelist mode, units don't log by default attachLogger({ mode: 'whitelist' }) From 4d2e013062c4fc219cbe77d8ce6ddf6b685ba856 Mon Sep 17 00:00:00 2001 From: Anton Kosykh Date: Thu, 25 May 2023 16:25:00 +0300 Subject: [PATCH 9/9] Update README.md Co-authored-by: Sova <5620073+sergeysova@users.noreply.github.com> --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 23aa149..c2cc2f6 100755 --- a/README.md +++ b/README.md @@ -157,7 +157,7 @@ configure([$data, loadDataFx], { log: 'enabled' }); ### Whitelist mode -If you wan't to disable logs by default and only log necessary units, you can use `mode` option of `createLogger` +If you wan't to disable logs by default and only log necessary units, you can use `mode` option of `attachLogger` ```ts import { createEvent } from 'effector';