使用oakLogger搭配esbuild插件,替代了之前的日志实现方式
This commit is contained in:
parent
00bdc8b896
commit
d3186240fa
48
esbuild.js
48
esbuild.js
|
|
@ -69,6 +69,49 @@ const esbuildProblemMatcherPlugin = {
|
|||
},
|
||||
};
|
||||
|
||||
/**
|
||||
* @type {import('esbuild').Plugin}
|
||||
*/
|
||||
const loggerPlugin = {
|
||||
name: 'logger-plugin',
|
||||
setup(build) {
|
||||
build.onLoad({ filter: /\.ts$/ }, async (args) => {
|
||||
if (
|
||||
['logger.ts', 'analyzeWorker.ts'].includes(
|
||||
path.basename(args.path)
|
||||
)
|
||||
) {
|
||||
return;
|
||||
}
|
||||
|
||||
const contents = await fs.readFile(args.path, 'utf8');
|
||||
const transformed = contents.replace(
|
||||
/(console\.)(log|error|warn)\(/g,
|
||||
(match, p1, p2) => {
|
||||
const method =
|
||||
p2 === 'log'
|
||||
? 'log'
|
||||
: p2 === 'error'
|
||||
? 'error'
|
||||
: 'warn';
|
||||
|
||||
console.log(
|
||||
`transform: ${path.basename(
|
||||
args.path
|
||||
)} ${match} to oakLogger.${method}(`
|
||||
);
|
||||
|
||||
return `oakLogger.${method}(`;
|
||||
}
|
||||
);
|
||||
|
||||
// 添加import oakLogger from './oakLogger';
|
||||
const contentsAdd = `import oakLogger from '@/utils/logger';\n${transformed}`;
|
||||
return { contents: contentsAdd, loader: 'ts' };
|
||||
});
|
||||
},
|
||||
};
|
||||
|
||||
async function main() {
|
||||
const ctx = await esbuild.context({
|
||||
entryPoints: [
|
||||
|
|
@ -94,7 +137,12 @@ async function main() {
|
|||
/* add to the end of plugins array */
|
||||
esbuildProblemMatcherPlugin,
|
||||
copyTemplatesPlugin,
|
||||
loggerPlugin,
|
||||
],
|
||||
// 路径别名
|
||||
alias: {
|
||||
'@': path.resolve(__dirname, 'src'),
|
||||
},
|
||||
});
|
||||
if (watch) {
|
||||
await ctx.watch();
|
||||
|
|
|
|||
|
|
@ -280,7 +280,7 @@ export async function activate(context: vscode.ExtensionContext) {
|
|||
return;
|
||||
}
|
||||
const projectHome = join(uri.fsPath, '..', config.projectDir);
|
||||
log('projectHome:', projectHome);
|
||||
log('log', 'projectHome:', projectHome);
|
||||
// 设置projectHome
|
||||
setProjectHome(projectHome);
|
||||
// 通知已经启用
|
||||
|
|
|
|||
|
|
@ -1,9 +1,10 @@
|
|||
import * as vscode from 'vscode';
|
||||
|
||||
// 通过createOutputChannel创建一个输出频道
|
||||
export const logger = vscode.window.createOutputChannel('oak-assistant');
|
||||
|
||||
// 输出日志
|
||||
export const log = (...message: any[]) => {
|
||||
export const log = (level: 'log' | 'warn' | 'error' = 'log', ...message: any[]) => {
|
||||
// 将日志信息输出到频道。需要判断类型
|
||||
const msg: string[] = message.map((msg) => {
|
||||
if (typeof msg === 'object') {
|
||||
|
|
@ -14,27 +15,34 @@ export const log = (...message: any[]) => {
|
|||
return msg;
|
||||
}
|
||||
});
|
||||
logger.appendLine(msg.join(' '));
|
||||
const prefix = {
|
||||
// log: '\x1b[32m[INFO] \x1b[0m', // 绿色
|
||||
// warn: '\x1b[33m[WARN] \x1b[0m', // 黄色
|
||||
// error: '\x1b[31m[ERROR] \x1b[0m' // 红色
|
||||
log: '[INFO] ',
|
||||
warn: '[WARN] ',
|
||||
error: '[ERROR] '
|
||||
}[level];
|
||||
logger.appendLine(`${prefix}${msg.join(' ')}`);
|
||||
};
|
||||
|
||||
// polyfill全局的console对象,重定向到输出频道
|
||||
const console = global.console;
|
||||
global.console = {
|
||||
...console,
|
||||
const oakLogger = {
|
||||
log: (...args: any[]) => {
|
||||
console.log(...args);
|
||||
log(...args);
|
||||
log('log', ...args);
|
||||
},
|
||||
error: (...args: any[]) => {
|
||||
console.error(...args);
|
||||
log(...args);
|
||||
log('error', ...args);
|
||||
},
|
||||
warn: (...args: any[]) => {
|
||||
console.warn(...args);
|
||||
log(...args);
|
||||
log('warn', ...args);
|
||||
},
|
||||
info: (...args: any[]) => {
|
||||
console.info(...args);
|
||||
log(...args);
|
||||
log('log', ...args);
|
||||
},
|
||||
};
|
||||
|
||||
export default oakLogger;
|
||||
|
|
@ -79,7 +79,6 @@ export const loadConfig = () => {
|
|||
const content = fs.readFileSync(path, 'utf-8');
|
||||
const config = JSON.parse(content);
|
||||
cachedConfig = deepMergeObject(defaultConfig, config);
|
||||
|
||||
console.log('load config:', cachedConfig);
|
||||
};
|
||||
|
||||
|
|
|
|||
|
|
@ -0,0 +1,10 @@
|
|||
export {}; // This ensures the file is treated as a module
|
||||
|
||||
declare global {
|
||||
interface oakLogger {
|
||||
log: (...args: any[]) => void;
|
||||
error: (...args: any[]) => void;
|
||||
warn: (...args: any[]) => void;
|
||||
info: (...args: any[]) => void;
|
||||
}
|
||||
}
|
||||
Loading…
Reference in New Issue