Node.js Winston未在typescript中登录到控制台
温斯顿把我弄糊涂了。我正在使用以下typescript代码登录到我的*.ts文件中的控制台:Node.js Winston未在typescript中登录到控制台,node.js,typescript,winston,Node.js,Typescript,Winston,温斯顿把我弄糊涂了。我正在使用以下typescript代码登录到我的*.ts文件中的控制台: import { Logger, LoggerInstance } from "winston"; const logger:LoggerInstance = new Logger(); logger.info('Now my debug messages are written to the console!'); 控制台仍然是空的。没有编译错误或其他问题 同时,以下工作也很好: const wn
import { Logger, LoggerInstance } from "winston";
const logger:LoggerInstance = new Logger();
logger.info('Now my debug messages are written to the console!');
控制台仍然是空的。没有编译错误或其他问题
同时,以下工作也很好:
const wnstn = require("winston");
wnstn.info('Finally my messages are written to the console!');
有人知道为什么会这样吗?我是否必须以不同的方式配置记录器?如何使用从第二个示例中获得的默认值?当您实例化一个新的记录器实例时,需要向其提供一个传输列表,以便它知道将日志发送到何处:
var logger = new (winston.Logger)({
transports: [
new (winston.transports.Console)(),
new (winston.transports.File)({ filename: 'somefile.log' })
]
});
那么
由于@idbehold的提示,我发现一个简单易懂的:
import * as winston from "winston";
winston.info('Now my debug messages are written to the console!');
适用于默认记录器。这是导入Winston的Typescript方式 首先,确保您已安装键入:
npm i-D@types/winston
然后,在脚本中。ts在genral中,您可以导入所有常量和类型,而无需使用以前的
winston.
。好的,我下面的文件是winston.ts
import * as appRoot from 'app-root-path';
import * as winston from 'winston';
// define the custom settings for each transport (file, console)
const options = {
file: {
level: 'info',
filename: `${appRoot}/logs/app.log`,
handleExceptions: true,
json: true,
maxsize: 5242880, // 5MB
maxFiles: 5,
colorize: false,
},
console: {
level: 'debug',
handleExceptions: true,
json: false,
colorize: true,
},
};
// instantiate a new Winston Logger with the settings defined above
const logger: winston.Logger = winston.createLogger({
transports: [
new winston.transports.File(options.file),
new winston.transports.Console(options.console)
],
exitOnError: false, // do not exit on handled exceptions
});
// create a stream object with a 'write' function that will be used by `morgan`
const myStream = {
write: (message: string) => {
// use the 'info' log level so the output will be picked up by both transports (file and console)
logger.info(message);
}
};
export default logger;
在app.ts内部使用as
this.app.use(morgan('combined',{stream:winston.stream}))代码>我无法在文件中写入,因为我认为默认值是控制台输出
import * as appRoot from 'app-root-path';
import * as winston from 'winston';
// define the custom settings for each transport (file, console)
const options = {
file: {
level: 'info',
filename: `${appRoot}/logs/app.log`,
handleExceptions: true,
json: true,
maxsize: 5242880, // 5MB
maxFiles: 5,
colorize: false,
},
console: {
level: 'debug',
handleExceptions: true,
json: false,
colorize: true,
},
};
// instantiate a new Winston Logger with the settings defined above
const logger: winston.Logger = winston.createLogger({
transports: [
new winston.transports.File(options.file),
new winston.transports.Console(options.console)
],
exitOnError: false, // do not exit on handled exceptions
});
// create a stream object with a 'write' function that will be used by `morgan`
const myStream = {
write: (message: string) => {
// use the 'info' log level so the output will be picked up by both transports (file and console)
logger.info(message);
}
};
export default logger;