程序员老王 发布的文章

pre {
      white-space: pre-wrap; /* 保留格式并自动换行 */
      word-wrap: break-word; /* IE 浏览器兼容 */
      overflow-wrap: break-word; /* 现代浏览器兼容 */
      border: 1px solid #ccc; /* 添加边框便于观察 */
      padding: 10px;
    }

import sys
import datetime
import os

def print_with_lineno(message):
    frame = sys._getframe(1)  # 获取调用者的帧
    lineno = frame.f_lineno  # 行号
    filename = os.path.basename(frame.f_code.co_filename)  # 提取文件名
    current_time = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")  # 格式化时间
    print(f"{current_time} - {filename} - Line {lineno}: {message}")

# 示例
print_with_lineno("This is a test message")
print_with_lineno("Another message")