<p>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.</p>

<p>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.</p>

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

[{'answer_id': 35952, 'author': 'Ross', 'author_id': 2025, 'author_profile': 'https://Stackoverflow.com/users/2025', 'pm_score': 1, 'selected': False, 'text': "<p>I'd use a database simply for maintainability - also multiple edits on a file may cause some getting missed out.</p>\n"}, {'answer_id': 35961, 'author': 'Community', 'author_id': -1, 'author_profile': 'https://Stackoverflow.com/users/-1', 'pm_score': 4, 'selected': True, 'text': "<p>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. </p>\n\n<p>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.</p>\n"}, {'answer_id': 35971, 'author': 'UnkwnTech', 'author_id': 115, 'author_profile': 'https://Stackoverflow.com/users/115', 'pm_score': 1, 'selected': False, 'text': '<p>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.</p>\n'}, {'answer_id': 35995, 'author': 'Vegard Larsen', 'author_id': 1606, 'author_profile': 'https://Stackoverflow.com/users/1606', 'pm_score': 3, 'selected': False, 'text': "<p>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.</p>\n\n<p>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.</p>\n\n<p>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)</p>\n"}, {'answer_id': 42168, 'author': 'Gary Richardson', 'author_id': 2506, 'author_profile': 'https://Stackoverflow.com/users/2506', 'pm_score': 2, 'selected': False, 'text': '<p>I\'m a big fan of <a href="http://incubator.apache.org/log4php/" rel="nofollow noreferrer">log4php</a>. 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. </p>\n'}]
['https://Stackoverflow.com/questions/35943', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/1384652/']