python 中 设置gunicorn的日志以天为单位
时间: 2024-02-20 17:58:24 浏览: 32
要将Gunicorn的日志设置为按天轮换,可以使用Python的logging模块中的TimedRotatingFileHandler。以下是一个示例配置:
```python
import logging
from logging.handlers import TimedRotatingFileHandler
from gunicorn import glogging
class CustomLogger(glogging.Logger):
def setup(self, cfg):
super().setup(cfg)
handler = TimedRotatingFileHandler(
filename="gunicorn.log",
when="midnight",
interval=1,
backupCount=7
)
formatter = logging.Formatter(fmt="%(asctime)s [%(process)d] [%(levelname)s] %(message)s",
datefmt="%Y-%m-%d %H:%M:%S")
handler.setFormatter(formatter)
self.logger.addHandler(handler)
```
上述代码中,我们定义了一个CustomLogger类,继承自gunicorn的Logger类,并重写了setup()方法。在该方法中,我们创建了一个TimedRotatingFileHandler实例,设置了日志文件名为"gunicorn.log",轮换方式为每天一次,保留7个备份日志文件。接着,我们定义了一个Formatter实例,设置了日志输出的格式。最后,我们将TimedRotatingFileHandler实例添加到Logger中。
要使用这个CustomLogger类,只需要在Gunicorn配置文件中指定logger_class为该类即可:
```bash
gunicorn --logger-class myapp.CustomLogger myapp:app
```
这样就会将Gunicorn的日志设置为按天轮换,并输出到"gunicorn.log"文件中。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)