[英]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
优点 :
logging.info(...)
, you know you're writing a log message. logging.info(...)
,您知道您正在写一条日志消息。 Cons for logging
: logging
缺点:
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.