Skip to content

Commit

Permalink
fix: logger trace mode issues
Browse files Browse the repository at this point in the history
  • Loading branch information
noomorph committed Jan 18, 2024
1 parent 90e1037 commit 5c709ff
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 16 deletions.
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

0 comments on commit 5c709ff

Please sign in to comment.