1

My app engine application deployed fine before setting this variable. Now when I deploy the application the build is looking for this local file. Can i set this as a relative path or can I remove this variable and use new logger({ projectId, relative/path/to/json }) in my logger utils?

const {Logging} = require('@google-cloud/logging');

export const gCloudLogging = new Logging({ ... make this change here });
export const logName = 'API';
export const log = gCloudLogging.log(logName);
export const resource = {
    "type": "global",
    "displayName": "...",
    "description": "..."
};

export const severity = {
    DEFAULT: 'DEFAULT', //(0) The log entry has no assigned severity level.
    DEBUG: 'DEBUG', //(100) Debug or trace information.
    INFO: 'INFO', //(200) Routine information, such as ongoing status or performance.
    NOTICE: 'NOTICE', //(300) Normal but significant events, such as start up, shut down, or a configuration change.
    WARNING: 'WARNING', //(400) Warning events might cause problems.
    ERROR: 'ERROR', //(500) Error events are likely to cause problems.
    CRITICAL: 'CRITICAL', //(600) Critical events cause more severe problems or outages.
    ALERT: 'ALERT', //(700) A person must take an action immediately.
    EMERGENCY: 'EMERGENCY', //(800) One or more systems are unusable.
};

export const writeLog = (severity: string, text: string): void => {
    const metaData = {
        resource,
        severity
    };
    const entry = log.entry(metaData, text);
    log.write(entry);
}
almcaffee
  • 112
  • 8

1 Answers1

2

You can remove the environment variable from your App Engine deployment. As you can see in this documentation Google Client libraries support Application Default Credentials (ADC).

This means that the libraries will attempt to find credentials for you automatically. The Client libraries can then use the environment variable to find the service account to be used on local environment and automatically use the App Engine Default Service Account when deployed.

Ralemos
  • 5,571
  • 2
  • 9
  • 18
  • 1
    This is correct. Additionally (and somewhat confusingly) I think it's necessary to provide the GCP Project ID to create the Logging client. This can be provided by way of an environment variable or can be obtained from the metadata service. – DazWilkin Mar 26 '21 at 16:32
  • Yes, I think I forgot to mention the why which was that I still wanted to log locally which I thought I needed the environment variable, but removing it and using the projectId and keyFilename worked for me locally and after deployment. – almcaffee Mar 27 '21 at 02:03