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

fix: logger trace mode issues #15

Merged
merged 1 commit into from
Jan 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
35 changes: 22 additions & 13 deletions plugin.mjs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { readFile, writeFile } from 'node:fs/promises';
import { sep, join, relative, resolve } from 'node:path';
import importFrom from 'import-from';
import { logger, optimizeTracing } from "./utils/logger.mjs";
import { logger, optimizedLogger, optimizeTracing } from "./utils/logger.mjs";
import { convertPathToImport } from "./utils/resolve-module.mjs";
import { isBuiltinReporter } from "./utils/is-builtin-reporter.mjs";
import { mapSourceToOutputFiles } from "./utils/map-inputs-outputs.mjs";
Expand All @@ -11,20 +11,27 @@ import { JEST_DEPENDENCIES } from "./utils/jest-dependencies.mjs";

const passThrough = (filePath, fileContents) => fileContents;

const __CONTENT = optimizeTracing((log, content, message) => log.trace({ content }, message));
const __PROCESS = optimizeTracing((log, before, after, message) => {
const __CONTENT = optimizeTracing((log, content, message) => log.trace(
typeof content === 'object'
? content
: { content },
message
));

const __DIFF = optimizeTracing((log, before, after, message) => {
if (before !== after) {
log.trace({content: after}, message);
__CONTENT(log, after, message);
}
});

const __READ = (log, content) => __CONTENT(log, content, 'read file');
const __PREPROCESS = (log, before, after) => __PROCESS(log, before, after, 'pre-process file');
const __PREPROCESS = (log, before, after) => __DIFF(log, before, after, 'pre-process file');
const __TRANSFORM = (log, content) => __CONTENT(log, content, 'transform file');
const __POSTPROCESS = (log, before, after) => __PROCESS(log, before, after, 'post-process file');
const __FILE_MAPPING = (log, content) => __CONTENT(log, content, 'create file mapping');
const __JEST_CONFIG = (log, content) => __CONTENT(log, content, 'create jest config');
const __PACKAGE_JSON = (log, content) => __CONTENT(log, content, 'create package.json');
const __POSTPROCESS = (log, before, after) => __DIFF(log, before, after, 'post-process file');
const __FILE_MAPPING_CREATING = (log, input) => __CONTENT(log, input, 'creating file mapping');
const __FILE_MAPPING_CREATED = (log, input) => __CONTENT(log, input, 'created file mapping');
const __JEST_CONFIG = (log, config) => __CONTENT(log, config, 'create jest config');
const __PACKAGE_JSON = (log, packageJson) => __CONTENT(log, packageJson, 'create package.json');

export default ({
package: packageMiddleware,
Expand All @@ -45,7 +52,7 @@ export default ({
const transformer = await createScriptTransformer(projectConfig);

build.onLoad({ filter: /.*/ }, async (args) => {
const log = logger.child({ tid: ['jest-transform', args.path] });
const log = optimizedLogger.child({ tid: ['jest-transform', args.path] });

return log.trace.complete(relative(rootDir, args.path), async () => {
const fileContent = await readFile(args.path, 'utf8');
Expand All @@ -66,14 +73,16 @@ export default ({
});

build.onEnd(async (result) => {
const mapping = mapSourceToOutputFiles({
const mappingInput = {
rootDir,
outdir,
sourceFiles: Object.keys(result.metafile.inputs),
outputFiles: Object.keys(result.metafile.outputs),
});
};

__FILE_MAPPING(logger, mapping);
__FILE_MAPPING_CREATING(logger, mappingInput);
const mapping = mapSourceToOutputFiles(mappingInput);
__FILE_MAPPING_CREATED(logger, mapping);

/**
* @param {string} file
Expand Down
9 changes: 6 additions & 3 deletions utils/logger.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import * as fs from 'node:fs';
import * as path from 'node:path';
import {
bunyamin,
nobunyamin,
isDebug,
threadGroups,
traceEventStream,
Expand All @@ -19,13 +20,15 @@ threadGroups.add({
threadGroups.add({
id: 'jest-transform',
displayName: 'Jest Transform',
maxConcurrency: 100500,
});

bunyamin.useLogger(createBunyanImpl(), 1);

export const logger = bunyamin.child({
cat: PACKAGE_NAME,
});
export const logger = bunyamin.child({ tid: PACKAGE_NAME });
export const optimizedLogger = isDebug(PACKAGE_NAME)
? logger
: nobunyamin;

const noop = () => {};

Expand Down