NodeJS Logger: winston.transports.DailyRotateFile is not a function - node.js

I'm writing a rest api with NodeJS and express and I'm using express-winston to log accesses and erros. But I want to separate de log daily. Like in this post
I'm trying to do so with winston.transports.DailyRotateFile. A piece of code below.
api.use(expressWinston.logger({
transports: [
new winston.transports.DailyRotateFile({
name: 'file',
datePattern: '.yyyy-MM-ddTHH',
filename: path.join(__dirname, "log-access", "log_file.log")
})
]
}));
Then I receive the error: winston.transports.DailyRotateFile is not a function
I guess I have to install another pack, since reading winston's doc I found that you can write custom transports.
Would you have information on which package I would have to install? I found some that doesn't match or was discontinued.
Thanks for any help

I had to do this so it would work:
var winston = require('winston'), expressWinston = require('express-winston');
winston.transports.DailyRotateFile = require('winston-daily-rotate-file');
I already had the right package but it wouldn't work, until I wrote the lines above.

You do not need to assign:
var winston = require('winston'), expressWinston = require('express-winston');
winston.transports.DailyRotateFile = require('winston-daily-rotate-file');
You just need to require it:
const winston = require('winston');
require('winston-daily-rotate-file');

What you're looking for is this module.
Just follow the documentation, and you're good to go.

Related

"const config = new Config()" not defined

I'm quite new to NodeJS and I try to follow an example setting up a payment solution provided by Adyen. In their example code they give me this:
const config = new Config();
config.apiKey = MY_API_KEY;
config.merchantAccount = MY_ACCOUNT;
const client = new Client({ config });
client.setEnvironment("TEST");
const checkout = new CheckoutAPI(client);
const paymentsResponse = checkout.paymentMethods({
amount: {
currency: "EUR",
value: 1000,
},
countryCode: "NL",
channel: "Web",
merchantAccount: config.merchantAccount
}).then(res => res);
However (maybe not so surprising) I get the following error:
const config = new Config();
^
ReferenceError: Config is not defined
What should Config() be here? Should I define a new class? (class Config {}?) Or am I missing something? Like something to include? Same for client, how can I call .setEnvironment if Client is a class I create?
Any help appreciated.
Turned out you are supposed to import #adyen/api-library with:
npm install --save #adyen/api-library
Source
After installing the API library, you can include the modules:
const {Client, Config, CheckoutAPI} = require('#adyen/api-library');
Just place it at the top of the file where the rest of your code lives (i.e., where you make that checkout.paymentMethods() call), and you should be good to go!

Custom Logger in SailsJS

Below is my log.js file but it only logs "Hello from colorized winston", but it's not logging all my sails.log.info in my app. What am I doing wrong. I searched everywhere and can't figure it out. All I found is this https://groups.google.com/forum/#!topic/sailsjs/67u7SqzsNJQ and it seems to confirm I'm doing correctly.
var winston = require('winston'),
Papertrail = require('winston-papertrail').Papertrail;
var logger = new winston.Logger({
transports: [
new Papertrail({
host: 'logs3.papertrailapp.com',
port: xxxxx, // my port here
colorize: true
})
]
});
logger.info('Hello from colorized winston', logger);
module.exports = {
log: {
custom: logger
}
};
Any help would greatly be appreciated.
PS. I'm jumping into a project created by someone else so it's possible they broke something. If someone can give me a lead how to debug by telling me how custom works that would also be appreciated.
Make sure you've setup the correct logging level. In your config/log.js file, set the log level to something like level: silly. This should log pretty much everything.

Handlebars.js missing helper

I'm using handlebars 2.0.0, hapijs 6.5.1 and specifying the helpersPath like this:
var Handlebars = require('handlebars');
var path = require('path');
require('handlebars-layouts')(Handlebars);
module.exports = function(plugin) {
plugin.views({
engines: {
html: Handlebars
},
path: path.join(__dirname, '../public/pages'),
layoutPath: path.join(__dirname, '../public/pages'),
helpersPath: path.join(__dirname, '../lib/helpers')
});
}
The helpersPath is correct. However, it complains saying that my helper is missing even tho it is there and named correctly.
It would seem to me that the helpers are somehow not being registered even tho it knows where they are. Any ideas?
I was having a similar issue, and I think it was related to the format of my helper file.
I was using the examples from the handlebars.js site, rather than looking at the hapijs site. Basically my helper didn't have the module.exports = function(){ ... } a la http://hapijs.com/tutorials/views#view-helpers
Hope this helps.

Express Morgan. Logging to the internal object

