File tree Expand file tree Collapse file tree 1 file changed +29
-2
lines changed Expand file tree Collapse file tree 1 file changed +29
-2
lines changed Original file line number Diff line number Diff line change @@ -105,7 +105,30 @@ def getLevel(self):
105
105
return self .__level
106
106
107
107
108
- class Consoler (sys .stdout .__class__ , LeverCtl ):
108
+ # 检测是否在 notebook 环境中
109
+ def in_notebook ():
110
+ try :
111
+ # notebook 中会有 __IPYTHON__,而正常环境没有定义,所以 try
112
+ # 'type: ignore': 可以让 pylance 忽略对变量定义的检查
113
+ __IPYTHON__ # type: ignore
114
+ return True
115
+ except NameError :
116
+ return False
117
+
118
+
119
+ # Consoler 继承的父类
120
+ def __consoler_class ():
121
+ # 如果在 notebook 中,使用 io.StringIO
122
+ if in_notebook ():
123
+ from io import StringIO
124
+
125
+ return StringIO
126
+ # 正常环境使用标准输出
127
+ else :
128
+ return sys .stdout .__class__
129
+
130
+
131
+ class Consoler (__consoler_class (), LeverCtl ):
109
132
# 记录日志行数
110
133
__sum = 0
111
134
@@ -114,7 +137,11 @@ class Consoler(sys.stdout.__class__, LeverCtl):
114
137
__previous_message = None
115
138
116
139
def __init__ (self ):
117
- super ().__init__ (sys .stdout .buffer )
140
+ # 根据环境进行不同的初始化
141
+ if in_notebook ():
142
+ super ().__init__ ()
143
+ else :
144
+ super ().__init__ (sys .stdout .buffer )
118
145
self .original_stdout = sys .stdout # 保存原始的 sys.stdout
119
146
120
147
def init (self , path , swanlog_level = "debug" ):
You can’t perform that action at this time.
0 commit comments