什么更有效 - 将日志存储在 sql 数据库或文件中? [英] What's more efficient - storing logs in sql database or files?

查看:90
本文介绍了什么更有效 - 将日志存储在 sql 数据库或文件中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有几个脚本经常被 cron 加载.现在我不存储任何日志,所以如果任何脚本加载失败,我在看到结果之前不会知道它 - 即使我注意到结果不正确,我也不能做任何事情,因为我没有不知道哪个脚本失败了.

I have few scripts loaded by cron quite often. Right now I don't store any logs, so if any script fails to load, I won't know it till I see results - and even when I notice that results are not correct, I can't do anything since I don't know which script failed.

我决定存储日志,但我仍然不知道如何去做.所以,我的问题是 - 什么更有效 - 将日志存储在 sql 数据库或文件中?

I've decided to store logs, but I am still not sure how to do it. So, my question is - what's more efficient - storing logs in sql database or files?

我可以在我的 mysql 数据库中创建日志"表并将每个日志存储在单独的行中,或者我可以只使用 php 的 file_put_contents 或 fopen/fwrite 将日志存储在单独的文件中.

I can create 'logs' table in my mysql database and store each log in separate row, or I can just use php's file_put_contents or fopen/fwrite to store logs in separate files.

我的脚本在工作时每分钟大约会添加 5 个日志(总共).我做了很少的测试来确定什么更快 - fopen/fwrite 或 mysql 的插入.我将插入"语句循环了 3000 次以生成 3000 行,并将 fopen/fwrite 循环了 3000 次以生成 3000 个带有示例文本的文件.fwrite 的执行速度比 sql 的 insert 快 4-5 倍.我进行了第二个循环——我循环了一个select"语句并将其分配给一个字符串 3000 次——我还使用fopen"打开了 3000 个文件并将结果分配给字符串.结果是一样的 - fopen/fwrite 完成任务的速度提高了 4-5 倍.

My scripts would approximately add 5 logs (in total) per minute while working. I've done few tests to determine what's faster - fopen/fwrite or mysql's insert. I looped an "insert" statement 3000 times to make 3000 rows and looped fopen/fwrite 3000 times to make 3000 files with sample text. Fwrite executed 4-5 times faster than sql's insert. I made a second loop - I looped a 'select' statement and assigned it to a string 3000 times - I also opened 3000 files using 'fopen' and assigned the results to the string. Result was the same - fopen/fwrite finished the task 4-5 times faster.

那么,对于所有有经验的程序员 - 您在存储日志方面有什么经验?有什么建议吗?

So, to all experienced programmers - what's your experience with storing logs? Any advice?

//04.09.2011 编辑 -谢谢大家的回答,给了我很大的帮助.每个帖子都很有价值,所以很难只接受一个答案;-)

// 04.09.2011 EDIT - Thank you all for your answers, they helped ma a lot. Each post were valuable, so it was quite hard to accept only one answer ;-)

推荐答案

你可以使用诸如 Zend_Log 原生支持附加到同一个日志实例的作者概念.通过这种方式,您可以将相同的消息记录到一个或多个不同的地方,而无需更改您的记录代码.您可以随时更改代码以替换日志系统或以简单的方式添加新的系统.

You can use a component such as Zend_Log which natively supports the concept of writers attached to the same log instance. In that way you can log the same message to one or more different place with no need to change your logging code. And you can always change your code to replace the log system or add a new one in a simple way.

对于您的问题,如果您(开发人员)是唯一需要阅读日志消息的人,我认为将日志记录到文件更简单、更合适.

For your question I think that log to files is simpler and more appropriate if you (developer) is the only one who needs to read log messages.

如果您需要其他人需要在 Web 界面中读取日志,或者您需要能够搜索日志,请改为登录 db.正如其他人所指出的,并发也很重要,如果您有很多用户登录到数据库,则可以更好地扩展.

Log to db instead if you need other people needs to read logs in a web interface or if you need the ability to search through logs. As someone else has pointed out also concurrency matters, if you have a lot of users log to db could scale better.

最后,每分钟 5 条消息的日志频率几乎不需要您的应用程序的 CPU,因此您无需担心性能.在您的情况下,我会从日志文件开始,然后在您的需求发生变化时进行更改(或添加更多作者).

Finally, a log frequency of 5 messages per minute requires almost no cpu for your application, so you don't need to worry about performances. In your case I'd start with logfiles and then change (or add more writers) if your requisites will change.

这篇关于什么更有效 - 将日志存储在 sql 数据库或文件中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