Skip to content
This repository was archived by the owner on Mar 11, 2026. It is now read-only.

Commit 0cb76ea

Browse files
authored
Merge fb542b1 into b0ba942
2 parents b0ba942 + fb542b1 commit 0cb76ea

1 file changed

Lines changed: 33 additions & 27 deletions

File tree

samples/logs.js

Lines changed: 33 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -16,44 +16,50 @@
1616

1717
async function writeLogEntry(logName) {
1818
// [START logging_write_log_entry]
19-
// Imports the Google Cloud client library
2019
const {Logging} = require('@google-cloud/logging');
21-
22-
// Creates a client
2320
const logging = new Logging();
2421

2522
/**
26-
* TODO(developer): Uncomment the following line to run the code.
23+
* TODO(developer): Uncomment the following line and replace with your values.
2724
*/
28-
// const logName = 'Name of the log to write to, e.g. my-log';
29-
25+
// const logName = 'my-log';
3026
const log = logging.log(logName);
3127

32-
// Modify this resource to match a resource in your project
33-
// See
34-
// https://cloud.google.com/logging/docs/api/ref_v2beta1/rest/v2beta1/MonitoredResource
35-
const resource = {
36-
// This example targets the "global" resource for simplicity
37-
type: 'global',
28+
// A text log entry
29+
const text_entry = log.entry('Hello world!');
30+
31+
// A json log entry with additional context
32+
const metadata = {
33+
severity: 'WARNING',
34+
labels: {
35+
foo: 'bar',
36+
},
37+
// A default log resource is added for some GCP environments
38+
// This log resource can be overwritten per spec:
39+
// https://cloud.google.com/logging/docs/reference/v2/rest/v2/MonitoredResource
40+
resource: {
41+
type: 'global',
42+
},
43+
};
44+
45+
const message = {
46+
name: 'King Arthur',
47+
quest: 'Find the Holy Grail',
48+
favorite_color: 'Blue',
3849
};
3950

40-
// A text log entry
41-
const entry = log.entry({resource}, 'Hello, world!');
42-
43-
// A structured log entry
44-
const secondEntry = log.entry(
45-
{resource: resource},
46-
{
47-
name: 'King Arthur',
48-
quest: 'Find the Holy Grail',
49-
favorite_color: 'Blue',
50-
}
51-
);
51+
const json_Entry = log.entry(metadata, message);
5252

5353
async function writeLogEntry() {
54-
// Save the two log entries. You can write entries one at a time, but it is
55-
// best to write multiple entires together in a batch.
56-
await log.write([entry, secondEntry]);
54+
// Synchronously write the log entry
55+
await log.write(text_entry);
56+
57+
// Synchronously batch write the log entries
58+
await log.write([text_entry, json_Entry]);
59+
60+
// Asynchronously let the logging library dispatch logs
61+
log.write(text_entry);
62+
5763
console.log(`Wrote to ${logName}`);
5864
}
5965
writeLogEntry();

0 commit comments

Comments
 (0)