【发布时间】:2020-07-09 19:07:52
【问题描述】:
我目前正在学习 Typescript 装饰器。我的第一个目标是在一定程度上将 @Slf4J 从 Project Lombok 在 Java 中所做的复制到 Typescript。想法是用例如注释/装饰一个类。 @logger 在同一类中接收 LogUtil 类型的字段 log 以便调用例如log.info().
LogUtil 类:
export class LoggerUtil {
logLevel: LogLevel;
constructor(logLevel: LogLevel) {
this.logLevel = logLevel;
}
error(className: string, message: string) {
if (this.logLevel >= LogLevel.ERROR) {
console.error(`${new Date()} [ERROR] ${className}: ${message}`);
}
}
warn(className: string, message: string) {
if (this.logLevel >= LogLevel.WARN) {
console.log(`${new Date()} [WARN] ${className}: ${message}`);
}
}
log(className: string, message: string): void {
console.log(`${new Date()} [LOG] ${className} ${message}`)
}
info(className: string, message: string): void {
if (this.logLevel >= LogLevel.INFO) {
console.log(`${new Date()} [INFO] ${className}: ${message}`)
}
}
call(className: string, message: string) {
if (this.logLevel >= LogLevel.INFO) {
console.log(`${new Date()} [CALL] ${className}.${message}`)
}
}
debug(className: string, message: string) {
if (this.logLevel >= LogLevel.DEBUG) {
console.log(`${new Date()} [DEBUG] ${className}: ${message}`)
}
}
}
LogLevel 枚举:
export enum LogLevel {
ERROR = 0,
WARN = 1,
INFO = 2,
DEBUG = 3
}
使用@logger 装饰器获取LoggerUtil 实例作为日志的示例类
@logger
export class SomeService {
exampleFunction() {
log.info("exampleFunction called")
}
}
我目前正在尝试使用类级别的装饰器来做到这一点。在这里我尝试做不同的事情:
使用 Reflect API 定义类的属性。在这里我什至不确定这是否有效。
export function logger() {
return function(target: Function) {
Reflect.defineProperty(target, "log", { value: new LoggerUtil(LogLevel.DEBUG) } )
}
}
使用类原型定义属性:
export function logger() {
return function(target: Function) {
target.prototype.log = new LoggerUtil(LogLevel.DEBUG);
}
}
在引用服务中的日志实例时,我得到“找不到名称 'log'”的每一种方法:
@logger
export class SomeService {
exampleFunction() {
log.info("exampleFunction called") // Cannot find name 'log'
}
}
我的想法可能吗?我缺少什么基本的东西吗?
非常感谢您提前提供任何反馈!
【问题讨论】:
-
谢谢!我觉得该解决方案不适用于我的情况,因为我想在同一个类中使用方法/字段而不先创建实例。因此,据我所知,我无法利用继承。
标签: javascript typescript logging decorator lombok