Search code examples
google-cloud-platformgoogle-app-enginegoogle-cloud-logging

App engine standard and automatic error logging question (and also regarding Error reporting)


I am doing my first App engine project using App engine standard and Nodejs. I like the experience but I am having issues right now with error logging.

It seems that App engine logs every application error automatically with "ERROR" severity.

Is there a way to prevent App Engine standard to automatically log application errors?

I also realized that the errors that I log with @google-cloud/logging Nodejs package from App Engine standard are not added to Error reporting. Only the errors automatically logged by App Engine are. Is there a way to add those logs to Error reporting?

Thank you!

Here are some code snippets if it can help you answer or discuss this topic:

throw new AppCustomError({isPublic:"public",logSeverity:"WARNING",statusCode:403,errorCode:10010,dataToLog:req.dataToLog,
            message:"GETtextsData - User is not logged in."});

class AppCustomError extends Error{ //appCustomError is a custom Error class

constructor(errorObject){
    super();
    this.message=errorObject.message;
    this.errorCode=errorObject.errorCode; 
    this.statusCode=errorObject.statusCode;
    this.status=errorObject.statusCode >= 400 && errorObject.statusCode < 500 ? "fail" : "error";
    this.logSeverity=errorObject.logSeverity;
    this.dataToLog=errorObject.dataToLog;

    if(errorObject.isPublic==="public"){
        this.isPublic=true;
    }else{
        this.isPublic=false;
    }

    Error.captureStackTrace(this,this.connector);
}}export default AppCustomError;

Where the log is created using logSync() method:

if(process.env.GAE_APPLICATION){
env="gae";
logPayload.LOGGERenv=env;
log=logging.logSync(logName); //Using logsync will use a logging agent and stdout the log. Parameters will be set automatically. This is best for serverless applications like GAE. https://cloud.google.com/nodejs/docs/reference/logging/latest#writing-to-stdout
metadata.GAE_deployment_id=process.env.GAE_DEPLOYMENT_ID,
metadata.GAE_instance_id=process.env.GAE_INSTANCE,
metadata.GAE_runtime=process.env.GAE_RUNTIME}

Solution

  • App Engine logs errors automatically: You can't prevent this, but you can control your custom error logging.

    You may try these steps:

    • Use @google-cloud/logging for structured logs: Create clear messages, severity levels, and relevant data.

    • Optional Error Reporting integration: Use @google-cloud/error-reporting to report errors explicitly for centralized management.

    //JavaScript Example:
    
    const { Reporting } = require('@google-cloud/error-reporting');
    
    const errorsClient = new Reporting({
      projectId: 'your-project-id',
      // Other client options
    });
    
    // ... (Your application logic)
    
    try {
      // Your code
    } catch (error) {
      errorsClient.reportError(error);
      throw error; // Re-throw to trigger App Engine's automatic logging
    }
    
    • Throw the error after logging: Re-throw the error to trigger App Engine's automatic logging for comprehensive coverage.

    Reference Documentation: