数据记录:触发器还是特殊的设计模式?
我目前正在为我们的一个网络项目开发日志解决方案。
- 服务器端语言:PHP 5.2.3
- 数据库:MySQL 5.0
- Apache 2
与此处所述类似
我们想要记录现有记录的更改列、新记录的插入和记录的删除。对于更新,我必须以某种方式比较旧值 数据库记录与用户保存的记录。对于插入,日志记录很容易,对于删除,我必须先获取旧值来存储,然后再删除日志表中。
我现在面临的问题是。您认为更好的解决方案是什么。
通过数据库级别的触发器进行日志记录或在应用程序级别使用合适的设计模式。
已经存在一些关于它的线程,但没有考虑哪个更好,一个(触发器)或另一个(在应用程序级别)。
在另一个线程中建议AOP
我将按照此处建议的方式使用数据库设计进行日志记录 #1
,其中我有一个单独的表来记录每个观察到的表。结构如下所示
ID : Integer
COLUMN: String, the column name of the column that changed
OLD_VAL: String
NEW_VAL: String
SOURCE: String, the source which changed the data, currently user and an agent are possible as sources
create_at: DATETIME, then the change occurred
,大家觉得怎么样。考虑到性能与干净的代码和信息隐藏,实现这样的日志系统的更好的解决方案是什么。
任何想法表示赞赏。
I'm currently working on a logging solution for one of our web projects.
- Server side language: PHP 5.2.3
- Database: MySQL 5.0
- Apache 2
Similar as described in here
MySQL Trigger based Audit logging with comparisons
we want to log changed columns for existing records, insertion of new records and deletion of records. For updates I have to somehow compare the old values
of the DB records with the the ones, the user saved. For insert the logging is easy and for delete I have to get the old values first to store then before deletion in the logging table.
The question I'm facing right now is. What do you think is the better solution.
Logging via Triggers on the DB level or using a suitable design pattern on the application level.
There already exists some threads about it, but there is no consideration what is better, one (Triggers) or the other (on application level).
In another thread AOP is suggested
Which design pattern would you consider when Logging is needed?
I'm going with the DB design for logging #1 as suggested in here
where I have a separate table for logging for each observed table. The structure is like the following
ID : Integer
COLUMN: String, the column name of the column that changed
OLD_VAL: String
NEW_VAL: String
SOURCE: String, the source which changed the data, currently user and an agent are possible as sources
create_at: DATETIME, then the change occurred
So what do you people think. Considering performance vs. clean code and information hiding, what is the better solution to achieve a logging system like this.
Any ideas appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果您不在触发器中执行此操作,则您不会记录所需信息的可能性接近 100%。并非所有数据都是通过应用程序更改的。即使开发人员认为会是这样。有时需要修复或更新大量数据(假设价格上涨 10%),而这些查询将直接在数据库上进行。仅通过应用程序开始记录意味着您将错过这些更改。有时,这些是恶意用户或员工所做的更改,也是最需要审核的更改之一。
If you do not do it in a trigger the likelihood that you will not log information you need is pretty close to 100%. Not all data is changed through applications. Even when developers think it will be. There are times when large amounts of data need to be fixed or updated (think a price increase of 10%) and those queries will happen directly on the database. To institute logging only through the application will mean you will miss those changes. Sometimes those are the changes made a malicous user or employee and are among the most important to audit.
FWIW我更喜欢使用装饰器来实现日志记录和其他横切关注点。
FWIW I prefer to use a Decorator to implement logging and other cross-cutting concerns.