gpt4 book ai didi

javascript - 有没有办法在 typescript 程序中获取类的详细信息,以便我可以将这些详细信息传递给记录器(winston)?

转载 作者:行者123 更新时间:2023-11-29 15:06:57 26 4
gpt4 key购买 nike

我在 winston 上写了一个包装器。为了对此进行测试,我还编写了一个“问候语类”,我在其中调用了 logger.info()。我希望我的日志也显示类名。为此,当我调用 logger.info() 时,我还必须传递类的名称,以便我的包装器知道调用记录器的类。但问题是我不希望程序员指定那个。我想要创建一个抽象级别。我的包装器应该是用户友好的。有没有办法做到这一点?如果上述问题无法解决,是否有办法获取类及其方法的所有详细信息。我尝试传递“this”,但我得到的是 {greeting:world}对不起,如果我没有使用正确的术语。我是 javascript 和 node.js 的新手。

包装器:

import "reflect-metadata";
import { injectable } from "inversify";
import { createLogger, format, transports, Logger as Iwinston } from "winston";

const { combine, timestamp, metadata, json, errors, label, printf } = format;

@injectable()
export default class Logger {
public logger: Iwinston;

private selfInfo: string;

constructor() {
this.selfInfo = "winston version >= 3.x";
this.logger = createLogger(this.readOptions());
this.addTransportConsole(this.getTransportConsoleOptions());
}

public info(message: string, data?: any): void {
console.log(data);
this.logger.info(message,data);
}

public error(message: string, data?: any): void {
this.logger.error(message, data);
}

public getCoreVersion(): string {
return this.selfInfo;
}

private readOptions(): Object {
return {
format: combine(
label({ label: process.env.APP_NAME || "Unknown App" }),
errors({ stack: true }),
timestamp(),
json(),
),
transports:[
new transports.File({filename:'error.log',level:'info'})
]
};
}

private getTransportConsoleOptions(): transports.ConsoleTransportOptions {
return {
debugStdout: false
};
}

private addTransportConsole(
options?: transports.ConsoleTransportOptions
): void {
this.logger.add(new transports.Console(options));
}
}

我的测试:

import Logger from '../lib/component/logger/appLogger/winstonLogger'
let logger:Logger
logger = new Logger()

test('Winston Logger Test',() => {
class Greeter {
greeting: string;
constructor(message: string) {
this.greeting = message;
logger.info('hello',this)
}
greet() {
console.log(globalThis.Document)
logger.info('in greet')
return "Hello, " + this.greeting;
}
}
let greeter = new Greeter("world");
greeter.greet()
})

//{"level":"info","message":"hello","metadata":{},"label":"Unknown App","timestamp":"2019-11-22T09:30:07.256Z"}

最佳答案

所以我在这里要做的是让它们在创建记录器实例时将类作为参数传递:

const logger = new Logger<Greeter>(Greeter)

test('Winston Logger Test',() => {
class Greeter {
....

这意味着这个记录器实例现在在整个使用过程中都链接到这个类。在您的记录器类中,您可以在构造函数中设置我在 nameByInstance 方法中抽象出来的那个类的名称。

import "reflect-metadata";
import { injectable } from "inversify";
import { createLogger, format, transports, Logger as Iwinston } from "winston";

const { combine, timestamp, metadata, json, errors, label, printf } = format;

@injectable()
export default class Logger<TInstance> {
public logger: Iwinston;

private selfInfo: string;

private loggerInstanceName: string;

constructor(instance: TInstance) {
this.selfInfo = "winston version >= 3.x";
this.logger = createLogger(this.readOptions());
this.addTransportConsole(this.getTransportConsoleOptions());
this.loggerInstanceName = this.nameByInstance(instance);

console.log(this.loggerInstanceName) // Greeter :)
}

public info(message: string, data?: any): void {
console.log(data);
this.logger.info(message,data);
}

public error(message: string, data?: any): void {
this.logger.error(message, data);
}

public getCoreVersion(): string {
return this.selfInfo;
}

private readOptions(): Object {
return {
format: combine(
label({ label: process.env.APP_NAME || "Unknown App" }),
errors({ stack: true }),
timestamp(),
json(),
),
transports:[
new transports.File({filename:'error.log',level:'info'})
]
};
}

private getTransportConsoleOptions(): transports.ConsoleTransportOptions {
return {
debugStdout: false
};
}

private addTransportConsole(
options?: transports.ConsoleTransportOptions
): void {
this.logger.add(new transports.Console(options));
}

private nameByInstance(type: TInstance): string {
return type.prototype["constructor"]["name"];
}
}

现在这意味着您可以让他们执行 logger.info('hello') 并且您在记录器实例 loggerInstanceName 中具有类名的上下文以添加它到他们在 loggerinfo 方法中传递的消息,而他们不必这样做..很好嘿?!

关于javascript - 有没有办法在 typescript 程序中获取类的详细信息,以便我可以将这些详细信息传递给记录器(winston)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59016773/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com