Logging

   log all visits to your web site directly in a table. In such an application, speed is probably the most important goal; you don’t want the database to bethe bottleneck. The MyISAM and Archive storage engines would work very well because they have very low overhead and can insert thousands of records per second. The PBXT storage engine is also likely to be particularly suitable for logging purposes.


Things will get interesting, however, if you decide it’s time to start running reports to summarize the data you’ve logged. Depending on the queries you use, there’s a good chance that gathering data for the report will significantly slow the process of inserting records. What can you do?



You can also run queries at times of low load, but don’t rely on this strategy continuing to work as your application grows.


Another option is to use a Merge table. Rather than always logging to the same table,adjust the application to log to a table that contains the year and name or number of the month in its name, such as web_logs_2008_01 or web_logs_2008_jan. Then define a Merge table that contains the data you’d like to summarize and use it in your queries. If you need to summarize data daily or weekly, the same strategy works; you just need to create tables with more specific names, such as web_logs_2008_01_01.


While you’re busy running queries against tables that are no longer being written to, your application can log records to its current table uninterrupted.

03-12
### Python 中日志记录 (logging) 使用方法 在 Python 开发过程中,`logging` 模块被广泛用于跟踪事件的发生情况以便于后续分析和调试。此模块提供了一种结构化的方式来处理不同严重程度的消息,并允许这些消息发送到多个目的地。 #### 创建并配置 Logger 实例 为了开始使用 `logging` 模块,通常会先获取一个 logger 对象: ```python import logging logger = logging.getLogger('my_logger') ``` 可以通过调用 getLogger() 函数来创建一个新的 logger 或者访问已存在的同名 logger [^4]。一旦有了 logger 对象之后,则可以根据需要对其进行进一步的个性化设置,比如设定最低的日志等级、指定输出格式等。 #### 设置日志级别与处理器 日志级别决定了哪些类型的日志会被实际写入文件或其他目标位置;常见的有 DEBUG, INFO, WARNING, ERROR 和 CRITICAL 这五个级别的日志信息。默认情况下,只有 WARN 及以上级别的日志才会显示出来。如果希望改变这一行为,可以这样做: ```python logger.setLevel(logging.DEBUG) console_handler = logging.StreamHandler() file_handler = logging.FileHandler('app.log') formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s') console_handler.setFormatter(formatter) file_handler.setFormatter(formatter) logger.addHandler(console_handler) logger.addHandler(file_handler) ``` 上述代码片段展示了如何向 logger 添加控制台和文件两个不同的 handler 来分别处理不同类型或者去处的日志数据流 [^3]。 #### 记录日志条目 当一切准备就绪后就可以利用 logger 发布各种各样的日志了: ```python logger.debug("This is a debug message.") logger.info("Informational message here.") logger.warning("Warning:config file not found!") logger.error("Error occurred.") logger.critical("Critical error! System might be unstable now.") ``` 每一种 log 方法对应着特定的日志级别,在编写应用时应当依据实际情况选择合适的方式来进行日志记录 [^1]。 #### 安全性考虑 值得注意的是,在设计日志系统的过程中还需要考虑到安全性因素。例如,应避免将敏感个人信息或者其他机密资料暴露给未经授权的人士查看。因此建议采取措施保护好存储介质上的原始日志文档,并且谨慎对待任何可能泄露隐私的内容 [^2]。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值