Reputation: 31455
Following this tutorial and some other docs, I have this code, which is working fine, as far as sending logs to my project's Cloud Logging:
testLogging.ts
import { Logging } from "@google-cloud/logging";
const PROJECT_ID = "MY_PROJECT_ID";
const REGION = "MY_REGION";
const SERVICE_NAME = "MY_SERVICE_NAME";
const logging = new Logging({ projectId: PROJECT_ID });
const logName = "my-logs";
const log = logging.log(logName);
const resource = {
type: "cloud_run_revision", // I'M LOGGING FOR A CLOUD RUN CONTAINER
labels: {
service_name: SERVICE_NAME,
location: REGION,
project_id: PROJECT_ID,
revision_name: "",
configuration_name: ""
}
};
const text_msg = `MY ERROR MSG`;
const json_payload = { foo: "bar" };
const text_entry = log.entry(
{ resource, severity: "ERROR" },
text_msg
);
const json_entry = log.entry(
{ resource, severity: "ERROR" },
json_payload
);
log.write(text_entry);
log.write(json_entry);
They are working. And this is getting logged:
But so far I've only been able to log it separately. And I'd like to send a single log entry with a text message and a JSON payload. How can I do that?
References:
Upvotes: 0
Views: 1993
Reputation: 3842
Update after testing
This version is what I tested with and works, so see if you can adapt it to your needs.
const {Logging} = require("@google-cloud/logging");
const PROJECT_ID = "PROJECT_ID";
const REGION = "REGION";
const SERVICE_NAME = "MY_SERVICE_NAME";
const KEYFILE_PATH = "keyfile.json";
const logging = new Logging({ projectId: PROJECT_ID, keyFile: KEYFILE_PATH, fallback: false });
const logName = "my-log";
const log = logging.log(logName);
const resource = {
type: "cloud_run_revision", // I'M LOGGING FOR A CLOUD RUN CONTAINER
labels: {
service_name: SERVICE_NAME,
location: REGION,
project_id: PROJECT_ID,
revision_name: "",
configuration_name: ""
}
};
const text_msg = `MY ERROR MSG`;
const json_payload = { foo: "bar" };
// The metadata associated with the entry
const metadata = {
resource,
severity: 'ERROR',
textPayload: text_msg,
jsonPayload: json_payload
};
// Prepares a log entry
const entry = log.entry(metadata);
async function writeLog() {
await log.write(entry);
console.log('Logged!');
}
writeLog();
Original Post
The entry()
function accepts multiple configurations, try dropping the second parameter and just passing it all in the metadata.
log.entry({
resource,
severity: "ERROR",
jsonPayload: json_payload,
textPayload: text_payload
});
Upvotes: 2
Reputation: 31455
With the help of @BrianBurton and his answer, I've found a way to do it:
From the LogEntry reference doc, wee see that the payload must be one of the following: protoPayload
, textPayload
or jsonPayload
.
But I just found out that if you add a message
field to your jsonPayload
object, it will be displayed as the error or log message.
For example:
const json_payload = { message: "SOMETHING HAPPENED", foo: "bar" };
const json_entry = log.entry(
{ resource,
severity: "ERROR",
},
json_payload
);
log.write(json_entry);
Results in:
Upvotes: 1