|
16 | 16 |
|
17 | 17 | async function writeLogEntry(logName) { |
18 | 18 | // [START logging_write_log_entry] |
19 | | - // Imports the Google Cloud client library |
20 | 19 | const {Logging} = require('@google-cloud/logging'); |
21 | | - |
22 | | - // Creates a client |
23 | 20 | const logging = new Logging(); |
24 | 21 |
|
25 | 22 | /** |
26 | | - * TODO(developer): Uncomment the following line to run the code. |
| 23 | + * TODO(developer): Uncomment the following line and replace with your values. |
27 | 24 | */ |
28 | | - // const logName = 'Name of the log to write to, e.g. my-log'; |
29 | | - |
| 25 | + // const logName = 'my-log'; |
30 | 26 | const log = logging.log(logName); |
31 | 27 |
|
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', |
38 | 49 | }; |
39 | 50 |
|
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); |
52 | 52 |
|
53 | 53 | 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 | + |
57 | 63 | console.log(`Wrote to ${logName}`); |
58 | 64 | } |
59 | 65 | writeLogEntry(); |
|
0 commit comments