guys. I'd like to use express middleware logging tool 'morgan' and want to change some default behaviour.
As it's said in the documentation (https://github.com/expressjs/morgan/blob/master/README.md) one of the paramateres it takes is "stream" which defines the output. By default it outputs to the node console but we can change it to log to the specified file.
// create a write stream (in append mode)
var accessLogStream = fs.createWriteStream(__dirname + '/access.log', {flags: 'a'})
// setup the logger
app.use(morgan('combined', {stream: accessLogStream}))
I wonder if there is an opportunity to forward all the logs to the specified object on the server? I mean do something like this:
var obj = []
var foo = function(param) {obj.push(param)}
app.use(morgan('combined'), {stream: foo})
thanks in advance!

Node.js Logging

Is there any library which will help me to handle logging in my Node.Js application? All I want to do is, I want to write all logs into a File and also I need an options like rolling out the file after certain size or date.
I have incorporated log4js im trying to keep all the configuration details in one file and use only the methods in other application files for ease of maintenance. But it doesnt work as expected. Here is what I'm trying to do
var log4js = require('log4js');
log4js.clearAppenders()
log4js.loadAppender('file');
log4js.addAppender(log4js.appenders.file('test.log'), 'test');
var logger = log4js.getLogger('test');
logger.setLevel('ERROR');
var traceLogger = function (message) {
logger.trace('message');
};
var errorLogger = function (message) {
logger.trace(message);
};
exports.trace = traceLogger;
exports.error = errorLogger;
I have included this file in other files and tried
log.error ("Hello Error Message");
But it is not working. Is there anything wrong in this ?
Winston is a pretty good logging library. You can write logs out to a file using it.
Code would look something like:
var winston = require('winston');
var logger = new (winston.Logger)({
transports: [
new (winston.transports.Console)({ json: false, timestamp: true }),
new winston.transports.File({ filename: __dirname + '/debug.log', json: false })
],
exceptionHandlers: [
new (winston.transports.Console)({ json: false, timestamp: true }),
new winston.transports.File({ filename: __dirname + '/exceptions.log', json: false })
],
exitOnError: false
});
module.exports = logger;
You can then use this like:
var logger = require('./log');
logger.info('log to file');
Scribe.JS Lightweight Logger
I have looked through many loggers, and I wasn't able to find a lightweight solution - so I decided to make a simple solution that is posted on github.
Saves the file which are organized by user, date, and level
Gives you a pretty output (we all love that)
Easy-to-use HTML interface
I hope this helps you out.
Online Demo
http://bluejamesbond.github.io/Scribe.js/
Secure Web Access to Logs
Prints Pretty Text to Console Too!
Web Access
Github
https://github.com/bluejamesbond/Scribe.js
Log4js is one of the most popular logging library for nodejs application.
It supports many cool features:
Coloured console logging
Replacement of node's console.log functions (optional)
File appender, with log rolling based on file size
SMTP, GELF, hook.io, Loggly appender
Multiprocess appender (useful when you've got worker processes)
A logger for connect/express servers
Configurable log message layout/patterns
Different log levels for different log categories (make some parts
of your app log as DEBUG, others only ERRORS, etc.)
Example:
Installation: npm install log4js
Configuration (./config/log4js.json):
{"appenders": [
{
"type": "console",
"layout": {
"type": "pattern",
"pattern": "%m"
},
"category": "app"
},{
"category": "test-file-appender",
"type": "file",
"filename": "log_file.log",
"maxLogSize": 10240,
"backups": 3,
"layout": {
"type": "pattern",
"pattern": "%d{dd/MM hh:mm} %-5p %m"
}
}
],
"replaceConsole": true }
Usage:
var log4js = require( "log4js" );
log4js.configure( "./config/log4js.json" );
var logger = log4js.getLogger( "test-file-appender" );
// log4js.getLogger("app") will return logger that prints log to the console
logger.debug("Hello log4js");// store log in file
You can also use npmlog by issacs, recommended in
https://npmjs.org/doc/coding-style.html.
You can find this module here
https://github.com/isaacs/npmlog
The "logger.setLevel('ERROR');" is causing the problem. I do not understand why, but when I set it to anything other than "ALL", nothing gets printed in the file. I poked around a little bit and modified your code. It is working fine for me. I created two files.
logger.js
var log4js = require('log4js');
log4js.clearAppenders()
log4js.loadAppender('file');
log4js.addAppender(log4js.appenders.file('test.log'), 'test');
var logger = log4js.getLogger('test');
logger.setLevel('ERROR');
var getLogger = function() {
return logger;
};
exports.logger = getLogger();
logger.test.js
var logger = require('./logger.js')
var log = logger.logger;
log.error("ERROR message");
log.trace("TRACE message");
When I run "node logger.test.js", I see only "ERROR message" in test.log file. If I change the level to "TRACE" then both lines are printed on test.log.
Winston is strong choice for most of the developers. I have been using winston for long. Recently I used winston with with papertrail which takes the application logging to next level.
Here is a nice screenshot from their site.
How its useful
you can manage logs from different systems at one place. this can be very useful when you have two backend communicating and can see logs from both at on place.
Logs are live. you can see realtime logs of your production server.
Powerful search and filter
you can create alerts to send you email if it encounters specific text in log.
and you can find more http://help.papertrailapp.com/kb/how-it-works/event-viewer/
A simple configuration using winston,winston-express and winston-papertrail node modules.
import winston from 'winston';
import expressWinston from 'express-winston';
//
// Requiring `winston-papertrail` will expose
// `winston.transports.Papertrail`
//
require('winston-papertrail').Papertrail;
// create winston transport for Papertrail
var winstonPapertrail = new winston.transports.Papertrail({
host: 'logsX.papertrailapp.com',
port: XXXXX
});
app.use(expressWinston.logger({
transports: [winstonPapertrail],
meta: true, // optional: control whether you want to log the meta data about the request (default to true)
msg: "HTTP {{req.method}} {{req.url}}", // optional: customize the default logging message. E.g. "{{res.statusCode}} {{req.method}} {{res.responseTime}}ms {{req.url}}"
expressFormat: true, // Use the default Express/morgan request formatting. Enabling this will override any msg if true. Will only output colors with colorize set to true
colorize: true, // Color the text and status code, using the Express/morgan color palette (text: gray, status: default green, 3XX cyan, 4XX yellow, 5XX red).
ignoreRoute: function (req, res) { return false; } // optional: allows to skip some log messages based on request and/or response
}));
A 'nodejslogger' module can be used for simple logging. It has three levels of logging (INFO, ERROR, DEBUG)
var logger = require('nodejslogger')
logger.init({"file":"output-file", "mode":"DIE"})
D : Debug, I : Info, E : Error
logger.debug("Debug logs")
logger.info("Info logs")
logger.error("Error logs")
The module can be accessed at : https://www.npmjs.com/package/nodejslogger
Observe that errorLogger is a wrapper around logger.trace. But the level of logger is ERROR so logger.trace will not log its message to logger's appenders.
The fix is to change logger.trace to logger.error in the body of errorLogger.
Each answer is 5 6 years old, so bit outdated or depreciated. Let's talk in 2020.
simple-node-logger is simple multi-level logger for console, file, and rolling file appenders. Features include:
levels: trace, debug, info, warn, error and fatal levels (plus all and off)
flexible appender/formatters with default to HH:mm:ss.SSS LEVEL message
add appenders to send output to console, file, rolling file, etc
change log levels on the fly
domain and category columns
overridable format methods in base appender
stats that track counts of all log statements including warn, error, etc
You can easily use it in any nodejs web application:
// create a stdout console logger
const log = require('simple-node-logger').createSimpleLogger();
or
// create a stdout and file logger
const log = require('simple-node-logger').createSimpleLogger('project.log');
or
// create a custom timestamp format for log statements
const SimpleNodeLogger = require('simple-node-logger'),
opts = {
logFilePath:'mylogfile.log',
timestampFormat:'YYYY-MM-DD HH:mm:ss.SSS'
},
log = SimpleNodeLogger.createSimpleLogger( opts );
or
// create a file only file logger
const log = require('simple-node-logger').createSimpleFileLogger('project.log');
or
// create a rolling file logger based on date/time that fires process events
const opts = {
errorEventName:'error',
logDirectory:'/mylogfiles', // NOTE: folder must exist and be writable...
fileNamePattern:'roll-<DATE>.log',
dateFormat:'YYYY.MM.DD'
};
const log = require('simple-node-logger').createRollingFileLogger( opts );
Messages can be logged by
log.info('this is logged info message')
log.warn('this is logged warn message')//etc..
PLUS POINT: It can send logs to console or socket. You can also append to log levels.
This is the most effective and easy way to handle logs functionality.
Here is lightweight module for logging data with full stack trace
#grdon/logger
const logger = require('#grdon/logger')({
defaultLogDirectory : __dirname + "/logs",
})
// ...
logger(someParams, 'logfile.txt')
logger(anotherParams, 'anotherLogFile.log')

Resources