更重要的是有效的 - 存储SQL数据库或文件,日志? [英] What's more efficient - storing logs in sql database or files?

查看:156
本文介绍了更重要的是有效的 - 存储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执行速度快4-5倍,比SQL的插入。我做了第二循环 - 我一个循环'选择'语句并将其分配到一个字符串3000次 - 我用'F打开和分配结果字符串也开了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?

// 2011年9月4日编辑 -
谢谢大家对你的答案,他们帮助马了很多。每一个职位是有价值的,所以这是非常难以接受的只有一个答案; - )

// 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进行它本身支持连接到同一个日志实例作家的概念。这样,你就可以不需要改变你的日志code登录同样的消息到一个或多个不同的地方。你可以随时更改您code来代替日志系统或添加一个简单的方法一个新的。

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.

登录,如果您需要其他人需要在一个网络界面来阅读日志,或者如果需要通过日志来搜索的能力,而不是数据库。正如有人所指出的还并发的问题,如果你有很多用户登录到数据库可以更好地扩展

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天全站免登陆