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

[Logger] Add redact step #1610

Merged
merged 2 commits into from
Sep 6, 2023
Merged
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
44 changes: 30 additions & 14 deletions src/Utils/Logger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,26 +33,38 @@ const isDebugMode = process.env.VSCODE_DEBUG_MODE === "true";
* - message: watch out
*/
function _logStr(severity: string, tag: string, ...msgs: MsgList) {
let logStrList = [];

if (msgs.length === 0) {
// Do not print
return "";
}

for (let m of msgs) {
if (m instanceof Error) {
const err = m as Error;
logStrList.push(
`\nError was thrown:\n- name: ${err.name}\n- message: ${err.message}`
);
} else if (typeof m === "object") {
logStrList.push(`\n${m.constructor.name}: ${JSON.stringify(m)}`);
} else {
logStrList.push(`${m}`);
const flatten = (msgs: MsgList) => {
let logStrList = [];
for (let m of msgs) {
if (m instanceof Error) {
const err = m as Error;
logStrList.push(
`\nError was thrown:\n- name: ${err.name}\n- message: ${err.message}`
);
} else if (typeof m === "object") {
logStrList.push(`\n${m.constructor.name}: ${JSON.stringify(m)}`);
} else {
logStrList.push(`${m}`);
}
}
}
const msg = logStrList.join(" ");
return logStrList.join(" ");
};

const redact = (msg: string) => {
// Replace Github Personal Access Tokens with ********
const classicPAT = "ghp_[a-zA-Z0-9]+";
const findGrainedPAT = "github_pat_[a-zA-Z0-9_]+";
const regex = new RegExp(`(${classicPAT})|(${findGrainedPAT})`, "g");

return msg.replace(regex, "*********************");
};

const msg = redact(flatten(msgs));
const time = new Date().toLocaleString();

return `[${time}][${tag}][${severity}] ${msg}`;
Expand Down Expand Up @@ -116,6 +128,8 @@ export class Logger {
* @brief Print msg and a line feed character without adding '[time][tag][severity]'
* @detail When log is long and need to be splitted into many chunks, append() could be used
* after the first chunk.
*
* @todo streamify logger to format consistently (ex. redact is not applied to this function)
*/
public static appendLine(msg: string) {
Logger.checkShow();
Expand All @@ -126,6 +140,8 @@ export class Logger {
* @brief Print msg without adding '[time][tag][severity]'
* @detail When log is long and need to be splitted into many chunks, append() could be used
* after the first chunk.
*
* @todo streamify logger to format consistently (ex. redact is not applied to this function)
*/
public static append(msg: string) {
Logger.checkShow();
Expand Down