No entries in log file when using logging library - node.js

I do not know exactly if this is a issue resolving around forever or the libraries I tried but maybe someone can help here.
I was using console.log() through my application (express) to write logs. Now I've switched to a more feature packed logging library, namely Winston.js. Configured winston like this:
var winston = require('winston');
winston.remove(winston.transports.Console);
winston.add(winston.transports.Console, {timestamp: true});
and replaced my console.log() entries with winston.info/winston.error, you name it. After I launched my app with node app.js everything went as expected. So I tried to run it with forever, since in production I run it that way.
Now I encountered the problem that the whole log file from forever, using just forever start app.js, was empty. Not a single log entry was there.
I replaced winston with log4js, but the log file remained empty when launching it with forever.
Is there anything I am missing right now and if so where is the problem?
Sincerly,
cschaeffler

It looks like you simply kept the Console transport without adding a transport for a file, e.g.,
winston.add(winston.transports.File, { filename: 'somefile.log' });

Related

Nodejs crashing silently when trying to aceess file system with "fs"

I'm currently working on an Nodejs API server with Express. and on a specific path i had this,
const fs = require("fs/promises");
fs.mkdir(folderPath, {recursive: true})).then(console.log).catch(consolle.log);
"folderPath" is the full path of the folder that i want to create. now when the execution reaches this line the NodeJs server crashes silently without any trace, nothing in the log files, nothing in the try/catch block for the code, nothing in the Express error handler. The request to that specific route will get a 503 error, which is not from express, and i think this error originates from the server it'self after NodeJS crashes.
To make sure that the error is not caused by the parameters passed to fs.mkdir i specifically put the above lines of code into the entry file (index.js) where express app is started, and replaced "folderPath" with an actual path that i'm sure exists.
const fs = require("fs/promises");
fs.mkdir("/home/abcd/efg", {recursive: true})).then(console.log).catch(console.log);
And the NodeJS server crashes as soon as i start it, with out any trace just like i mentioned above.
The next thing i tried is running a "test.js" file with the above lines of code from the terminal as, test.js only contains those two lines of code.
node test.js
test.js is located within the same folder as index.js, and surprisingly it works. the folder get's created without no error, which leaves me confused. Why would it fail when running from index.js, but works fine when running from test.js.
what am i doing wrong here that's causing the crashes, is there a permission thing that i'm not aware of, or is it something else.
here is some detail of the server:
I'm using Node 14.7.0. One other thing is that when the sever crash occurs large size files named "core####" (eg core3424) are created in the api root folder, where index.js is. and also Number of processes and Memory usage Max out.

Make a logger for Node Js

I have a project in Node Js, which executes the project on port 3000 and I access from ngrok with my browser to said localhost port, and it executes a server on port 3001 to make requests to a Maria database db. The project is done in react and the server with express.
I want to save the application logs (errors, warnings, etc.) in a log file so that I can see them whenever I want.
My intention was to use winston, and while I have no problem on the server side (3001), when I try to adapt it to the main project, I get an error that it cannot save files (the reason that appears is that it runs from the browser, and you can't create such a file because you don't have access to the project folders)
Can anyone give me some advice? Am I wrong to use winston, and should I use another?
Greetings and thanks
I've never used winston before and I couldn't find anything online about your error. In the past I've always just used node's fs module to create a log of errors and restarts.
const fs = require('fs')
Node's File System Documentation: https://nodejs.dev/learn/the-nodejs-fs-module
Short YouTube Tutorial: https://www.youtube.com/watch?v=U57kU311-nE

MEAN Stack Application's console.log not working?

I am currently developing a MEAN Stack application.
Previously my node version is 0.12 and I was using grunt to run my application which uses nodemon (which uses server.js) to run the server and watch to monitor for changes.
it works fine all the way till i upgrade my node to v 0.4.
now when I use grunt, the console.log does not print to console at all and it only works if i use
node server.js
instead of
grunt
the image below shows the situation that i'm facing:
in an ideal situation, when running grunt, I should be able to see the message
App listening on port 8000
however after updating node, the console.log doesn't seem to be working for my grunt(?)/nodemon(?) anymore.
can anyone advise on this matter?
thank you.
You can use very useful logging module winston. Using this you can log all your errors in a text file with timestamp, Which will be very helpful for application support. You can use it's debug, error, info methods for the corresponding console.debug(), console.error(), console.info().
EX:
Var logger = require('winston')
logger.debug('Debug things here');
logger.info('put info to track');
logger.error('to track error with status code')

Viewing node console log remotely

I have been building my first node app. In testing on my mac, I was able to view the console log output using terminal.
I'm now moving the app to a server but I still want to get a live dump of the console log. Yes, I can get this by SSH'ing into the server - start the app then watch the output. But, say my SSH connection to the server gets disconnected. After re-connecting to the server, how do I go about viewing the terminal output of that process?
One solution I came across was http://console.re - this looks ideal, however it comes with warnings not to use in a production environment. Coupled with the fact that it's public, I'm hesitant to use it.
Does anyone know of an alternative solution similar to console.re?
Thanks
You could try using a custom function that writes the output to a log file, as well as printing it on screen.
Something like this: (note that this won't accept multiple arguments)
var fs = require('fs');
module.exports = function(text) {
fs.writeFile('console.log', text, {
flag: 'a' // append
}, function(){}); // ignore the response
console.log(text);
};
Perhaps screen, tmux, or similar software might work for you.

Logging configuration in sailsjs

I've started to use sailsjs not long ago and really love it ! (I'm a previous rails lover).
Anyway, I do not find the documentation on how to configure the logger transport (I'd like my log to be written in a file within the logs folder, and also to remove the console transport).
Usually, when using winston I use something like:
// Add transport file
winston.add(winston.transports.File, {
filename: some_filename
});
// Handle exceptions
winston.handleExceptions(new winston.transports.File({
filename: another_filename
}));
As sails already wraps Winston, I'm not sure how to do it in a clean way. Any idea or pointer towards an example ?

Resources