Hansimov commited on
Commit
047008b
1 Parent(s): 395ee29

:zap: [Enhance] OSLogger: Re-wrap for better syntax check, and move stream log to debug

Browse files
Files changed (2) hide show
  1. networks/message_streamer.py +7 -2
  2. utils/logger.py +41 -23
networks/message_streamer.py CHANGED
@@ -65,7 +65,12 @@ class MessageStreamer:
65
  proxies=enver.requests_proxies,
66
  stream=stream,
67
  )
68
- print(stream.status_code)
 
 
 
 
 
69
  for line in stream.iter_lines():
70
  if not line:
71
  continue
@@ -77,7 +82,7 @@ class MessageStreamer:
77
  logger.success("\n[Finished]")
78
  else:
79
  content_type = "Completions"
80
- logger.mesg(content, end="")
81
 
82
  if yield_output:
83
  output = self.message_outputer.output(
 
65
  proxies=enver.requests_proxies,
66
  stream=stream,
67
  )
68
+ status_code = stream.status_code
69
+ if status_code == 200:
70
+ logger.success(status_code)
71
+ else:
72
+ logger.err(status_code)
73
+
74
  for line in stream.iter_lines():
75
  if not line:
76
  continue
 
82
  logger.success("\n[Finished]")
83
  else:
84
  content_type = "Completions"
85
+ logger.back(content, end="")
86
 
87
  if yield_output:
