After some research on this, I find that as of now it is better to handle it ourselves as follows.
Create a custom logger which you can use for complex logging. And use the general option, i.e. console.log, for other simple, single-line logs. I assume the type of log we are trying to handle this way is not a regular job. It is mostly required for some initial debugging needs primarily.
I created the following custom log function with the help of Google Spreadsheet services. My environment is as follows.
- Node.js v14.17.1
- Visual Studio Code (VS Code) IDE
'use strict'
const { google } = require('googleapis') // required to connect to spreadsheet
const keys = require('./editor-key.json')
const { istNowStr } = require('./date-time') // you may avoid this and use simple date time
const { stringify } = require('flatted') // to handle circular JSON
const client = new google.auth.JWT(
keys.client_email,
null,
keys.private_key,
['https://www.googleapis.com/auth/spreadsheets']
)
client.authorize((err, tokens) => {
if (err)
console.log('spreadsheet authorization error at log.js')
else {
// console.log('Connected!');
}
})
function log(log, note) {
return new Promise(async (resolve, reject) => {
try {
const gsapi = google.sheets({ version: 'v4', auth: client })
try {
log = (typeof log === 'string' ? log : JSON.stringify(log)) // this stringify is better
} catch {
log = (typeof log === 'string' ? log : stringify(log)) // only if the above fails (for circular JSON)
}
const values = [[istNowStr(), log, note]]
const opt = {
spreadsheetId: '<your spreadsheet id>',
range: 'log',
valueInputOption: 'RAW',
insertDataOption: 'INSERT_ROWS',
resource: { values }
}
await gsapi.spreadsheets.values.append(opt, (err, result) => {
if (err) {
console.log('spreadsheet append error at log.js')
reject(err)
} else
// console.log(`${result} cells appended.`)
resolve(result)
})
} catch (err) {
reject(err)
}
})
}
module.exports = { log }
// // for testing the functions
// async function test() {
// const testRes = await log('testing log', 'testing note')
// // const logTxt = JSON.stringify(testRes)
// await log(testRes, 'testing note')
// // console.log(logTxt)
// }
// test()
I take a step further by copying the above output and paste it into the Visual Studio Code.
How in VS Code: New File → Select a language → JSON → Paste the copied log → Right Click → Format Document. This increases the readability of the output. It looks something as follows.
Update on 19-Aug-2021
Recently I have used my custom function call tracer to improve this logger function as follows.
Hope you could locate the enhancement in the code above after line number 11.
function log(log, note) {
return new Promise(async (resolve, reject) => {
try {
const gsapi = google.sheets({ version: 'v4', auth: client })
try {
log = (typeof log === 'string' ? log : JSON.stringify(log)) // this stringify is better
} catch {
log = (typeof log === 'string' ? log : stringify(log)) // only if the above fails (for circular JSON)
}
const e = new Error()
let traceDetails = trace(e) // custom function call tracer by Narottam
if (!traceDetails) traceDetails = e.stack
const values = [[istNowStr(), log, note, new Date().toISOString(), traceDetails]]
You can read about this custom function call tracer at Tracing Function Calls in JavaScript or Node.js or Dialogflow
Thank you.
Post a Comment