Node.js @google-cloud/logging-winston not working inside a GCE instance

Viewed 2116

I am using npm @google-cloud/logging-winston to send application log events to google stack-driver log sinks. Below is my code snippet and it works perfectly fine in my local macbook. When i try to run it inside a GCE instance (ubuntu 16.10 image compute instance on google cloud),it does not send the log events to log sinks and i am not able to see it on google cloud logging dashboard. Any help here appreciated

        ///// code start here
            const winston = require('winston');
        const Logger = winston.Logger;
        const Console = winston.transports.Console;

        const LoggingWinston = require('@google-cloud/logging-winston');

        // Instantiates a Winston Stackdriver Logging client
        const loggingWinston = LoggingWinston({
        projectId: 'myproject-id',
        keyFilename: 'mykey.json',
        level: 'info',// log at 'warn' and above ,


        labels: { "env": "poc" }
        ,
        logName: "poc-gcl.log"
        });

        // Create a Winston logger that streams to Stackdriver Logging
        // Logs will be written to: "projects/YOUR_PROJECT_ID/logs/winston_log"
        const logger = new Logger({
        level: 'info', // log at 'info' and above
        transports: [
            // Log to the console
            new Console(),
            // And log to Stackdriver Logging
            loggingWinston
        ]



        });

        // Writes some log entries
        logger.info('Node Winston logger initialized.Transport GCL Stakdriver logging', 
        { type: "poc", server: "test" });


        //code ends here.

thanks in advance - jag

2 Answers

Had the same issue, in the end it was because I was looking at the wrong place:

  • When logging from outside Google Cloud Platform (such as your computer), if you haven't provided a resource to log against, the library routes logs to the 'Global' resource by default.

  • When doing the same from inside Google Cloud Platform, logs can be found in 'GCE VM Instance' category.

Related