tags:

views:

330

answers:

7

I want to keep logs of some things that people do in my app, in some cases so that it can be undone if needed.

Is it best to store such logs in a file or a database? I'm completely at a loss as to what the pros and cons are except that it's another table to setup.

Is there a third (or fourth etc) option that I'm not aware of that I should look into and learn about?

+1  A: 

I'd use a database simply for maintainability - also multiple edits on a file may cause some getting missed out.

Ross
+4  A: 

You will almost certainly want to use a database for flexible, record based access and to take advantage of the database's ability to handle concurrent data access. If you need to track information that may need to be undone, having it in a structured format is a benefit, as is having the ability to update a row indicating when and by whom a given transaction has been undone.

You likely only want to write to a file if very high performance is an issue, or if you have very unstructured or large amounts of data per record that might be unweidly to store in a database. Note that Unless your application has a very large number of transactions database speed is unlikely to be an issue. Also note that if you are working with a file you'll need to handle concurrent access (read / write / locking) very carefully which is likely not something you want to have to deal with.

+1  A: 

I will second both of the above suggestions and add that file locking on a flat file log may cause issues when there are a lot of users.

Unkwntech
+1  A: 

Have a look at my earlier thread http://stackoverflow.com/questions/23770/good-strategy-for-leaving-an-audit-trailchange-history-for-db-applications it seems to cover the same topic.

Dana the Sane
+5  A: 

There is at least one definite reason to go for storing in the database. You can use INSERT DELAYED in MySQL (or similar constructs in other databases), which returns immediately. You won't get any return data from the database with these kinds of queries, and they are not guaranteed to be applied.

By using INSERT DELAYED, you won't slow down your app to much because of the logging. The database is free to write the INSERTs to disk at any time, so it can bundle a bunch of inserts together.

You need to watch out for using MySQL's built in timestamp function (like CURRENT_TIMESTAMP or CUR_DATE()), because they will be called whenever the query is actually executed. So you should make sure that any time data is generated in your programming language, and not by the database. (This paragraph might be MySQL-specific)

Vegard Larsen
That's a new one to me. Very slick.
Mark Biek
+1  A: 

I found this article: http://www.devshed.com/c/a/PHP/Logging-With-PHP/

svrist
+2  A: 

I'm a big fan of log4php. It gives you a standard interface for logging actions. It's based on log4j. The library loads a central config file, so you never need to change your code to change logging. It also offers several log targets, like files, syslog, databases, etc.

Gary Richardson