如何为Linux和Windows localhost在winston和app.module.ts中设置路径



我不可能是第一个在Windows 10上开发并希望日志文件在本地主机上的开发模式和泵送/管道到/var/logs/myservice文件夹,如果它是在集成或生产。

如何根据环境变量将window路径替换为linux路径?

在本例中,我使用winston和nestjs。

import { Module } from '@nestjs/common';
import { EventEmitterModule } from '@nestjs/event-emitter';
import { AppController } from './app.controller';
import { AppGateway } from './app.gateway';
import { AppService } from './app.service';
import { WinstonModule } from 'nest-winston';
import * as winston from 'winston';

@Module({
imports: [ 
WinstonModule.forRoot({
level: 'info',
format: winston.format.json(),
defaultMeta: { service: 'trading-signal-listener' },
transports: [
//
// - Write all logs with level `error` and below to `error.log`
// - Write all logs with level `info` and below to `combined.log`
//
new winston.transports.File({ filename: 'error.log', level: 'error' }),
new winston.transports.File({ filename: 'combined.log' }),
],
}),
],
controllers: [AppController],
providers: [AppService],
})
export class AppModule {}

感谢您的帮助。

我不太确定你的问题是什么,你不能像这里描述的那样使用@nestjs/confighttps://docs.nestjs.com/techniques/configuration并根据环境变量传递一个特定的路径给你的WinstonModule吗?

可以使用常规的process.env变量,它可以工作。

new winston.transports.File({ filename: process.env.logfile}),
new winston.transports.File({ filename: process.env.errorfile, level: 'error' }),

相关内容

最新更新