强制日志 asctime 输出为上海时间,避免容器/服务器时区差异。
| 24 | |
| 25 | |
| 26 | class ShanghaiTimeFormatter(logging.Formatter): |
| 27 | """ |
| 28 | 强制日志 asctime 输出为上海时间,避免容器/服务器时区差异。 |
| 29 | """ |
| 30 | |
| 31 | def formatTime(self, record, datefmt=None): # noqa: N802 |
| 32 | dt = datetime.fromtimestamp(record.created, tz=timezone.utc).astimezone(SHANGHAI_TZ) |
| 33 | if datefmt: |
| 34 | return dt.strftime(datefmt) |
| 35 | return dt.strftime("%Y-%m-%d %H:%M:%S") |
| 36 | |
| 37 | |
| 38 | def setup_logging( |