Trouble sending transport to remote server using Winston - node.js

I have been trying to implement centralized logging across several clustered servers. I am impressed with Winston, but have been running into a path issue. Need to know the syntax for accessing remote server (code snippet included below)
I am able to log to console, a local file and a rotating local file(s)...but when I try to send a transport over to a remote server, it gets lost in the ether.
const options = {
file: {`enter code here`
level: 'info',
filename: `${appRoot}/logs/app.log`,
handleExceptions: true,
json: true,
maxsize: 5242880,
maxFiles: 5,
colorize: true,
},
console: {
level: 'debug',
handleExceptions: true,
json: false,
colorize: true,
},
rotator:{
frequency: '1m',
filename: `${appRoot}/logs/application-%DATE%.log`,
datePattern: 'YYYY-MM-DD-HH',
zippedArchive: true,
maxSize: '20k',
maxFiles: '1d',
level: 'info'
},
remoteServer:{
level:'info',
filename: '\\\\SERVERNAME.corp.COMPANY.com\\Filestore\\PM2_logs\\SM-`API-out.log'`
},
};
const logger = winston.createLogger({
transports: [
new winston.transports.File(options.file),
new winston.transports.Console(options.console),
new winston.transports.DailyRotateFile(options.rotator),
new winston.transports.File(options.remoteServer),
],
exitOnError: false,
});
I expected a copy of the log output to go to the file at the end of the path, but no such luck as of yet. I have tried a variety of forward/backward slash syntax.

Related

Winston Logger (v2.4) writing to file even after rotation

I have a transport for console out as well as one for file out.
This works fine and creates a new log file, while renaming the old one
eg. logs/appLogs.txt and logs/appLogs1.txt
The issue i face is that it continues to write to the first file even though its above the maxsize. Winston is logging to both files, but splitting the logs between the two.
eg: appLogs1.txt
2019-02-21T18:53:04.581Z - debug: ... logs
and appLogs.txt
2019-02-21T18:53:04.538Z - debug: ... logs
I can ssh into the container and with ls -l see both files grow.
below is my winston config:
new winston.Logger({
transports: [
new winston.transports.Console({
colorize: true,
timestamp: true,
handleExceptions: true,
stderrLevels: ['error'],
humanReadableUnhandledException: true,
level: level,
label: category
}),
new winston.transports.File({
filename: 'logs/appLogs.txt',
maxsize: 5000000,
maxFiles: 20,
tailable : true,
timestamp: true,
handleExceptions: true,
humanReadableUnhandledException: true,
level: level,
label: category,
json: false
})
]
})
appLogs1.txt is now 9948153 bytes, and appLogs.txt is 882922

TypeError: winston.Logger is not a constructor with winston and morgan

I tried with Winston for logger. I used in one project their It's working well when I copy paste the code from their to the current existing project than I face an issue like TypeError: winston.Logger is not a constructor
let logger = new (winston.Logger)({
^
TypeError: winston.Logger is not a constructor
Please guide me, Why this error and what should I have to do for solving this issue.
"morgan": "^1.9.0", "winston": "^3.0.0"
Following is my code in logger.js file.
var appRoot = require('app-root-path');
var winston = require('winston');
var options = {
file: {
level: 'info',
name: 'file.info',
filename: `${appRoot}/logs/app.log`,
handleExceptions: true,
json: true,
maxsize: 5242880, // 5MB
maxFiles: 100,
colorize: true,
},
errorFile: {
level: 'error',
name: 'file.error',
filename: `${appRoot}/logs/error.log`,
handleExceptions: true,
json: true,
maxsize: 5242880, // 5MB
maxFiles: 100,
colorize: true,
},
console: {
level: 'debug',
handleExceptions: true,
json: false,
colorize: true,
},
};
// your centralized logger object
let logger = new (winston.Logger)({
transports: [
new (winston.transports.Console)(options.console),
new (winston.transports.File)(options.errorFile),
new (winston.transports.File)(options.file)
],
exitOnError: false, // do not exit on handled exceptions
});
As you mention, you are using 3.0.0, you can not not use winston.Logger, you may refer library code( https://github.com/winstonjs/winston/blob/master/lib/winston.js#L178 )
You need to make small update in your code, use winston.createLogger instead of new (winston.Logger)
// your centralized logger object
let logger = winston.createLogger({
transports: [
new (winston.transports.Console)(options.console),
new (winston.transports.File)(options.errorFile),
new (winston.transports.File)(options.file)
],
exitOnError: false, // do not exit on handled exceptions
});

How add two or more logger using different files?

I am working on node Js logging library. I am using winston for this.I created log files but I stuck when I want to create two log files.Suppose I have to create all log file which contain all logs and error log file which contain only error logs.But I stuck here:
if (!fs.existsSync(logDir)) {
fs.mkdirSync(logDir);
}
const tsFormat = () => (new Date()).toLocaleTimeString();
let logger = new(winston.Logger)({
transports: [
new(winston.transports.Console)({
level: 'debug',
timestamp: tsFormat,
handleExceptions: true,
colorize: true,
json: false
}),
new(winston.transports.File)({
name: 'all-file',
handleExceptions: true,
filename: `${logDir}/all-file.log`,
level: 'debug',
maxsize: 100000000,
json: true
}),
new(winston.transports.File)({
name: 'error-file',
handleExceptions: true,
filename: `${logDir}/error-file.log`,
level: 'error',
maxsize: 100000000,
json: false
})
]
});
I want that error log show different type of console than debug console.But by default its only taking debug one. how can I manually add console config for both files.When I try to add another transport.console it shows error.
Transport already attached: console, assign a different name
var winston = require('winston');
let consoleLogger = function(options){
return new (winston.Logger)({
transports: [
new (winston.transports.Console)(options),
]
});
}
// the CustomTransport is to prevent error log show multiple times
class CustomTransport extends winston.Transport {
constructor(options) {
super(options);
this.options = options;
this.levels = options && options.levels || [this.level];
}
log(level, msg, meta, callback) {
if(this.levels.indexOf(level) > -1){
consoleLogger(this.options)[level](msg, meta);
}
callback(null, true);
}
}
winston.transports.CustomTransport = CustomTransport;
const tsFormat = () => (new Date()).toLocaleTimeString();
var logger = new (winston.Logger)({
transports: [
// add name attribute to prevent Transport already attached error
// no error log for this transport
new (winston.transports.CustomTransport)(
{
name: 'info-console',
level: 'debug',
levels : ['debug', 'verbose', 'info', 'warn'],
timestamp: tsFormat,
handleExceptions: true,
colorize: true,
json: false
}
),
// only error log for this transport, modify the configuration as you need
new (winston.transports.CustomTransport)(
{
name: 'error-console',
level: 'error',
timestamp: tsFormat,
handleExceptions: true,
colorize: true,
json: false
}
),
new (winston.transports.File)(
{
name: 'info-file',
filename: 'info.log',
timestamp: true,
maxsize: 1024000,
level: 'info'
}),
new (winston.transports.File)(
{
name: 'error-file',
filename: 'error.txt',
timestamp: true,
maxsize: 1024000,
level: 'error'
})
]});
usage:
logger.debug('debug');
logger.error('error');

Set how many files winston daily logrotate should keep

I'm implementing logging for a node app and need some advice: I've read some stuff about winston.transports.DailyRotateFile but I still don't get, where can I configure, how much daily log files should it keep. Where can I set it? Also, where do I configure it to compress old logs?
You should use the following two options:
zippedArchive
maxFiles
Example:
new (winston.transports.DailyRotateFile)({
level: process.env.LOG_LEVEL || 'error',
name: 'log.all',
colorize: false,
timestamp: true,
json: false,
filename: `logs/famitsu-server`,
datePattern: '.yyyy-MM-dd.log',
zippedArchive: true,
maxFiles: 10,
}),

logging with Express Node.js using winston

I have my logging configured like this in my express/nodejs web application. The timestamp in the log messages are in GMT. Is there a way to get the local time instead?
var winston = require('winston');
winston.emitErrs = true;
var logger = new winston.Logger({
transports: [
new winston.transports.File({
level: 'debug',
filename: './logs/all-logs.log',
handleExceptions: true,
json: false,
maxsize: 5242880, //5MB
maxFiles: 5,
colorize: false,
timestamp:true
}),
new winston.transports.Console({
timestamp :true,
level: 'debug',
handleExceptions: true,
json: false,
colorize: true
})
],
exitOnError: false
});
module.exports = logger;
module.exports.stream = {
write: function(message, encoding){
logger.info(message);
}
};
This is a sample log out put I get
2014-11-18T18:30:33.570Z - debug: Authenticated
Have you tried with :
function myTimestamp() {
return new Date().toString();
};
var logger = new winston.Logger({
transports: [
new winston.transports.File({
level: 'debug',
filename: './logs/all-logs.log',
handleExceptions: true,
json: false,
maxsize: 5242880, //5MB
maxFiles: 5,
colorize: false,
timestamp: myTimestamp
}),
new winston.transports.Console({
timestamp :true,
level: 'debug',
handleExceptions: true,
json: false,
colorize: true
})
],
exitOnError: false
});

Resources