88
  output = self.message_outputer.output(
utils/logger.py CHANGED
@@ -33,7 +33,7 @@ def add_fillers(text, filler="=", fill_side="both"):
33
  return filled_str
34
 
35
 
36
- class Logger:
37
  LOG_METHODS = {
38
  "err": ("error", "red"),
39
  "warn": ("warning", "light_red"),
@@ -75,8 +75,8 @@ class Logger:
75
  module = inspect.getmodule(frame[0])
76
  name = module.__name__
77
 
78
- self.logger = logging.getLogger(name)
79
- self.logger.setLevel(logging.INFO)
80
 
81
  if prefix:
82
  formatter_prefix = "[%(asctime)s] - [%(name)s] - [%(levelname)s]\n"
@@ -85,11 +85,10 @@ class Logger:
85
 
86
  self.formatter = logging.Formatter(formatter_prefix + "%(message)s")
87
 
88
- self.handler = logging.StreamHandler()
89
- self.handler.setLevel(logging.INFO)
90
-
91
- self.handler.setFormatter(self.formatter)
92
- self.logger.addHandler(self.handler)
93
 
94
  self.log_indent = 0
95
  self.log_indents = []
@@ -97,8 +96,6 @@ class Logger:
97
  self.log_level = "info"
98
  self.log_levels = []
99
 
100
- self.bind_functions()
101
-
102
  def indent(self, indent=2):
103
  self.log_indent += indent
104
 
@@ -116,7 +113,7 @@ class Logger:
116
 
117
  def set_level(self, level):
118
  self.log_level = level
119
- self.logger.setLevel(self.LEVEL_NAMES[level])
120
 
121
  def store_level(self):
122
  self.log_levels.append(self.log_level)
@@ -139,7 +136,8 @@ class Logger:
139
 
140
  def log(
141
  self,
142
- method,
 
143
  msg,
144
  indent=0,
145
  fill=False,
@@ -162,24 +160,44 @@ class Logger:
162
  if fill:
163
  indented_msg = add_fillers(indented_msg, fill_side=fill_side)
164
 
165
- handler = self.logger.handlers[0]
166
  handler.terminator = end
167
 
168
- level, color = self.LOG_METHODS[method]
169
- getattr(self.logger, level)(colored(indented_msg, color), *args, **kwargs)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
170
 
171
- def bind_functions(self):
172
- for method in self.LOG_METHODS:
173
- setattr(self.logger, method, functools.partial(self.log, method))
174
 
175
- for method in self.INDENT_METHODS:
176
- setattr(self.logger, method, getattr(self, method))
177
 
178
- for method in self.LEVEL_METHODS:
179
- setattr(self.logger, method, getattr(self, method))
180
 
181
 
182
- logger = Logger().logger
183
 
184
 
185
  def shell_cmd(cmd, getoutput=False, showcmd=True, env=None):
 
33
  return filled_str
34
 
35
 
36
+ class OSLogger(logging.Logger):
37
  LOG_METHODS = {
38
  "err": ("error", "red"),
39
  "warn": ("warning", "light_red"),
 
75
  module = inspect.getmodule(frame[0])
76
  name = module.__name__
77
 
78
+ super().__init__(name)
79
+ self.setLevel(logging.INFO)
80
 
81
  if prefix:
82
  formatter_prefix = "[%(asctime)s] - [%(name)s] - [%(levelname)s]\n"
 
85
 
86
  self.formatter = logging.Formatter(formatter_prefix + "%(message)s")
87
 
88
+ stream_handler = logging.StreamHandler()
89
+ stream_handler.setLevel(logging.INFO)
90
+ stream_handler.setFormatter(self.formatter)
91
+ self.addHandler(stream_handler)
 
92
 
93
  self.log_indent = 0
94
  self.log_indents = []
 
96
  self.log_level = "info"
97
  self.log_levels = []
98
 
 
 
99
  def indent(self, indent=2):
100
  self.log_indent += indent
101
 
 
113
 
114
  def set_level(self, level):
115
  self.log_level = level
116
+ self.setLevel(self.LEVEL_NAMES[level])
117
 
118
  def store_level(self):
119
  self.log_levels.append(self.log_level)
 
136
 
137
  def log(
138
  self,
139
+ level,
140
+ color,
141
  msg,
142
  indent=0,
143
  fill=False,
 
160
  if fill:
161
  indented_msg = add_fillers(indented_msg, fill_side=fill_side)
162
 
163
+ handler = self.handlers[0]
164
  handler.terminator = end
165
 
166
+ getattr(self, level)(colored(indented_msg, color), *args, **kwargs)
167
+
168
+ def route_log(self, method, msg, *args, **kwargs):
169
+ level, method = method
170
+ functools.partial(self.log, level, method, msg)(*args, **kwargs)
171
+
172
+ def err(self, msg: str = "", *args, **kwargs):
173
+ self.route_log(("error", "red"), msg, *args, **kwargs)
174
+
175
+ def warn(self, msg: str = "", *args, **kwargs):
176
+ self.route_log(("warning", "light_red"), msg, *args, **kwargs)
177
+
178
+ def note(self, msg: str = "", *args, **kwargs):
179
+ self.route_log(("info", "light_magenta"), msg, *args, **kwargs)
180
+
181
+ def mesg(self, msg: str = "", *args, **kwargs):
182
+ self.route_log(("info", "light_cyan"), msg, *args, **kwargs)
183
+
184
+ def file(self, msg: str = "", *args, **kwargs):
185
+ self.route_log(("info", "light_blue"), msg, *args, **kwargs)
186
+
187
+ def line(self, msg: str = "", *args, **kwargs):
188
+ self.route_log(("info", "white"), msg, *args, **kwargs)
189
 
190
+ def success(self, msg: str = "", *args, **kwargs):
191
+ self.route_log(("info", "light_green"), msg, *args, **kwargs)
 
192
 
193
+ def fail(self, msg: str = "", *args, **kwargs):
194
+ self.route_log(("info", "light_red"), msg, *args, **kwargs)
195
 
196
+ def back(self, msg: str = "", *args, **kwargs):
197
+ self.route_log(("debug", "light_cyan"), msg, *args, **kwargs)
198
 
199
 
200
+ logger = OSLogger()
201
 
202
 
203
  def shell_cmd(cmd, getoutput=False, showcmd=True, env=None):