lundi 20 novembre 2017

What's the cleanest way to check if logging is enabled?

I am implementing a software and the user can, before starting a task, decide if he wants to create a log file for this task.

  • A csv-writer is created if logging is enabled
  • Throughout the code there are several lines which use the csv-writer to write into the log
  • The csv-writer is disposed

There are now three ways how to check if logging is enabled and I don't like any of them:

  1. Every time csv-writer is created or accessed put a 'if (isLogging)' check before it (a ton of calls)
  2. Create a csv-writer interface and make calls to the interface. Create another EmptyCsvWriter that implements the interface and has empty methods and initialize this one when isLogging is false (seems wrong to create a 'fake' null object)
  3. Create the csv-writer and pass isLogging. Check inside csv-writer if isLogging is enabled on every method call (why even create the csv-writer object in the first place - seems wrong, too)

Aucun commentaire:

Enregistrer un commentaire