Hi @Matheus_Souza_Silva ,
I’d faced the same issues when I wanted to use Google Cloud Logging.
I gave up trying to read the json file after several attempts and found a way around it.
I created a helper class that would wrap the Goggle Cloud Logging logic inside it without the need for an external json file.
Note that the below code is in TypeScript, so it might contain type definitions along as well.
Define your GoogleCloudLogging class
// GoogleCloudLogging.ts
import { Logging } from '@google-cloud/logging';
import { ApiResponse } from '@google-cloud/logging/build/src/log';
export interface GoogleServiceAccountConfig {
client_email: string;
private_key: string;
project_id: string;
}
export type AnyJson = boolean | number | string | null | JsonArray | JsonMap;
export interface JsonMap {
[key: string]: AnyJson;
}
export type JsonArray = Array<AnyJson>;
export interface GoogleCloudLoggingConfig extends GoogleServiceAccountConfig {
logName: string;
}
export enum LogSeverity {
ALERT = 'ALERT',
CRITICAL = 'CRITICAL',
DEBUG = 'DEBUG',
DEFAULT = 'DEFAULT',
EMERGENCY = 'EMERGENCY',
ERROR = 'ERROR',
INFO = 'INFO',
NOTICE = 'NOTICE',
WARNING = 'WARNING',
}
export class GoogleCloudLogging {
private logging: Logging;
constructor(private config: GoogleCloudLoggingConfig) {
this.logging = new Logging({
credentials: {
client_email: this.config.client_email,
private_key: this.config.private_key,
},
projectId: this.config.project_id,
});
}
log(jsonMap: JsonMap, severity: LogSeverity): Promise<ApiResponse> {
const log = this.logging.log(this.config.logName);
const metadata = {
resource: {
type: 'global',
},
severity,
};
const entry = log.entry(metadata, jsonMap);
return log.write(entry);
}
}
Usage
The credentials you require from GCP are client_email
, private_key
, and project_id
. You could store them in a separate secrets.ts
file and export it only to be imported wherever you want, just like any Javascript module, thereby obviating the need for a json
file.
Here’s how you’d use the class defined above.
// Step 1: Import the module
// server.ts
import {
JsonMap,
LogSeverity,
GoogleCloudLogging,
} from './GoogleCloudLogging';
// Step 2: Create an instance of GoogleCloudLogging by giving it the necessary credentials
const googleCloudLogging = new GoogleCloudLogging({
client_email: '<gcp-client-email>',
private_key: '<gcp-private-key>',
project_id: '<gcp-project-id>',
logName: '<your-app-name>'
});
// Step 3: Use the log method to send any payload of your choice. Remember to set the severity as well.
googleCloudLogging.log({
{
...payload // Your payload goes here
},
severity: LogSeverity.DEFAULT
});
Update manifest.json
Make sure to include @google-cloud/logging
as a dependency in your manifest.json.
//manifest.json
{
"platform-version": "2.2",
"dependencies": {
"@google-cloud/logging": "9.5.1"
},
... // rest of the stuff
}
Closing notes
This approach has been successfully running for several years now on several of my apps in the Freshworks Marketplace.
Remember to not commit your secrets.ts
file in your revision control system as it could lead to a security vulnerability.
Hope this helps.
Regards,
Arun Rajkumar
cc: @Saif , @satwik , @Raviraj