Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Core: Add logLevel preset property to filter logging #10370

Merged
merged 26 commits into from
Jun 2, 2020
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
b51eac3
ADD logLever preset property && filter lower lever log messages
ndelangen Apr 9, 2020
24c44fc
FIX lockfile
ndelangen Apr 10, 2020
db16289
FIX tests
ndelangen Apr 10, 2020
76392b9
Merge branch 'next' into add/loglevel
ndelangen Apr 13, 2020
cbff38f
Merge branch 'next' into add/loglevel
ndelangen Apr 13, 2020
acc2de5
FIX
ndelangen Apr 13, 2020
8cc33ad
Set correct initial value of logLevel
ndelangen Apr 13, 2020
50aeeae
Merge branch 'tech/install-optional-on-ci' into add/loglevel
ndelangen Apr 13, 2020
2a5106c
ADD puppeteer as optionalDependency
ndelangen Apr 13, 2020
13a2e29
FIX
ndelangen Apr 13, 2020
da6b230
FIX
ndelangen Apr 13, 2020
0d09d56
Merge branch 'next' into add/loglevel
ndelangen Apr 14, 2020
8c58a5e
Merge branch 'next' into add/loglevel
ndelangen Apr 14, 2020
32e34e7
Merge branch 'next' into add/loglevel
ndelangen Apr 24, 2020
b475070
Merge branch 'next' into add/loglevel
ndelangen May 1, 2020
d550600
ADD a loglevel option for dev mode in CLI
ndelangen May 14, 2020
dca26c2
Merge branch 'next' into tech/loglevel-for-dev
ndelangen May 14, 2020
457ca88
Merge branch 'next' into tech/loglevel-for-dev
ndelangen May 15, 2020
f3906aa
Merge branch 'next' into add/loglevel
ndelangen May 26, 2020
05e2cf0
FIX loglevel being a string within a string
ndelangen May 26, 2020
8b4a7e2
FIX lockfile && safety check for window
ndelangen May 26, 2020
8ab812b
Merge branch 'next' into add/loglevel
ndelangen May 29, 2020
b26e8c1
ADD docs
ndelangen May 29, 2020
4540246
Merge branch 'tech/loglevel-for-dev' into add/loglevel
ndelangen May 29, 2020
c604b26
set loglevel for node based on cli param in dev-mode
ndelangen May 29, 2020
7078a12
Use cli param to set default loglevel in preset
ndelangen May 29, 2020
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions addons/links/src/preview.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ jest.mock('global', () => ({
kind: 'kind',
})),
},
window: global,
__STORYBOOK_LOGGER: 'warn',
ndelangen marked this conversation as resolved.
Show resolved Hide resolved
__STORYBOOK_CLIENT_API__: {
raw: jest.fn(() => [
{
Expand Down
1 change: 1 addition & 0 deletions addons/links/src/react/components/link.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ jest.mock('global', () => ({
search: 'search',
},
},
window: global,
__STORYBOOK_STORY_STORE__: {
getSelection: jest.fn(() => ({ id: 1 })),
fromId: jest.fn(() => ({})),
Expand Down
2 changes: 1 addition & 1 deletion app/react/src/client/preview/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ describe('preview', () => {
() =>
({
document: undefined,
window: undefined,
window: {},
} as any)
);
const api = require('.');
Expand Down
4 changes: 3 additions & 1 deletion lib/client-logger/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,9 @@
"prepare": "node ../../scripts/prepare.js"
},
"dependencies": {
"core-js": "^3.0.1"
"core-js": "^3.0.1",
"global": "^4.3.2",
"loglevel": "^1.6.7"
},
"publishConfig": {
"access": "public"
Expand Down
3 changes: 3 additions & 0 deletions lib/client-logger/src/index.test.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
import { logger } from '.';

jest.mock('loglevel', () => global.console);

describe('client-logger', () => {
const initialConsole = { ...global.console };
beforeEach(() => {
global.console.trace = jest.fn();
global.console.debug = jest.fn();
global.console.log = jest.fn();
global.console.info = jest.fn();
Expand Down
21 changes: 15 additions & 6 deletions lib/client-logger/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,25 @@
const { console } = global;

/* tslint:disable: no-console */
import { window, LOGLEVEL } from 'global';
import console from 'loglevel';

export const logger = {
trace: (message: any, ...rest: any[]): void => console.trace(message, ...rest),
debug: (message: any, ...rest: any[]): void => console.debug(message, ...rest),
log: (message: any, ...rest: any[]): void => console.log(message, ...rest),
log: (message: any, ...rest: any[]): void => window.console.log(message, ...rest),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is pretty surprising, what's up with this?

Copy link
Member Author

@ndelangen ndelangen May 26, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

console.log has no "level" to it. it's not part of console lib.

I could decide to make log equal to info?

info: (message: any, ...rest: any[]): void => console.info(message, ...rest),
warn: (message: any, ...rest: any[]): void => console.warn(message, ...rest),
error: (message: any, ...rest: any[]): void => console.error(message, ...rest),
};

export const pretty = (type: keyof typeof logger) => (...args: string[]) => {
if (LOGLEVEL) {
console.setLevel(LOGLEVEL);
}

// eslint-disable-next-line no-underscore-dangle
window.__STORYBOOK_LOGGER = console;
ndelangen marked this conversation as resolved.
Show resolved Hide resolved

export const pretty = (type: 'trace' | 'debug' | 'info' | 'warn' | 'error') => (
...args: string[]
) => {
const argArray = [];

if (args.length) {
Expand All @@ -36,8 +45,8 @@ export const pretty = (type: keyof typeof logger) => (...args: string[]) => {
console[type].apply(console, argArray);
};

pretty.trace = pretty('trace');
pretty.debug = pretty('debug');
pretty.log = pretty('log');
pretty.info = pretty('info');
pretty.warn = pretty('warn');
pretty.error = pretty('error');
1 change: 1 addition & 0 deletions lib/client-logger/src/typings.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
declare module 'global';
2 changes: 2 additions & 0 deletions lib/core/src/server/common/common-preset.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,5 @@ export const babel = async (_, options) => {
presets.apply('babelDefault', babelConfig(options), options)
);
};

export const logLevel = () => 'info';
5 changes: 4 additions & 1 deletion lib/core/src/server/manager/manager-webpack.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import { resolvePathInStorybookCache } from '../utils/resolve-path-in-sb-cache';
const coreDirName = path.dirname(require.resolve('@storybook/core/package.json'));
const context = path.join(coreDirName, '../../node_modules');

export default ({
export default async ({
configDir,
configType,
docsMode,
Expand All @@ -32,8 +32,10 @@ export default ({
cache,
previewUrl,
versionCheck,
presets,
}) => {
const { raw, stringified } = loadEnv();
const logLevel = await presets.apply('logLevel', []);
const isProd = configType === 'PRODUCTION';
const refsTemplate = fse.readFileSync(path.join(__dirname, 'virtualModuleRef.template.js'), {
encoding: 'utf8',
Expand Down Expand Up @@ -78,6 +80,7 @@ export default ({
version,
dlls: dll ? ['./sb_dll/storybook_ui_dll.js'] : [],
globals: {
LOGLEVEL: JSON.stringify(logLevel),
VERSIONCHECK: JSON.stringify(versionCheck),
DOCS_MODE: docsMode, // global docs mode
PREVIEW_URL: previewUrl, // global preview URL
Expand Down
5 changes: 4 additions & 1 deletion lib/core/src/server/preview/iframe-webpack.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ export default async ({
presets,
}) => {
const dlls = await presets.apply('webpackDlls', []);
const logLevel = await presets.apply('logLevel', []);
ndelangen marked this conversation as resolved.
Show resolved Hide resolved
const { raw, stringified } = loadEnv({ production: true });
const babelLoader = createBabelLoader(babelOptions);
const isProd = configType === 'PRODUCTION';
Expand Down Expand Up @@ -107,7 +108,9 @@ export default async ({
files,
options,
version: packageJson.version,
globals: {},
globals: {
LOGLEVEL: JSON.stringify(logLevel),
},
headHtmlSnippet: getPreviewHeadHtml(configDir, process.env),
dlls,
bodyHtmlSnippet: getPreviewBodyHtml(configDir, process.env),
Expand Down
2 changes: 1 addition & 1 deletion yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -19631,7 +19631,7 @@ loglevel-plugin-prefix@^0.8.4:
resolved "https://registry.yarnpkg.com/loglevel-plugin-prefix/-/loglevel-plugin-prefix-0.8.4.tgz#2fe0e05f1a820317d98d8c123e634c1bd84ff644"
integrity sha512-WpG9CcFAOjz/FtNht+QJeGpvVl/cdR6P0z6OcXSkr8wFJOsV2GRj2j10JLfjuA4aYkcKCNIEqRGCyTife9R8/g==

loglevel@^1.4.1, loglevel@^1.6.4, loglevel@^1.6.6:
loglevel@^1.4.1, loglevel@^1.6.4, loglevel@^1.6.6, loglevel@^1.6.7:
version "1.6.7"
resolved "https://registry.yarnpkg.com/loglevel/-/loglevel-1.6.7.tgz#b3e034233188c68b889f5b862415306f565e2c56"
integrity sha512-cY2eLFrQSAfVPhCgH1s7JI73tMbg9YC3v3+ZHVW67sBS7UxWzNEk/ZBbSfLykBWHp33dqqtOv82gjhKEi81T/A==
Expand Down