mirror of
https://github.com/denoland/std.git
synced 2024-11-21 12:40:03 +00:00
fix(log): remove leaky Logger.prototype.asString()
method (#6082)
This commit is contained in:
parent
a9a427a709
commit
bfbc53e2ca
@ -224,6 +224,31 @@ export interface LoggerOptions {
|
||||
handlers?: BaseHandler[];
|
||||
}
|
||||
|
||||
function asString(data: unknown, isProperty = false): string {
|
||||
if (typeof data === "string") {
|
||||
if (isProperty) return `"${data}"`;
|
||||
return data;
|
||||
} else if (
|
||||
data === null ||
|
||||
typeof data === "number" ||
|
||||
typeof data === "bigint" ||
|
||||
typeof data === "boolean" ||
|
||||
typeof data === "undefined" ||
|
||||
typeof data === "symbol"
|
||||
) {
|
||||
return String(data);
|
||||
} else if (data instanceof Error) {
|
||||
return data.stack!;
|
||||
} else if (typeof data === "object") {
|
||||
return `{${
|
||||
Object.entries(data)
|
||||
.map(([k, v]) => `"${k}":${asString(v, true)}`)
|
||||
.join(",")
|
||||
}}`;
|
||||
}
|
||||
return "undefined";
|
||||
}
|
||||
|
||||
export class Logger {
|
||||
#level: LogLevel;
|
||||
handlers: BaseHandler[];
|
||||
@ -285,9 +310,9 @@ export class Logger {
|
||||
let logMessage: string;
|
||||
if (msg instanceof Function) {
|
||||
fnResult = msg();
|
||||
logMessage = this.asString(fnResult);
|
||||
logMessage = asString(fnResult);
|
||||
} else {
|
||||
logMessage = this.asString(msg);
|
||||
logMessage = asString(msg);
|
||||
}
|
||||
const record: LogRecord = new LogRecord({
|
||||
msg: logMessage,
|
||||
@ -303,31 +328,6 @@ export class Logger {
|
||||
return msg instanceof Function ? fnResult : msg;
|
||||
}
|
||||
|
||||
asString(data: unknown, isProperty = false): string {
|
||||
if (typeof data === "string") {
|
||||
if (isProperty) return `"${data}"`;
|
||||
return data;
|
||||
} else if (
|
||||
data === null ||
|
||||
typeof data === "number" ||
|
||||
typeof data === "bigint" ||
|
||||
typeof data === "boolean" ||
|
||||
typeof data === "undefined" ||
|
||||
typeof data === "symbol"
|
||||
) {
|
||||
return String(data);
|
||||
} else if (data instanceof Error) {
|
||||
return data.stack!;
|
||||
} else if (typeof data === "object") {
|
||||
return `{${
|
||||
Object.entries(data)
|
||||
.map(([k, v]) => `"${k}":${this.asString(v, true)}`)
|
||||
.join(",")
|
||||
}}`;
|
||||
}
|
||||
return "undefined";
|
||||
}
|
||||
|
||||
debug<T>(msg: () => T, ...args: unknown[]): T | undefined;
|
||||
debug<T>(msg: T extends GenericFunction ? never : T, ...args: unknown[]): T;
|
||||
debug<T>(
|
||||
|
Loading…
Reference in New Issue
Block a user