0
votes

When using console.log in a node app deploy on app engine, if i console.log an object, it's all written line by line.

I would like to know if it's possible to have it logged in a clean way (cf picture) this is the result of console.log('connection: ', connection);

enter image description here

Idealy i would like to have my object connection appear like that :

enter image description here

I'm probably missing something there, if anyone can help me clear that it would be awesome ! thanks

1

1 Answers

1
votes

To log objects as one entry in stackdriver, I use the client library.[1]

Using[1] this is how my app.js looks:

'use strict';

const express = require('express');
const {Logging} = require('@google-cloud/logging');
const app = express();
const logging = new Logging(); 
const log = logging.log('projects/[PROJECT-ID/logs/[ANY-LOG-NAME]');
const resource = {type: 'gae_app',
                  labels: {
                           'project_id': '[PROJECT-ID]',
                           'module_id': '[SERVICE-NAME]',
                           'version_id': '[ANY-VERSION-ID]',
                           'zone': '[CURRENT-APPENGINE-REGION]'
                  }
                 };
const entry = log.entry({resource},"Plain message one");
let dict1 = {"First name": "Jhon",
             "Last name": "Smith",
             "Age":32,
             "Gender": "Male" };

const secondentry = log.entry({resource},{dict1});

app.get('/', (req, res) => {
  console.log('Using logging client library...');
  log.write([entry,secondentry]);
  res.status(200).send('Printing some messages to the console').end();
});

This is my package.json:

{
  "name": "node101",
  "engines": {
  "node": ">=8.0.0"
},
  "scripts": {
  "start": "node app.js",
  "test": "mocha --exit test/*.test.js"
},
 "dependencies": {
 "express": "^4.16.3",
 "@google-cloud/logging": "^7.3.0"
},
 "devDependencies": {
 "mocha": "^7.0.0",
 "supertest": "^4.0.2"
}

}

It is important to configure properly the resource variable, otherwise your log will not appear. I have used[2][3] to configure the resource variable, you can look at it for more information about other resources.

I deploy using the --version flag; the value of the flag is equal to the one given resource variable in app.js

gcloud app deploy --version='[ANY-VERSION_ID]'

[1] https://cloud.google.com/logging/docs/api/tasks/creating-logs#writing_log_entries [2] https://cloud.google.com/monitoring/api/resources#tag_gae_app [3] https://cloud.google.com/logging/docs/reference/v2/rest/v2/MonitoredResource