# backend/view_logs.py import os # 获取当前文件所在目录 basedir = os.path.abspath(os.path.dirname(__file__)) log_file = os.path.join(basedir, '..', 'logs', 'app.log') def view_logs(lines=20): """查看最新的日志条目""" try: with open(log_file, 'r', encoding='utf-8') as f: all_lines = f.readlines() # 获取最后几行 latest_lines = all_lines[-lines:] if len(all_lines) > lines else all_lines print(f"最新 {len(latest_lines)} 条日志:") for line in latest_lines: print(line.strip()) except FileNotFoundError: print("日志文件不存在,请先运行应用程序") except Exception as e: print(f"读取日志文件时出错: {e}") if __name__ == '__main__': view_logs()