Winston not Logging to console in typescript

wzr1337 picture wzr1337 · Jan 25, 2017 · Viewed 10.1k times · Source

I am confused by winston. I am using the following typescript code to log onto the console in my *.ts file:

import { Logger, LoggerInstance } from "winston";

const logger:LoggerInstance = new Logger();
logger.info('Now my debug messages are written to the console!');

the console remains empty. There are no compile errors or other issues.

At the same time the following works fine:

const wnstn = require("winston");
wnstn.info('Finally my messages are written to the console!');

Does anyone have a clue why that is the case? Do I have to configure the Logger differently? How would I use the defaults I get from the second example?

Answer

bguyl picture bguyl · May 10, 2017

This is the Typescript way to import Winston.

First, be sure you have installed the typing :
npm i -D @types/winston

Then, in your script.ts

import { Logger, transports } from 'winston';
var logger = new Logger({
  transports: [
    new transports.Console(),
    new transports.File ({ filename: 'somefile.log' })
  ]
});

In genral, you can import all constants and types without using winston.<...> before.