BBrother commited on
Commit
c32c689
1 Parent(s): 25322d7

Update nologs.py

Browse files
Files changed (1) hide show
  1. nologs.py +15 -9
nologs.py CHANGED
@@ -1,18 +1,24 @@
 
1
  import os
2
  import subprocess
3
 
 
 
 
 
4
  try:
5
- # 将标准输出和标准错误重定向到临时文件
6
- with open("temp_output.txt", "w") as temp_output:
7
- # 执行Python程序,将标准输出和标准错误重定向到临时文件
8
- subprocess.run(["python", "sd_clsa_webui_colab_ipynb_v2_3.py"], stdout=temp_output, stderr=temp_output)
9
 
10
  except subprocess.CalledProcessError as e:
11
- # 如果有报错,打印错误信息
12
- print(f"Error: {e}")
13
- with open("temp_output.txt", "r") as temp_output:
14
- print("Program Output:")
15
- print(temp_output.read())
 
 
 
16
 
17
  finally:
18
  # 恢复标准输出和标准错误到默认
 
1
+ import logging
2
  import os
3
  import subprocess
4
 
5
+ # 配置日志
6
+ log_file = '/content/logs.txt'
7
+ logging.basicConfig(filename=log_file, level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s')
8
+
9
  try:
10
+ # 执行Python程序并将标准输出和标准错误重定向到日志文件
11
+ subprocess.run(["python", "sd_clsa_webui_colab_ipynb_v2_3.py"], stdout=subprocess.PIPE, stderr=subprocess.PIPE, check=True)
 
 
12
 
13
  except subprocess.CalledProcessError as e:
14
+ # 如果程序报错,将异常信息输出到日志文件
15
+ logging.error(f"程序执行失败: {e}")
16
+ logging.error(f"标准输出:\n{e.stdout.decode()}")
17
+ logging.error(f"标准错误:\n{e.stderr.decode()}")
18
+
19
+ except Exception as e:
20
+ # 如果发生其他异常,将异常信息输出到日志文件
21
+ logging.exception(f"程序发生异常: {e}")
22
 
23
  finally:
24
  # 恢复标准输出和标准错误到默认