Configure custom winston logging transport in sails - node.js

How can I configure winston in sails to use a mongodb transport?
This seems to be on the right track, but with a different transport.
https://groups.google.com/forum/#!topic/sailsjs/67u7SqzsNJQ
Here is my present config, modeled on that:
http://pastebin.com/SNJxBNak
Sails automatically runs any functions that a config file exports, right?
The function notify() doesn't seem to be running.
I am new to sails and hating it.

I was also facing the same issue and I tried extending this one Sailsjs - Custom Logging with Winston
After extending above link, the working solution:
Create a new js file inside a config folder(code inside of this will be executed automatically by sails) and add mongodb transports as below,
var winston = require('winston');
var MongoDB = require('winston-mongodb').MongoDB;
var customLogger = new(winston.Logger)({
transports: [
new(winston.transports.MongoDB)({
db: 'mongodb://localhost:27017/test',
collection: 'logs',
level: 'debug'
})
]
});
module.exports.logging = {
colors: false, // To get clean logs without prefixes or color codings
custom: customLogger
};
And use it anywhere like
sails.config.logging.custom.debug("winston mongodb transport logging");

Related

Is it possible to use winston logging and debug module together?

I use winston logging because I use its features like different logging levels, multiple transports, etc.
But I also like debug's namespace feature. Besides, express already uses it. So is it possible to use them together, e.g. to let winston logging have namespace?
After using several different loggers I gain more insight about nodejs loggers, now I believe they are not supposed to be used together b/c they are designed for different purposes. On the other hand, morgan and winston can be used together, e.g. Node.js - logging / Use morgan and winston, morgan and debug can be used together too, Nodejs - How to use morgan with debug
But first of all, to quote from Logging in Node.js done right
Setting up proper logging in Node.js applications can be bit
overwhelming at first due to the plethora of modules available through
NPM.
That was indeed my situation when I had morgan, winston, debug together. But then I realized they are for different purposes with overlaps. So I use debugjs when I really debug an issue instead of using console.log(some said debugjs is not a logger). After I am done with it I turn that debug off.
Morgan is to log express HTTP request specifically. Better to use it in dev/prod env for different purposes.
Using different log levels for Winston in dev/prod env is probably a common practice. I can also use express-winston to log HTTP request instead of using morgan.
Winston uses a different log level to turn off some log, unlike debugjs to use namespace to turn log off so I don't think they can work together.
--- update 2021 ---
Since I first answered my own question in 2018, I was asked several times about the log level related question. I found an issue opened against debugjs confirmed what I said about debug, to quote the answer from its current maintainer
Debug isn't a general purpose logging library, it's meant to
conditionally turn on debug logs. There is no concept of log levels.
Instead, break up your namespaces into different components and enable
the ones you care about
It can be useful to start passing debug(...) calls into winston. You can override the debug.log function to achieve that.
const logger = require('./v1/lib/logger'); //my winston logger
const util = require('util');
const debug = require('debug');
//for testing and dev use the regular debug (optional)
if (process.env.NODE_ENV === 'production') {
debug.log = (...args) => logger.info(util.format(...args))
}
module.exports = debug;
When dealing with debug and winston, you may also want to override formatArgs function, since debug.log is receiving already colored arguments (depending on enviroment) and it may have unexpected results in some cases (e.g. setting env vs module loading order).
import debug from "debug";
import util from "util";
import winston from "winston";
// create winston logger
const logger = new winston.Logger({ /*...*/ });
// keep original arguments
debug.formatArgs = (args) => { /* do nothing */ };
// override logging (keep function to use `this`)
debug.log = function(...args) {
// log to winston
logger.log("info", {
// `this` is bound to debug instance
namespace: this.namespace,
// format as in `console.log`
message: util.format(...args),
// add message time
timestamp: new Date().toISOString(),
// optionally log also diff time
diff: '+' + debug.humanize(this.diff),
});
};
// enable all debug
debug.enable('*');

Node.js #google-cloud/logging-winston not working inside a GCE instance

