BBrother commited on
Commit
f3cc4f3
1 Parent(s): 8f55005

Update nologs.py

Browse files
Files changed (1) hide show
  1. nologs.py +22 -31
nologs.py CHANGED
@@ -1,39 +1,30 @@
 
1
  import subprocess
2
- import sys
3
  import logging
4
- import os
5
 
6
- # 检查是否存在 logs.txt 文件,如果存在则删除它
7
- log_file = '/content/logs.txt'
8
- if os.path.exists(log_file):
9
- os.remove(log_file)
10
 
11
- # 配置日志
12
- logging.basicConfig(filename=log_file, level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s')
 
 
 
 
 
13
 
14
- # 创建一个将输出同时发送到控制台和日志文件的处理程序
15
- console_handler = logging.StreamHandler(sys.stdout)
16
- console_handler.setLevel(logging.INFO)
17
- formatter = logging.Formatter('%(asctime)s - %(levelname)s - %(message)s')
18
- console_handler.setFormatter(formatter)
19
- logging.getLogger().addHandler(console_handler)
 
20
 
21
- # 重定向标准输出和标准错误到日志文件和控制台
22
- sys.stdout = logging.getLogger().handlers[0].stream
23
- sys.stderr = logging.getLogger().handlers[0].stream
24
 
25
- try:
26
- # 在try块中运行sd_clsa_webui_colab_ipynb_v2_3.py,并将输出重定向到日志文件
27
- with open(log_file, 'a') as log_file_handle:
28
- subprocess.run(['python', 'v2_3.py'], check=True, stdout=log_file_handle, stderr=subprocess.STDOUT)
29
- except subprocess.CalledProcessError as e:
30
- # 捕获异常并记录到日志文件
31
- logging.exception(f"程序发生异常: {e}")
32
  except Exception as e:
33
- # 捕获其他异常并记录到日志文件
34
- logging.exception(f"程序发生异常: {e}")
35
- finally:
36
- # 最后,关闭日志处理程序,以确保所有日志都被写入到日志文件
37
- logging.getLogger().removeHandler(console_handler)
38
- console_handler.close()
39
- print ("主文件安装完毕!请运行!")
 
1
+ import os
2
  import subprocess
 
3
  import logging
 
4
 
5
+ # 删除已存在的 logs.txt 文件
6
+ if os.path.exists("logs.txt"):
7
+ os.remove("logs.txt")
 
8
 
9
+ # 配置 logging,将日志同时输出到文件 logs.txt
10
+ logging.basicConfig(
11
+ level=logging.INFO,
12
+ format="%(asctime)s - %(levelname)s - %(message)s",
13
+ filename="logs.txt",
14
+ filemode="w",
15
+ )
16
 
17
+ try:
18
+ # 执行您的 Python 程序并将标准输出和标准错误都重定向到 subprocess.DEVNULL
19
+ with open(os.devnull, 'w') as null:
20
+ proc = subprocess.Popen(
21
+ ['python', 'v2_3.py'],
22
+ stdout=null, stderr=subprocess.STDOUT
23
+ )
24
 
25
+ # 等待程序执行完成
26
+ proc.wait()
 
27
 
 
 
 
 
 
 
 
28
  except Exception as e:
29
+ # 记录异常信息
30
+ logging.error(f"程序发生异常: {e}")