Skip to content

Commit

Permalink
Merge pull request #449 from Zack921/master
Browse files Browse the repository at this point in the history
feat: add cleanLog in tswconfig.js to hide default logs
  • Loading branch information
Zack921 committed Jun 2, 2021
2 parents 0d64d8a + 8e4d573 commit 037b090
Show file tree
Hide file tree
Showing 8 changed files with 97 additions and 45 deletions.
101 changes: 62 additions & 39 deletions benchmark/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion benchmark/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,6 @@
"xl_close_port": "^1.0.1"
},
"dependencies": {
"@tswjs/open-platform-plugin": "^1.1.2"
"@tswjs/open-platform-plugin": "^1.3.0"
}
}
3 changes: 2 additions & 1 deletion benchmark/start/tswconfig.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,5 +26,6 @@ module.exports = {
winstonTransports: [
new winston.transports.File({ filename: "error.log", level: "error" }),
new winston.transports.File({ filename: "debug.log", level: "debug" })
]
],
cleanLog: true
};
12 changes: 12 additions & 0 deletions lib/core/logger/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ type LogLevelStrings = keyof typeof LOG_LEVEL;
type WinstonLogLevel = keyof typeof winstonConfig.syslog.levels;

export class Logger {
private isCleanLog = false;

public logLevel: number

public winstonLogger: WinstonLogger
Expand All @@ -45,7 +47,16 @@ export class Logger {
return this.logLevel;
}

public setCleanLog(isCleanLog: boolean): void {
this.isCleanLog = isCleanLog;
}

public getCleanLog(): boolean {
return this.isCleanLog;
}

public debug(str: string): void {
if (this.isCleanLog) return;
if (!currentContext()) {
console.debug(Logger.formatStr(str, "DEBUG", {
levelLimit: this.logLevel
Expand All @@ -56,6 +67,7 @@ export class Logger {
}

public info(str: string): void {
if (this.isCleanLog) return;
if (!currentContext()) {
console.info(Logger.formatStr(str, "INFO", {
levelLimit: this.logLevel
Expand Down
15 changes: 12 additions & 3 deletions lib/core/runtime/capture/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,12 @@ export const hack = <T extends typeof http.request>(
address || "null"}. Cost ${timestamps.dnsTime}ms`);

if (err) {
logger.error(`${logPre} Lookup error ${err.stack}`);
if (logger.getCleanLog()) {
logger.error(`${logPre} Request: ${JSON.stringify(requestLog)}`);
}

logger.error(`${logPre} Lookup ${host} -> ${
address || "null"}, error ${err.stack}`);
}
});
}
Expand All @@ -160,6 +165,10 @@ export const hack = <T extends typeof http.request>(
});

request.once("error", (error: Error) => {
if (logger.getCleanLog()) {
logger.error(`${logPre} Request: ${JSON.stringify(requestLog)}`);
}

logger.error(`${logPre} Request error. Stack: ${error.stack}`);
finishRequest();
clearDomain();
Expand Down Expand Up @@ -211,7 +220,7 @@ export const hack = <T extends typeof http.request>(
response.statusCode
}. Cost: ${
timestamps.onResponse.getTime()
- timestamps.onSocket.getTime()
- timestamps.onSocket.getTime()
} ms`);

// responseInfo can't retrieve data until response "end" event
Expand Down Expand Up @@ -252,7 +261,7 @@ export const hack = <T extends typeof http.request>(
requestLog.responseLength
}. Cost: ${
timestamps.responseClose.getTime()
- timestamps.onSocket.getTime()
- timestamps.onSocket.getTime()
} ms`);

finishRequest();
Expand Down
4 changes: 3 additions & 1 deletion lib/core/runtime/dns.hack.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,9 @@ export const dnsHack = (): void => {
logger.debug(`dns lookup [${cost}ms]: ${hostname} > ${address}`);
eventBus.emit(EVENT_LIST.DNS_LOOKUP_SUCCESS, address);
} else {
logger.error(`dns lookup [${cost}ms] error: ${err.stack}`);
logger.error(`dns lookup [${cost}ms]: ${hostname} > ${address},
error: ${err.stack}`);

eventBus.emit(EVENT_LIST.DNS_LOOKUP_ERROR, err);
}

Expand Down
4 changes: 4 additions & 0 deletions lib/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { dnsHack, dnsRestore } from "./core/runtime/dns.hack";
import { requestHack, requestRestore } from "./core/runtime/capture/index";
import { winstonHack, winstonRestore } from "./core/winston";
import { eventBus } from "./core/bus";
import logger from "./core/logger";

export const installHacks = (): void => {
httpCreateServerHack();
Expand All @@ -32,6 +33,9 @@ export default async (
): Promise<void> => {
const configAbsolutePath = path.resolve(basePath, configPath);
global.tswConfig = await import(configAbsolutePath);

logger.setCleanLog(global.tswConfig && global.tswConfig.cleanLog);

// eslint-disable-next-line no-restricted-syntax
for (const plugin of global.tswConfig.plugins) {
try {
Expand Down
1 change: 1 addition & 0 deletions typings/type.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ declare namespace NodeJS {
tswConfig: {
plugins: any[];
winstonTransports: any[];
cleanLog?: boolean;
};
}
}

0 comments on commit 037b090

Please sign in to comment.