简体   繁体   English

Python日志记录与写入文件

[英]Python logging vs. write to file

Which is more efficient? 哪个更有效? Is there a downside to using open() -> write() -> close() compared to using logger.info() ? 与使用logger.info()相比,使用open() -> write() -> close() logger.info()吗?

PS. PS。 We are accumulating query logs for a university, so there's a perchance that it becomes big data soon (considering that the min-max cap of query logs per day is 3GB-9GB and it will run 24/7 constantly for a lifetime). 我们正在为一所大学积累查询日志,因此有一种可能很快会变成大数据(考虑到每天查询日志的最小-最大上限为3GB-9GB,并且将持续24/7连续运行一生)。 It would be appreciated if you could explain and differentiate in great detail the efficiency in time and being error prone aspects. 如果您能详细解释和区分时间效率和容易出错的方面,将不胜感激。

Use the method that more closely describes what you're trying to do. 使用更紧密地描述您要执行的操作的方法。 Are you making log entries? 您要输入日志吗? Use logger.* . 使用logger.* If (and only if!) that becomes a performance issue, then change it. 如果(并且仅当!)成为性能问题,请进行更改。 Until then it's an optimization that you don't know if you'll ever need. 在此之前,这是您尚不知道是否需要的优化。

Pros for logging : logging优点

  • It's semantic. 这是语义上的。 When you see logging.info(...) , you know you're writing a log message. 当您看到logging.info(...) ,您知道您正在写一条日志消息。
  • It's idiomatic. 这是惯用的。 This is how you write Python logs. 这就是您编写Python日志的方式。
  • It's efficient. 效率很高。 Maybe not extremely efficient, but it's so thoroughly used that it has lots of nice optimizations (like not running string interpolation on log messages that won't be emitted because of loglevels, etc.). 也许效率不是很高 ,但是它是如此地被广泛使用,以至于它具有许多不错的优化(例如,不对由于日志级别而不会发出的日志消息运行字符串插值)。

Cons for logging : logging缺点:

  • It's not as much fun as inventing your own solution (which will invariably turn into an unfeatureful, poorly tested, less efficient version of logging ). 它并没有发明自己的解决方案那么有趣(它总是会变成功能单一,测试不充分,效率较低的logging版本)。

Until you know that it's not efficient enough, I highly recommend you use it. 除非您知道效率不够高,否则我强烈建议您使用它。 Again, you can always replace it later if data proves that it's not sufficient. 同样,如果数据证明不够用,您以后可以随时替换它。

It is always better to use a built-in facility unless you are facing issues with the built-in functionality. 除非您遇到内置功能方面的问题,否则最好使用内置工具。

So, use the built-in logging function. 因此,请使用内置的日志记录功能。 It is proven, tested and very flexible - something you cannot achieve with open() -> f.write() -> close() . 它经过验证,测试并且非常灵活 -使用open() -> f.write() -> close()无法实现。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM