moleculerjs / moleculer-web

:earth_africa: Official API Gateway service for Moleculer framework
http://moleculer.services/docs/moleculer-web.html
MIT License
291 stars 119 forks source link

How to add winston logger to moleculer-web service ? #149

Closed stanislav-web closed 2 years ago

stanislav-web commented 4 years ago

Hi, there! I need to implement winston logger for my web service but i dont have created any ServiceBroker . My services started directly from console launcher...

/node_modules/.bin/moleculer-runner --repl --hot --envfile .env.dev endpoints

endpoints - is directory where my services are located in and exports by default Nodejs module.exports = {}

So , my question is actual because i don't know how to implement winston logger instead of basic console. Can anyone help me ?

icebob commented 4 years ago

You should create a moleculer.config.js and put the logger config into it: https://moleculer.services/docs/0.13/logging.html#Winston

// moleculer.config.js

const winston = require("winston");
const { extend } = require("moleculer").Logger;

module.exports = {
    logger: bindings => extend(winston.createLogger({
        format: winston.format.combine(
            winston.format.label({ label: bindings }),
            winston.format.timestamp(),
            winston.format.json(),
        ),
        transports: [
            new winston.transports.Console()
        ]
    }))
};
stanislav-web commented 4 years ago

@icebob its doesn't work I added winston.transports.File but logs files does not created. `transports: [

  new winston.transports.Console(),

  new winston.transports.File({ filename: './error.log', level: 'debug' }),

  new winston.transports.File({ filename: './combined.log' })

]`

Снимок экрана 2019-10-29 в 15 17 29 Снимок экрана 2019-10-29 в 15 19 53

As you can see console working properly as before with default logger configuration LOG_LEVEL=info LOG_FORMATTER=default

icebob commented 4 years ago

Please try it again with Moleculer 0.14 and Moleculer-web 0.9