Data logging: Triggers or special design pattern?
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 t开发者_如何学运维o 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.
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 I prefer to use a Decorator to implement logging and other cross-cutting concerns.
精彩评论