I am using npm #google-cloud/logging-winston to send application log events to google stack-driver log sinks.
Below is my code snippet and it works perfectly fine in my local macbook. When i try to run it inside a GCE instance (ubuntu 16.10 image compute instance on google cloud),it does not send the log events to log sinks and i am not able to see it on google cloud logging dashboard. Any help here appreciated
///// code start here
const winston = require('winston');
const Logger = winston.Logger;
const Console = winston.transports.Console;
const LoggingWinston = require('#google-cloud/logging-winston');
// Instantiates a Winston Stackdriver Logging client
const loggingWinston = LoggingWinston({
projectId: 'myproject-id',
keyFilename: 'mykey.json',
level: 'info',// log at 'warn' and above ,
labels: { "env": "poc" }
,
logName: "poc-gcl.log"
});
// Create a Winston logger that streams to Stackdriver Logging
// Logs will be written to: "projects/YOUR_PROJECT_ID/logs/winston_log"
const logger = new Logger({
level: 'info', // log at 'info' and above
transports: [
// Log to the console
new Console(),
// And log to Stackdriver Logging
loggingWinston
]
});
// Writes some log entries
logger.info('Node Winston logger initialized.Transport GCL Stakdriver logging',
{ type: "poc", server: "test" });
//code ends here.
thanks in advance - jag
Had the same issue, in the end it was because I was looking at the wrong place:
When logging from outside Google Cloud Platform (such as your computer), if you haven't provided a resource to log against, the library routes logs to the 'Global' resource by default.
When doing the same from inside Google Cloud Platform, logs can be found in 'GCE VM Instance' category.
Perhaps not the answer, but it may help. I had issue sending local logs to Stackdriver as well and finally realized that my service account didn't have the right permissions. Specifically "Logs Writer" role.
https://cloud.google.com/iam/docs/granting-roles-to-service-accounts#granting_access_to_a_service_account_for_a_resource

winston-mongodb error after initializing

I'm trying to configure a basic winston logger but keep getting the same error.
All I have so far to configure it is this
var winston = require('winston');
var mongoLog = require('winston-mongodb').MongoDB;
var appSettings = require('./appSettings');
var logger = new (winston.Logger)();
logger.add(mongoLog, {
db: appSettings.database,
host: appSettings.dbConnection,
collection: appSettings.loggingCollection
}
);
This is the error I'm getting.
winston-mongodb: error initialising logger Error: invalid schema, expected mongodb
The host is the ip of a mongodb instance in azure, but that doesn't seem to be the issue because if I remove host (defaulting it to localhost according to the docs) and try to have it connect to my local mongo instance it gives the same error. It also doesn't seem to matter if I call any methods on the logger or not.
Was looking at the wrong documentation I guess?
On this page it has the db param described like this
db: The name of the database you want to log to.
So I thought I would have the db name there and would need to specify the host separately.
But on this page it has a different description.
db: MongoDB connection uri, pre-connected db object or promise object which will be resolved with pre-connected db object.
Which apparently was the correct description, the full URI being this format:
mongodb://<host>:<port>/<db>

How do I log Socket.io through Winston?

I would like to use Winston as logger for Socket.io. I have seen this issue where it says:
var io = require('socket.io').listen(8080);
io.set('logger', { debug: <log function>, info: … , error: .., warn: .. })
Unfortunately, it is not described what the log function should look like.
Some playing around and a look into the Socket.io logger documentation told me that there is no fixed set of parameters: There are log messages with one, two and three parameters. Perhaps there are even more, I don't know.
I think that this is definitely not a good practice to have an undefined number of parameters, especially if this is your interface to external components.
Anyway ... does anybody have some experience with this? Can anyone point out what to watch out for?
This seems to work fine for me
var io = require('socket.io').listen(server, {
logger: {
debug: winston.debug,
info: winston.info,
error: winston.error,
warn: winston.warn
}
});
As a bonus, by setting the logger in the same call as .listen(), you catch all the log output from Socket.IO. Note that you should be able to just pass winston instead of that object, but it isn't working for me, so that's why I've posted this solution instead.
Since socket.io v1.0 logger parameter does not longer work.
They switched to debug
You may refer to this issue on how to setup Winston with socket.io
You can simply plug in the winston instance as the logger object:
var winston = require('winston');
io.set('logger', winston);

error handling using winston not returning response

I am using winston error handling in Node.js application
var logConfig={
level:'info',
timestamp:true,
colorize:true,
handleExceptions=true
};
logger = new (winston.Logger)({
transports: [
new (winston.transports.Console)(logConfig)
]
});
}
logger.exitOnError=false;
Apart from logging I want to use the error handling feature of winston. It is an express app
I can see winston handles the error properly, prints the stacktrace and prevents the server from crashing, but the response never come back to browser. Is there a way to specify this behavior?
Thanks

Resources