Which design pattern would you consider when Logging is needed?

后端 未结 5 1986
悲哀的现实
悲哀的现实 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 01:07

    Observer Pattern is well suited for logging framework. You can have Logger class extending Observable, and child classes like log to console, log to Database and log to File system etc and each child class implements Observer. Now whenever a log message is logged all the observer classes registered with Logger class will be notified so that each Child class ex: log to console will log the message to console. Also Logger class can follow Singleton pattern to make sure a single instance of Logger is available through out application.

提交回复
热议问题