0

Write Log

by
Published Jul 17, 2024

Writes log data to the Logging service, [See the docs](https://cloud.google.com/nodejs/docs/reference/logging/latest/logging/log#_google_cloud_logging_Log_write_member_1_)

Script gcloud Verified

The script

Submitted by hugo697 Bun
Verified 692 days ago
1
import { Logging } from "@google-cloud/logging";
2

3
type Gcloud = {
4
  type: string;
5
  project_id: string;
6
  private_key_id: string;
7
  private_key: string;
8
  client_email: string;
9
  client_id: string;
10
  auth_uri: string;
11
  token_uri: string;
12
  auth_provider_x509_cert_url: string;
13
  client_x509_cert_url: string;
14
  universe_domain: string;
15
};
16

17
export async function main(
18
  resource: Gcloud,
19
  logName: string,
20
  message: string,
21
  severity?:
22
    | "DEFAULT"
23
    | "DEBUG"
24
    | "INFO"
25
    | "NOTICE"
26
    | "WARNING"
27
    | "ERROR"
28
    | "CRITICAL"
29
    | "ALERT"
30
    | "EMERGENCY"
31
) {
32
  const loggingClient = new Logging({
33
    credentials: resource,
34
    projectId: resource.project_id,
35
  });
36

37
  const log = loggingClient.log(logName);
38

39
  const metadata = {
40
    severity: severity || "DEFAULT",
41
  };
42

43
  const options = {
44
    resource: { type: "global" },
45
  };
46

47
  const entry = log.entry(metadata, message);
48
  await log.write(entry, options);
49

50
  return { success: true };
51
}
52