14

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?

wzr1337
  • 3,609
  • 5
  • 30
  • 53

4 Answers4

8

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.

bguyl
  • 383
  • 3
  • 10
7

When you instantiate a new Logger instance you need to provide it a list of transports so it knows where to send the logs:

var logger = new (winston.Logger)({
  transports: [
    new (winston.transports.Console)(),
    new (winston.transports.File)({ filename: 'somefile.log' })
  ]
});
idbehold
  • 16,833
  • 5
  • 47
  • 74
5

Well,

thanks to the hint of @idbehold , I found that a plain and easy:

import * as winston from "winston";

winston.info('Now my debug messages are written to the console!');

works for the default logger..

wzr1337
  • 3,609
  • 5
  • 30
  • 53
1

Well, I am following below file as 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;

as using inside app.ts as this.app.use(morgan('combined', { stream: winston.stream }));

Kapil Raghuwanshi
  • 867
  • 1
  • 13
  • 22