refactor: unify log format with new logger utility (#5994)

Co-authored-by: sebastienlorber <lorber.sebastien@gmail.com>
This commit is contained in:
Joshua Chen 2021-12-21 00:24:59 +08:00 committed by GitHub
parent faef753730
commit 770418f8d2
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
66 changed files with 717 additions and 650 deletions

View file

@ -221,7 +221,11 @@ describe('docsVersion', () => {
getVersionsFilePath(simpleSiteDir, DEFAULT_PLUGIN_ID),
);
expect(versions).toEqual(['1.0.0']);
expect(consoleMock).toHaveBeenCalledWith('[docs]: version 1.0.0 created!');
expect(consoleMock).toHaveBeenCalledWith(
expect.stringMatching(
/.*\[SUCCESS\].* .*\[docs\].*: version .*1\.0\.0.* created!.*/,
),
);
copyMock.mockRestore();
writeMock.mockRestore();
@ -274,7 +278,11 @@ describe('docsVersion', () => {
getVersionsFilePath(versionedSiteDir, DEFAULT_PLUGIN_ID),
);
expect(versions).toEqual(['2.0.0', '1.0.1', '1.0.0', 'withSlugs']);
expect(consoleMock).toHaveBeenCalledWith('[docs]: version 2.0.0 created!');
expect(consoleMock).toHaveBeenCalledWith(
expect.stringMatching(
/.*\[SUCCESS\].* .*\[docs\].*: version .*2\.0\.0.* created!.*/,
),
);
copyMock.mockRestore();
writeMock.mockRestore();
@ -326,7 +334,9 @@ describe('docsVersion', () => {
);
expect(versions).toEqual(['2.0.0', '1.0.0']);
expect(consoleMock).toHaveBeenCalledWith(
'[community]: version 2.0.0 created!',
expect.stringMatching(
/.*\[SUCCESS\].* .*\[community\].*: version .*2.0.0.* created!.*/,
),
);
copyMock.mockRestore();

View file

@ -29,8 +29,7 @@ describe('lastUpdate', () => {
});
test('non-existing file', async () => {
const consoleMock = jest.spyOn(console, 'error');
consoleMock.mockImplementation();
const consoleMock = jest.spyOn(console, 'error').mockImplementation();
const nonExistingFileName = '.nonExisting';
const nonExistingFilePath = path.join(
__dirname,
@ -39,8 +38,8 @@ describe('lastUpdate', () => {
);
expect(await getFileLastUpdate(nonExistingFilePath)).toBeNull();
expect(consoleMock).toHaveBeenCalledTimes(1);
expect(consoleMock.mock.calls[0][0].message).toContain(
' with exit code 128',
expect(consoleMock).toHaveBeenLastCalledWith(
expect.stringMatching(/with exit code 128/),
);
expect(await getFileLastUpdate(null)).toBeNull();
expect(await getFileLastUpdate(undefined)).toBeNull();
@ -60,7 +59,9 @@ describe('lastUpdate', () => {
const lastUpdateData = await getFileLastUpdate(existingFilePath);
expect(lastUpdateData).toBeNull();
expect(consoleMock).toHaveBeenLastCalledWith(
'Sorry, the docs plugin last update options require Git.',
expect.stringMatching(
/.*\[WARNING\].* Sorry, the docs plugin last update options require Git\..*/,
),
);
consoleMock.mockRestore();

View file

@ -15,6 +15,7 @@ import path from 'path';
import type {PathOptions, SidebarOptions} from './types';
import {loadSidebarsFile, resolveSidebarPathOption} from './sidebars';
import {DEFAULT_PLUGIN_ID} from '@docusaurus/utils';
import logger from '@docusaurus/logger';
function createVersionedSidebarFile({
siteDir,
@ -133,5 +134,5 @@ export function cliDocsVersionCommand(
fs.ensureDirSync(path.dirname(versionsJSONFile));
fs.writeFileSync(versionsJSONFile, `${JSON.stringify(versions, null, 2)}\n`);
console.log(`${pluginIdLogPrefix}: version ${version} created!`);
logger.success`name=${pluginIdLogPrefix}: version name=${version} created!`;
}

View file

@ -7,7 +7,7 @@
import path from 'path';
import fs from 'fs-extra';
import chalk from 'chalk';
import logger from '@docusaurus/logger';
import {keyBy, last} from 'lodash';
import {
aliasedSitePath,
@ -274,11 +274,7 @@ export function processDocMetadata(args: {
try {
return doProcessDocMetadata(args);
} catch (e) {
console.error(
chalk.red(
`Can't process doc metadata for doc at path "${args.docFile.filePath}" in version "${args.versionMetadata.versionName}"`,
),
);
logger.error`Can't process doc metadata for doc at path path=${args.docFile.filePath} in version name=${args.versionMetadata.versionName}`;
throw e;
}
}

View file

@ -51,7 +51,7 @@ import {
translateLoadedContent,
getLoadedContentTranslationFiles,
} from './translations';
import chalk from 'chalk';
import logger from '@docusaurus/logger';
import {getVersionTags} from './tags';
import {createVersionRoutes} from './routes';
import type {PropTagsListPage} from '@docusaurus/plugin-content-docs';
@ -203,11 +203,7 @@ export default function pluginContentDocs(
try {
return await doLoadVersion(versionMetadata);
} catch (e) {
console.error(
chalk.red(
`Loading of version failed for version "${versionMetadata.versionName}"`,
),
);
logger.error`Loading of version failed for version name=${versionMetadata.versionName}`;
throw e;
}
}

View file

@ -6,6 +6,7 @@
*/
import shell from 'shelljs';
import logger from '@docusaurus/logger';
type FileLastUpdateData = {timestamp?: number; author?: string};
@ -36,7 +37,7 @@ export async function getFileLastUpdate(
if (!shell.which('git')) {
if (!showedGitRequirementError) {
showedGitRequirementError = true;
console.warn('Sorry, the docs plugin last update options require Git.');
logger.warn('Sorry, the docs plugin last update options require Git.');
}
return null;
@ -51,8 +52,8 @@ export async function getFileLastUpdate(
);
}
return getTimestampAndAuthor(result.stdout.trim());
} catch (error) {
console.error(error);
} catch (e) {
logger.error(e);
}
return null;

View file

@ -19,7 +19,7 @@ import type {
OptionValidationContext,
ValidationResult,
} from '@docusaurus/types';
import chalk from 'chalk';
import logger from '@docusaurus/logger';
import admonitions from 'remark-admonitions';
import {DefaultSidebarItemsGenerator} from './sidebars/generator';
import {
@ -157,11 +157,7 @@ export function validateOptions({
};
}
if (options.sidebarCollapsed) {
console.warn(
chalk.yellow(
'The docs plugin config is inconsistent. It does not make sense to use sidebarCollapsible=false and sidebarCollapsed=true at the same time. sidebarCollapsed=false will be ignored.',
),
);
logger.warn`The docs plugin config is inconsistent. It does not make sense to use code=${'sidebarCollapsible: false'} and code=${'sidebarCollapsed: true'} at the same time. code=${'sidebarCollapsed: true'} will be ignored.`;
options = {
...options,
sidebarCollapsed: false,

View file

@ -14,7 +14,7 @@ import {
} from './types';
import type {PropCategoryGeneratedIndex} from '@docusaurus/plugin-content-docs';
import {toVersionMetadataProp} from './props';
import chalk from 'chalk';
import logger from '@docusaurus/logger';
export async function createCategoryGeneratedIndexRoutes({
version,
@ -163,11 +163,7 @@ export async function createVersionRoutes({
try {
return await doCreateVersionRoutes(loadedVersion);
} catch (e) {
console.error(
chalk.red(
`Can't create version routes for version "${loadedVersion.versionName}"`,
),
);
logger.error`Can't create version routes for version name=${loadedVersion.versionName}`;
throw e;
}
}

View file

@ -57,7 +57,7 @@ describe('DefaultSidebarItemsGenerator', () => {
expect(sidebarSlice).toEqual([]);
expect(consoleWarn).toHaveBeenCalledWith(
expect.stringMatching(
/No docs found in dir .: can't auto-generate a sidebar/,
/.*\[WARNING\].* No docs found in .*\..*: can't auto-generate a sidebar\..*/,
),
);
});

View file

@ -16,7 +16,7 @@ import type {
} from './types';
import {sortBy, last} from 'lodash';
import {addTrailingSlash, posixPath} from '@docusaurus/utils';
import chalk from 'chalk';
import logger from '@docusaurus/logger';
import path from 'path';
import fs from 'fs-extra';
import Yaml from 'js-yaml';
@ -72,11 +72,7 @@ async function readCategoryMetadataFile(
try {
return validateCategoryMetadataFile(unsafeContent);
} catch (e) {
console.error(
chalk.red(
`The docs sidebar category metadata file looks invalid!\nPath: ${filePath}`,
),
);
logger.error`The docs sidebar category metadata file path=${filePath} looks invalid!`;
throw e;
}
}
@ -134,11 +130,7 @@ export const DefaultSidebarItemsGenerator: SidebarItemsGenerator = async ({
const docs = allDocs.filter(isInAutogeneratedDir);
if (docs.length === 0) {
console.warn(
chalk.yellow(
`No docs found in dir ${autogenDir}: can't auto-generate a sidebar.`,
),
);
logger.warn`No docs found in path=${autogenDir}: can't auto-generate a sidebar.`;
}
return docs;
}