Winston does not log events in productions version of Node.js app hosted on Google Cloud Compute Engine
I am using winston to log some events in my application. When I run my code from node app.js
locally, winston logs events just fine. But when I push my code to the server ( gcloud preview app deploy app.yaml
), or even run it in the production environment locally ( gcloud preview app run app.yaml
), it doesn't log any event. Locally winston creates general.log
and request.log
and edits them as needed, but in production mode it doesn't work.
Here is my logging.js code
"use strict";
var fs = require('fs');
var path = require('path');
var winston = require('winston');
var expressWinston = require('express-winston');
module.exports = function(logPath) {
// Create logging directory if necessary.
if (!fs.existsSync(logPath)) {
fs.mkdirSync(logPath);
}
/*
Logger to capture all requests and output them to the console
as well as request.log.
*/
// [START requests]
var requestLogger = expressWinston.logger({
transports: [
new winston.transports.Console({
json: false
}),
new winston.transports.File({
filename: path.join(logPath, 'request.log'),
})
],
expressFormat: true
});
// [END requests]
/*
Logger to capture any top-level errors from requests and
output them in error.log
*/
// [START errors]
var errorLogger = expressWinston.errorLogger({
transports: [
new winston.transports.Console({
json: false
}),
new winston.transports.File({
filename: path.join(logPath, 'error.log'),
})
]
});
// [END errors]
/*
General logger used for .log, .info, etc. Outputs all logs
to the console as well as general.log.
*/
// [START general]
winston.add(winston.transports.File, {
filename: path.join(logPath, 'general.log')
});
// [END general]
return {
requestLogger: requestLogger,
errorLogger: errorLogger,
error: winston.error,
warn: winston.warn,
info: winston.info,
log: winston.log,
verbose: winston.verbose,
debug: winston.debug,
silly: winston.silly
};
};
Here is an example (unimportant) event that I log for testing purposes in app.js
var logging = require('./lib/logging')(process.env.LOG_PATH || './');
app.use(logging.requestLogger);
app.use(logging.errorLogger);
logging.info("Sample test log");
Why doesn't this code work in production mode?
PS I followed the below steps to set up this code
https://cloud.google.com/nodejs/getting-started/logging-application-events
source to share
Your NodeJS is actually running in a Docker container on a managed VM from the Google Compute Engine.
You have SSH to your managed VM and connect to the container.
sudo docker ps
This should output 4 containers:
gunicorn and nginx for the proxy, Fluentd is the connector for App Engine login and finally npm start your Node app
sudo docker exec -it <ID of the container running npm start> bash
This gives you a hint in the container, you can see the log files there.
Please note that if you want to see your logs in the App Engine web console (you must put these files in the / var / log / app_engine / custom_logs / *. Log '
source to share
It's actually not that easy to create and view logs in the google cloud while I'm writing this. I want to offer you an alternative to sign up using the Google cloud. you can try using winston with papertrail.
You can use modules winston-express
and winston-papertrail
node to register your requests. Papertrail provides a nicely colorful interface to quickly analyze your logs.
Have a look at fooobar.com/questions/2184754 / ... for information on how you can customize.
source to share