数据记录:触发器还是特殊的设计模式?

发布于 2024-10-30 04:40:18 字数 1203 浏览 1 评论 0原文

我目前正在为我们的一个网络项目开发日志解决方案。

  • 服务器端语言:PHP 5.2.3
  • 数据库:MySQL 5.0
  • Apache 2

与此处所述类似

基于MySQL触发器的审计日志记录与比较

我们想要记录现有记录的更改列、新记录的插入和记录的删除。对于更新,我必须以某种方式比较旧值 数据库记录与用户保存的记录。对于插入,日志记录很容易,对于删除,我必须先获取旧值来存储,然后再删除日志表中。

我现在面临的问题是。您认为更好的解决方案是什么。

通过数据库级​​别的触发器进行日志记录或在应用程序级别使用合适的设计模式。

已经存在一些关于它的线程,但没有考虑哪个更好,一个(触发器)或另一个(在应用程序级别)。

在另一个线程中建议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

Audit Logging Strategies

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

与他有关 2024-11-06 04:40:18

如果您不在触发器中执行此操作,则您不会记录所需信息的可能性接近 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文