4
votes

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
It does look like you already went through this tutorial. But just to confirm, here is our documentation to setup Winston. In the event you already went through the documentation and it's still giving you errors, I would suggest opening a defect report in issue tracker with the errors(if any) you are getting. - Digil
Did you ever find the issue? For me - works no problem locally. On the GCE instance (Kubernetes engine) there are no errors but the logs aren't appearing in Logging (Global logs). cloud-platform (all api's) scope applied to instances. Service account has log writer (tried log admin) @Digil - Stan Bondi
For the life of me, I cannot find these log entries at all when executing the sample code from my desktop or from the Cloud Shell examples. They just disappear! I hope I'm looking in the right place, but I've looked everywhere in the GCP console logs, all resources and all log types. So frustrating! - BK-

2 Answers

4
votes

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.

1
votes

Perhaps not the answer, but it may help. I had issue sending local logs to Stackdriver as well and finally realized that my service account didn't have the right permissions. Specifically "Logs Writer" role.

https://cloud.google.com/iam/docs/granting-roles-to-service-accounts#granting_access_to_a_service_account_for_a_resource