-1

When building a backend, I usually log messages inside the controllers like this:

router.get('/books', async () => {

const books = await booksService.getAll()

logger.info('All books returned successfully')

return books

})

But what if getAll method from booksService is used in another service method? Should I then log in the service methods too? For example:

class BookService {
  
getAll() {
  
 const books = [] // all books

 logger.info('All books returned successfully')

 return books
}

}

For the endpoint /books, this will lead me to two logs with the same information called in different layers. But, it will be useful if another service method calls multiple service, then I will receive information about each service method being called, for example:

class ThirdApiLayerService {

syncBooks() {

 const books = await booksService.getAll()

 await this.createBooks(books)

 logger.info('Books synced successfully')

 return books
}

createBooks(books) {
  const parsedBooks = this.parse(books)
  await this.save(parsedBooks) 

  logger.info('Books parsed and saved successfully')
  return parsedBooks
}

}

So then my question is: Should I log messages inside both controllers and service regardless of repeating messages, which will be only differentiated by the layer that called them? I know I can add context to the logger so the layer where the message is sent is logged too, my question is only about repeated messages being written.

Thanks in advance!

Federico Peralta
  • 337
  • 4
  • 11

1 Answers1

1

Normally you would use INFO logs for ingress/egress messages i.e. in your controller or for external service to service calls.

Other log messages such as in your BookService would be DEBUG or TRACE depending on the use case.

You should also log a correlation/request id in each log message otherwise the logs will be pretty useless in production.

**** Make you should also redact sensitive information. ****

Popular logging frameworks such as winston and pino can help you with this.

Alternatively you can use a nodejs framework such as NestJS which has logging built into it. See this post for further info

mh377
  • 1,656
  • 5
  • 22
  • 41
  • So are suggesting that I won't need log what my services are doing under the hood in production? Thanks for the request ID suggestion! Maybe I wasn't clear enough, my concern is only about repeated messages inside controller and services. I'm fine with logging implementation in regards of frameworks, tracing id, etc.. – Federico Peralta Dec 14 '22 at 16:18
  • No I am not saying that you dont need to log what your services are doing in production. In production you normally only have INFO/ERROR logs so even if you have the same message in the controller and the service where the controller is INFO and the service is DEBUG, only the INFO log will be displayed. You could also change the text in the messages so they are slightly different if you are worried about duplicates – mh377 Dec 14 '22 at 17:07