Chad Johnson
Chad Johnson

Reputation: 21895

Should I use one class or more?

In my PHP web application, I want to have the ability to perform the following logging operations:

  1. Write a database record to an 'error_log' table.
  2. Write a database record to a 'history_log' table.
  3. Log all queries to the Firebug console via FirePHP.
  4. Log any arbitrary data to the Firebug console using FirePHP.

I am trying to decide on the better achitecture. I have two in mind. Which of these is the better one? I'm open to others as well.

Design #1

Design #2

EDIT Here's what I'm probably going to go with.

Upvotes: 1

Views: 52

Answers (1)

GolezTrol
GolezTrol

Reputation: 116110

I would use neither. I'd use a design that implements the same log method for each implementation. So it looks like Design #1, but a little different.

  • abstract class Logger
    • log(string)

Create descendants for each type of log you have and override the log method.

Then, create a factory for specific purposes, so:

  • class QueryLoggerFactory
    • getInstance() // Returns a/the FireBugConsoleLogger instance
  • class ErrorLoggerFactory
    • getInstance() // Returns a database logger instance
  • class HistoryLoggerFactory
    • getInstance() // Returns same or different database logger instance

That way, when you need to log a query, you just call

QueryLoggerFactory->getInstance()->log($query);

And nowhere in your application you need to call a specific method. If you decide you want to store the query in the database too, you just instantiate a different logger in your factory. You can even instantiate a logger that logs itself to two other loggers, so you can store errors in FireBug and in the database. Or you can instantiate a void logger that doesn't log anything.

Upvotes: 2

Related Questions