Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Winston not Logging to console in typescript

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?

like image 463
wzr1337 Avatar asked Jan 25 '17 18:01

wzr1337


People also ask

How do I use a Winston logger in typescript?

The logs will be appended, multiple options are available to specify the max size, rotate the file and zip the file. import { createLogger, transports, format } from "winston"; import { createWriteStream } from "fs"; const logger = createLogger({ transports: [ new transports. Stream({ stream: createWriteStream("hello.

What can I use instead of console log?

Instead of console. log() you can also use console.info() , console. error() and console. warn() .

Is Winston logging async?

It does use the async module, but that is not asynchronous on all accounts either, i.e.: function cb(err) { if (callback) { if (err) return callback(err); callback(null, level, msg, meta); } callback = null; if (! err) { self. emit('logged', level, msg, meta); } } async.


2 Answers

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' })
  ]
});
like image 68
idbehold Avatar answered Oct 10 '22 09:10

idbehold


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 }));

like image 21
Kapil Raghuwanshi Avatar answered Oct 10 '22 11:10

Kapil Raghuwanshi