DynamoBooster
DynamoBooster

Reputation: 185

Winston not logging events in productions version of a Node.js app hosted on Google Cloud Compute Engine

I am using winston to log some events in my app. When I run my code with node app.js locally, winston logs the events just fine. But when I push my code to the server (gcloud preview app deploy app.yaml), or even run it in production environment locally (gcloud preview app run app.yaml) it doesn't log any event. Locally winston creates the general.log and request.log and edits them as required, but it doesn't do any of that in production mode.

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 a sample (unimportant) event I am logging 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 isn't this code working in production mode?

P.S I followed the steps here to set up this code

https://cloud.google.com/nodejs/getting-started/logging-application-events

Upvotes: 5

Views: 2372

Answers (2)

vinesh
vinesh

Reputation: 4913

It is really not easy to create and view logs in google cloud while I am writing this. I want to suggest you an alternative of logging when using google cloud. you might want to try using winston with papertrail.

You can use winston-express and winston-papertrail node modules for logging your requests. Papertrail provides nice colorful UI to analyze your logs quickly.

Look at https://stackoverflow.com/a/42173862/842386 for details about how you can configure.

Upvotes: 0

Limezest
Limezest

Reputation: 140

Your NodeJS is actually running in a Docker container on a Managed VM from Google Compute Engine.

You have to SSH to your managed VM and connect to the container.

sudo docker ps

sudo docker ps

This should output 4 containers : gunicorn and nginx are for the proxy, fluentd is the connector to log to App Engine and finally npm start is your Node app

sudo docker exec -it <ID of the container running npm start> bash

This gives you the prompt on the container, you should be able to see the log files from there.


Note that if you want to see your logs on the App Engine Web console (you should place those files in the folder '/var/log/app_engine/custom_logs/*.log'

App Engin web console

Upvotes: 1

Related Questions