moscajs / mosca

MQTT broker as a module
mosca.io
3.2k stars 513 forks source link

help wanted How to enable log4j logs for mosca server. #690

Closed sunny1974 closed 6 years ago

sunny1974 commented 6 years ago

I want know how to enable log4j logs for mosca server.

mcollina commented 6 years ago

log4j? That's a Java logging library.

sunny1974 commented 6 years ago

My question is i want to write mosca server logs like info,debug and error to a file path. How can i do it?Using bunyan or anything else?

mcollina commented 6 years ago

Mosca uses pino: http://getpino.io/#/.

You can pass all the options of tipo to the logger options when creating a Mosca instance

sunny1974 commented 6 years ago

I'm very new to nodejs and mosca may be my question is wrong:

Question: Mosca is not using bunyan?

logger, the options for Bunyan. (Example: {level: 'debug'} ) logger.childOf, the parent Bunyan logger.

Based on above lines i thought mosca used bunyan to write logs.

And can you please share me working example of logger implementation for mosca

mcollina commented 6 years ago

Mosca is currently using pino: https://github.com/mcollina/mosca/blob/master/package.json#L91.

Pino is API compatible with Bunyan.

https://github.com/mcollina/mosca/blob/master/examples/Server_With_All_Interfaces-Settings.js#L24

sunny1974 commented 6 years ago

If i use below setting, Where logs should be logged(log folder path)?

logger: { name: 'MoscaServer', level: 'debug' },

mcollina commented 6 years ago

you can pass down logger: { stream: fs.createWriteStream('dest') } to store the log in a file.

sunny1974 commented 6 years ago

Thanks @mcollina

I tried same thing but no success. log file is created with 0kb and not writing any logs. i tried with below configurations.

1) logger:{ name: "SunilServer", level: "debug",stream: fs.createWriteStream("/apps/mosca/logs/server.log") },

2) logger:{stream: fs.createWriteStream("/apps/mosca/logs/server.log") }

3) var pinoms = require('pino-multi-stream'); var streams = [ {stream: fs.createWriteStream('/apps/logs/rtmott/mosca/mosca.log')}, {level: 'debug', stream: fs.createWriteStream('/apps/mosca/logs/mosca.log')} ] var log = pinoms({streams: streams})

logger:log,

sunny1974 commented 6 years ago

@mcollina please suggest me a way to do this.

sunny1974 commented 6 years ago

@mcollina Thanks for the support

I have changed this line https://github.com/mcollina/mosca/blob/master/lib/server.js#L152 to below. So logs are writing to specified file path

this.logger = pino(this.modernOpts.logger,this.modernOpts.logger.stream);

One more question:

Is there any way to rotate log files daily?

we have to go in this way ? https://github.com/pinojs/pino/blob/master/docs/howtos.md#rotate

or can you suggest me a better way pls?

mcollina commented 6 years ago

@sunny1974 can you send a PR?