所有
我正在开发一项功能,该功能将在执行操作时使用ftp将日志写入文件服务器。请注意,只有在配置文件服务器时才会发生写入文件服务器。如果未配置服务器,则操作将退出并返回状态。流程是这样的:
1.执行操作 2.如果连接了文件服务器(检入数据库并执行ping),则写入日志 回来
现在我想知道是否有相同功能的设计模式,但功能的范围将根据是否完成某些配置而有所不同。我非常感谢2种情况下的帮助:
静态 - 如果在启动期间DB配置是一次 - 如在启动后,系统可以“假设”文件服务器是否存在基于从DB读取
动态 - 当系统启动并运行时,我可能会启动文件服务器并配置数据库。理想情况下,对于此方案,系统应检测文件服务器并开始向其写入日志,而不是强制重新启动系统。
在这方面请求帮助。
由于
答案 0 :(得分:0)
您的设计似乎违反了Single Responsibility Principle。您正在纠缠两个不同的问题:第一个问题是操作本身,第二个问题是将日志发送到中心位置。
考虑将组件分成两个更简单,独立的组件。其中一个执行业务操作并写入日志,比如说本地文件,以及它。另一个组件检查本地文件系统上是否存在新日志,并将它们复制到中心位置。
答案 1 :(得分:0)
You didn't mention whether or not you are using an existing logging framework, such as Log4J. If not, it would probably be a good idea - if you try to roll out your own logging framework, you can end up having to deal with additional unforeseen complexities, such as dealing with log levels (INFO, DEBUG, ERROR, etc.).
With regards to your original message - I'd consider using the Factory pattern - create a factory class that can internally check whether the file server is available, and return one of two different logger types - something like a ConsoleLogger and an FTPLogger. Make sure that both of these implement the same interface so that your calling code doesn't have to care about what type of logger it's using. Alternatively, you can also use a Decorator that can wrap the object performing your operation - and once it completes a request, have the decorator do the logging.
A final comment - try to avoid checking whether the file server is available every time that you log. A database hit on every log call could result in horrible performance, not to mention that you'll have to ensure that errors in the logging method (such as DB locks) don't result in your entire operation failing.