You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在PyCharm中让控制台输出内容自动附带日期或时间?

给PyCharm控制台打印自动添加时间戳的方法

方法一:重写内置print函数

在项目入口文件开头加入这段代码,之后所有print调用都会自动带上时间戳,无需修改原有代码:

import datetime

def print(*args, **kwargs):
    # 生成指定格式的时间戳
    timestamp = datetime.datetime.now().strftime("[%m/%d %H:%M]")
    # 调用原生print函数,先输出时间戳再输出内容
    __builtins__.print(timestamp, *args, **kwargs)

方法二:使用logging模块

如果项目需要日志分级管理,用logging更合适,配置好格式后用logging.info替代print即可:

import logging

# 配置日志格式,指定时间戳样式
logging.basicConfig(
    format='[%(asctime)s] %(message)s',
    datefmt='%m/%d %H:%M',
    level=logging.INFO
)

# 示例调用
logging.info("Hello World!")
logging.info(f"1 + 2 = {1+2}")

方法三:PyCharm插件实现(无需改代码)

安装Grep Console插件后,通过控制台过滤规则自动添加时间戳:

  • 打开PyCharm的Settings -> Plugins,搜索并安装Grep Console
  • 进入Settings -> Other Settings -> Grep Console,找到Output Filters
  • 添加新过滤器,设置Pattern为.+,Replace with为[%m/%d %H:%M] $0
  • 启用该过滤器,之后控制台所有输出都会自动带上时间戳

内容的提问来源于stack exchange,提问作者Privvet

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.31 13:40:59