Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Which design pattern would you consider when Logging is needed?

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/appropriate for logging?

I'm thinking of Command pattern that require the current user and an action. Perform the action and write to the log.

What do you think? Any other alternatives I can consider?

Thank you.

like image 698
Henry Avatar asked Aug 21 '09 17:08

Henry


People also ask

Which design pattern is used in log4j?

It is using a mixture of design pattern, is using Singleton and Chain of responsibilities patterns.

How do I choose a logging framework?

Look for a logging framework with an elegant solution to this problem (or at least an elegant option). Of particular concern, from a codebase cleanliness perspective, are the impact on code noise and ease of unit test writing. Make sure that the framework you choose has guidance on how to handle these concerns.

What is a design pattern in web development?

What are web design patterns? A web design pattern, also known as a user interface design pattern, is a set of guidelines for designing a user interface aspect or component. Web design patterns are developed for specific user experience challenges and can be adopted and implemented by any website.


4 Answers

You can use AOP to apply logging without any intrusive behavior. AOP may feel like a mixture of Proxy and Decorator Pattern.

like image 137
Santosh Gokak Avatar answered Oct 21 '22 19:10

Santosh Gokak


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.

like image 44
Prashant Jadhav Avatar answered Oct 21 '22 19:10

Prashant Jadhav


Don't conflate the Command and the logging Memento.

The Command is something that gets done. Which may include some common aspects across all commands, including writing a log entry.

The log entry itself may be a Memento or a summary of a Memento.

The logger is a kind of Factory, which creates Mementos for logged events.

As with most things, you have a large number of interlocking design patterns. Which "one" pattern is "most popular/appropriate" doesn't enter into it.

The question is "what is supposed to be happening?"

like image 23
S.Lott Avatar answered Oct 21 '22 18:10

S.Lott


I agree, I think the command pattern would fit the most since you would have all the action listed.

But I don't think that you really need to follow a design specific pattern for this one.You can simply set a callback on the actions to update the log. It depends on your architecture and technologies but out of my head the command pattern sounds like overkill.

like image 43
marcgg Avatar answered Oct 21 '22 18:10

marcgg