nodejs-logging-bunyan
nodejs-logging-bunyan copied to clipboard
Doubts with Bunyan usage in Cloud Functions
Hello.
I'm trying to add logs on my nodejs CF with bunyan.
My logging setup looks like this:
const { ENV } = require("./env");
const bunyan = require('bunyan');
// Imports the Google Cloud client library for Bunyan
const {LoggingBunyan} = require('@google-cloud/logging-bunyan');
// Creates a Bunyan Cloud Logging client
const loggingBunyan = new LoggingBunyan({
redirectToStdout: true,
logName: 'tracking-logs' // Al parecer esto no funciona con redirectToStdout
});
let streams = [];
if (ENV === 'local') {
console.info("Local logging");
streams = [
{stream: process.stdout, level: 'debug'}
];
} else {
console.info("GCP logging");
streams = [
// Log to the console at 'info' and above
// {stream: process.stdout, level: 'info'},
// And log to Cloud Logging, logging at 'info' and above
loggingBunyan.stream('info')
];
}
// Create a Bunyan logger that streams to Cloud Logging
// Logs will be written to: "projects/YOUR_PROJECT_ID/logs/bunyan_log"
const logger = bunyan.createLogger({
// The JSON payload of the log as it appears in Cloud Logging
// will contain "name": "my-service"
name: 'engine',
streams: streams
});
exports.logger = logger;
It seems to work OK on the logging explorer, severity and jsonPayload of logs is shown OK, but on the CF records tab it shows this:

Based on the documentation, it says we should prefer setting redirectToStdout: true for cloud functions, but when i do this, the logName doesn't show up. In order to avoid log duplication in the logging explorer i commented the stream that is mapped to process.stdout.
My questions are:
- Should i leave both process.stdout and loggingBunyan.stream? If i leave both i have duplicated records on my log explorer.
- If i use redirectToStdout, is logName ignored for logging explorer?
- Why does my cloud function records tab show JSON entries without a visible message and a {{projectId}} logname field?
- Is there some recommended way of logging for cloud functions?? I want to be able to know the severity in the logging explorer and save in the jsonPayload debug data.
Any update on this? Seeing the same behavior.
Sorry on late response @mat105 and @Bmgaynor. Please see corresponding answers to your question:
- Indeed, if you use both,
stdoutandLoggingBunyan.streamyou will have duplicate records which I believe should be avoided. - If you use
redirectToStdout, indeed thelogNameis going to be overriden by Cloud Run/Functions. This is by design as of now we have an open issue to resolve this matter and we dont have a clear ETA. Thats said, we do embedlogNameinjsonPayloadwhen logs are printed tostdout. - Could you perhaps share the expanded message? I guess that message is not shown clearly in Logs Explorer because you didn't set to
falsetheuseMessageFielddescribed here. - Using
redirectToStdoutis more reliable in Cloud Run/Functions - I would recommend to update to latest library and retry your code withredirectToStdout = trueanduseMessageField = falseand sharing your feedback if the output looks acceptable.
Closing this issue for now since seems there is no more comments - feel free to ask more questions or reactivate per need. Thanks!
@losalex any update on the logName overriden by Cloud Run? We need to used sink to route the logs to gcs, but since sinks don't support paths, and logName is not configurable, we have to create a bucket per service and this generates a lot of overhead