Which design pattern would you consider when Logging is needed?

后端 未结 5 1971
悲哀的现实
悲哀的现实 2021-02-02 00:11

An application I\'m working on requires logging of actions, user who performs the action, and time of action to a database.

Which design pattern is most popular/appropri

5条回答
  •  生来不讨喜
    2021-02-02 00:56

    The command pattern sounds fine. It especially enables you to pass the logger to the command and let the command perform the log operation on the logger. In this way you can have each action care for formatting of the log itself and if some actions should not be logged or need special information you overall architecture don't need to know about it. The downside is the coupling of the actions to the logger if you want to to avert this you could give every action a method that returns a log string that should be added.

    If you will have a lots of different actions I don't think this is overkill, if that are only database actions and you can have the database framework perform actions at every database action it may be reinventing the wheel to have a one logging mechanism but as marcgg points out this depends on you architecture.

提交回复
热议问题