我正在尝试使用Winston记录stacktrace以及错误消息。我的记录器配置了自定义格式化程序:
this.errorLogger = winston.createLogger({
levels: this.levels,
level: 'error',
transports: [
new WinstonFileRotator({
filename: '%DATE%.log',
dirname: 'logs/error',
zippedArchive: true,
maxSize: '20m',
maxFiles: '14d',
handleExceptions: true,
json: false,
format: winston.format.combine(
winston.format.timestamp({
format: 'YYYY-MM-DD HH:mm:ss'
}),
winston.format.printf(info => {
return '[${info.timestamp}] -> ${info.message}';
}),
),
})
]
});
Run Code Online (Sandbox Code Playgroud)
我将错误与stacktrace一起记录:
this.errorLogger.error('My message', ex.Stack);
Run Code Online (Sandbox Code Playgroud)
在我的日志中,有一行:
[2018-09-03 23:41:14] -> My message
Run Code Online (Sandbox Code Playgroud)
如何在自定义格式化程序中访问传递给error功能以及消息的元数据?
我正在尝试在 Nestjs 后端实现 RS256 JWT 令牌。我按照Nestjs 文档中提供的示例进行操作。
在我的模块中,我JwtModule使用我的私钥注册:
@Module({
imports: [
PassportModule.register({ defaultStrategy: 'jwt' }),
JwtModule.register({
secretOrPrivateKey: extractKey(`${process.cwd()}/keys/jwt.private.key`),
signOptions: {
expiresIn: 3600,
},
}),
],
controllers: [AuthController],
providers: [AuthService, JwtStrategy, HttpStrategy],
})
export class AuthModule {}
Run Code Online (Sandbox Code Playgroud)
我能够调用 auth/token 端点并获取令牌,但是当我尝试访问受保护的端点时,我总是收到 401。
您可以在下面找到我的定制JwtStrategy:
@Injectable()
export class JwtStrategy extends PassportStrategy(Strategy) {
constructor(private readonly authService: AuthService) {
super({
jwtFromRequest: ExtractJwt.fromAuthHeaderAsBearerToken(),
secretOrKey: extractKey(`${process.cwd()}/keys/jwt.public.key`),
});
}
async validate(payload: JwtPayload) {
console.log('JwtStrategy');
const user = await this.authService.validateUser(payload);
if (!user) {
throw …Run Code Online (Sandbox Code Playgroud) javascript ×1
jwt ×1
nestjs ×1
node.js ×1
passport-jwt ×1
passport.js ×1
typescript ×1
winston ×1