<p>Okay, here's the scenario. I have a utility that processes tons of records, and enters information to the Database accordingly.</p>

<p>It works on these records in multi-threaded batches. Each such batch writes to the same log file for creating a workflow trace for each record. Potentially, we could be making close to a million log writes in a day.</p>

<p>Should this log be made into a database residing on another server? Considerations:</p>

<ol>
<li>The obvious disadvantage of multiple threads writing to the same log file is that the log messages are shuffled amongst each other. In the database, they can be grouped by batch id.</li>
<li>Performance - which would slow down the batch processing more? writing to a local file or sending log data to a database on another server on the same network. Theoretically, the log file is faster, but is there a gotcha here?</li>
</ol>

<p>Are there any optimizations that can be done on either approach?</p>

<p>Thanks.</p>

[{'answer_id': 29648, 'author': 'samjudson', 'author_id': 1908, 'author_profile': 'https://Stackoverflow.com/users/1908', 'pm_score': 0, 'selected': False, 'text': '<p>I think it depends greatly on what you are doing with the log files afterwards.</p>\n\n<p>Of the two operations writing to the log file will be faster - especially as you are suggesting writing to a database on another server.</p>\n\n<p>However if you are then trying to process and search the log files on a regular basis then the best place to do this would be a database.</p>\n\n<p>If you use a logging framework like log4net they often provide simple config file based ways of redirecting input to file or database.</p>\n'}, {'answer_id': 29650, 'author': 'Gishu', 'author_id': 1695, 'author_profile': 'https://Stackoverflow.com/users/1695', 'pm_score': 1, 'selected': False, 'text': '<p>Database - since you mentioned multiple threads. Synchronization as well as filtered retrieval are my reasons for my answer.<br>\nSee if you have a performance problem before deciding to switch to files<br>\n<em>"Knuth: Premature optimization is the root of all evil" I didn\'t get any further in that book... :)</em></p>\n'}, {'answer_id': 29651, 'author': 'Josh', 'author_id': 257, 'author_profile': 'https://Stackoverflow.com/users/257', 'pm_score': 1, 'selected': False, 'text': "<p>There are ways you can work around the limitations of file logging.</p>\n\n<p>You can always start each log entry with a thread id of some kind, and grep out the individual thread ids.  Or a different log file for each thread.  </p>\n\n<p>I've logged to database in the past, in a separate thread at a lower priority.  I must say, queryability is very valuable when you're trying to figure out what went wrong.</p>\n"}, {'answer_id': 29652, 'author': 'Rob Cooper', 'author_id': 832, 'author_profile': 'https://Stackoverflow.com/users/832', 'pm_score': 3, 'selected': True, 'text': '<p>I second the other answers here, <strong>depends on what you are doing with the data</strong>.</p>\n\n<p>We have two scenarios here:</p>\n\n<ol>\n<li><p>The majority of the logging is to a DB since admin users for the products we build need to be able to view them in their nice little app with all the bells and whistles.</p></li>\n<li><p>We log all of our diagnostics and debug info to file. We have no need for really "prettifying" it and TBH, we don\'t even often need it, so we just log and archive for the most part.</p></li>\n</ol>\n\n<p>I would say if the user is doing anything with it, then log to DB, if its for you, then a file will probably suffice.</p>\n'}, {'answer_id': 29653, 'author': 'Community', 'author_id': -1, 'author_profile': 'https://Stackoverflow.com/users/-1', 'pm_score': 1, 'selected': False, 'text': '<p>How about logging to database-file, say a SQLite database? I think it can handle multi-threaded writes - although that may also have its own performance overheads. </p>\n'}, {'answer_id': 29658, 'author': 'Rowan', 'author_id': 2087, 'author_profile': 'https://Stackoverflow.com/users/2087', 'pm_score': 2, 'selected': False, 'text': '<p>One thing that comes to mind is that you could have each thread writing to its own log file and then do a daily batch run to combine them.</p>\n\n<p>If you are logging to database you probably need to do some tuning and optimization, especially if the DB will be across the network. At the least you will need to be reusing the DB connections.</p>\n\n<p>Furthermore, do you have any specific needs to have the log in database? If all you need is a "grep " then I don\'t think you gain much by logging into database.</p>\n'}, {'answer_id': 29667, 'author': 'ZombieSheep', 'author_id': 377, 'author_profile': 'https://Stackoverflow.com/users/377', 'pm_score': 3, 'selected': False, 'text': "<p>The interesting question, should you decide to log to the database, is where do you log database connection errors?  </p>\n\n<p>If I'm logging to a database, I always have a secondary log location (file, event log, etc) in case there are communication errors.  It really does make it easier to diagnose issues later on.</p>\n"}, {'answer_id': 29681, 'author': 'onnodb', 'author_id': 1037, 'author_profile': 'https://Stackoverflow.com/users/1037', 'pm_score': 2, 'selected': False, 'text': '<p>Not sure if it helps, but there\'s also a utility called <a href="http://www.microsoft.com/technet/scriptcenter/tools/logparser/default.mspx" rel="nofollow noreferrer">Microsoft LogParser</a> that you can supposedly use to parse text-based log files and use them as if they were a database. From the website:</p>\n\n<blockquote>\n  <p>Log parser is a powerful, versatile\n  tool that provides universal query\n  access to text-based data such as log\n  files, XML files and CSV files, as\n  well as key data sources on the\n  Windows® operating system such as the\n  Event Log, the Registry, the file\n  system, and Active Directory®.  You\n  tell Log Parser what information you\n  need and how you want it processed.\n  The results of your query can be\n  custom-formatted in text based output,\n  or they can be persisted to more\n  specialty targets like SQL, SYSLOG, or\n  a chart.  Most software is designed to\n  accomplish a limited number of\n  specific tasks. Log Parser is\n  different... the number of ways it can\n  be used is limited only by the needs\n  and imagination of the user.  The\n  world is your database with Log\n  Parser.</p>\n</blockquote>\n\n<p>I haven\'t used the program myself, but it seems quite interesting!</p>\n'}, {'answer_id': 29982, 'author': 'James A. Rosen', 'author_id': 1190, 'author_profile': 'https://Stackoverflow.com/users/1190', 'pm_score': 2, 'selected': False, 'text': "<p>Or how about logging to a queue?  That way you can switch out pollers whenever you like to log to different things.  It makes things like rolling over and archiving log files very easy.  It's also nice because you can add pollers that log to different things, for example:</p>\n\n<ul>\n<li>a poller that looks for error messages and posts them to your FogBugz account</li>\n<li>a poller that looks for access violations ('x tried to access /foo/y/bar.html') to a 'hacking attempts' file</li>\n<li>etc.</li>\n</ul>\n"}, {'answer_id': 208678, 'author': 'noocyte', 'author_id': 11220, 'author_profile': 'https://Stackoverflow.com/users/11220', 'pm_score': 0, 'selected': False, 'text': "<p>I like Gaius' answer. Put all the log statements in a threadsafe queue and then process them from there. For DB you could batch them up, say 100 log statements in one batch and for file you could just stream them into the file as they come into the queue.</p>\n\n<p>File or Db? As many others say; it depends on what you need the log file for.</p>\n"}]
['https://Stackoverflow.com/questions/29643', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/380/']