Skip to content

Commit f9bab23

Browse files
authored
Merge pull request #50 from r1tar/refactor/simplify-log-conditionals
refactor: simplify log conditionals and replace unnecessary f-strings
2 parents d8bd2e3 + 32d4858 commit f9bab23

1 file changed

Lines changed: 16 additions & 19 deletions

File tree

‎VersaLog/core.py‎

Lines changed: 16 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ def __init__(self, enum: str= "simple", tag: Optional[Union[str, List[str]]]= No
8787
if self.save_levels is None:
8888
self.save_levels = self.valid_save_levels.copy()
8989
elif not isinstance(self.save_levels, list):
90-
raise ValueError(f"save_levels must be a list. Example: ['ERROR']")
90+
raise ValueError("save_levels must be a list. Example: ['ERROR']")
9191
elif not all(level in self.valid_save_levels for level in self.save_levels):
9292
raise ValueError(f"Invalid save_levels specified. Valid levels are: {', '.join(self.valid_save_levels)}")
9393

@@ -179,42 +179,39 @@ def _Log(self, msg: str, tye: str, tag: Optional[Union[str, List[str]]] = None)
179179
tags = [final_tag] if final_tag else []
180180
else:
181181
tags = []
182-
tag_str = "".join(f"[{t}]" for t in tags) if tags else ""
182+
tag_str = "".join(f"[{t}]" for t in tags)
183183

184184
caller = self._GetCaller() if self.show_file or self.enum == "file" else ""
185185

186186
if self.enum == "simple":
187187
symbol = self.SYMBOLS.get(tye, "[?]")
188188
if self.show_file:
189-
formatted = f"[{caller}]{tag_str}{colors}{symbol}{self.RESET} {msg}" if tag_str else f"[{caller}]{colors}{symbol}{self.RESET} {msg}"
190-
plain = f"[{caller}]{tag_str}{symbol} {msg}" if tag_str else f"[{caller}]{symbol} {msg}"
189+
# tag_str is empty string if no tags, otherwise formatted as "[tag1][tag2]..."
190+
formatted = f"[{caller}]{tag_str}{colors}{symbol}{self.RESET} {msg}"
191+
plain = f"[{caller}]{tag_str}{symbol} {msg}"
191192
else:
192-
formatted = f"{tag_str}{colors}{symbol}{self.RESET} {msg}" if tag_str else f"{colors}{symbol}{self.RESET} {msg}"
193-
plain = f"{tag_str}{symbol} {msg}" if tag_str else f"{symbol} {msg}"
193+
formatted = f"{tag_str}{colors}{symbol}{self.RESET} {msg}"
194+
plain = f"{tag_str}{symbol} {msg}"
194195

195196
elif self.enum == "simple2":
196197
symbol = self.SYMBOLS.get(tye, "[?]")
197198
time = self._GetTime()
198199
if self.show_file:
199-
formatted = f"[{time}] [{caller}]{tag_str}{colors}{symbol}{self.RESET} {msg}" if tag_str else f"[{time}] [{caller}]{colors}{symbol}{self.RESET} {msg}"
200-
plain = f"[{time}] [{caller}]{tag_str}{symbol} {msg}" if tag_str else f"[{time}] [{caller}]{symbol} {msg}"
200+
formatted = f"[{time}] [{caller}]{tag_str}{colors}{symbol}{self.RESET} {msg}"
201+
plain = f"[{time}] [{caller}]{tag_str}{symbol} {msg}"
201202
else:
202-
formatted = f"[{time}] {tag_str}{colors}{symbol}{self.RESET} {msg}" if tag_str else f"[{time}] {colors}{symbol}{self.RESET} {msg}"
203-
plain = f"[{time}] {tag_str}{symbol} {msg}" if tag_str else f"[{time}] {symbol} {msg}"
203+
formatted = f"[{time}] {tag_str}{colors}{symbol}{self.RESET} {msg}"
204+
plain = f"[{time}] {tag_str}{symbol} {msg}"
204205

205206
elif self.enum == "file":
206-
formatted = f"[{caller}]{tag_str}{colors}[{types}]{self.RESET} {msg}" if tag_str else f"[{caller}]{colors}[{types}]{self.RESET} {msg}"
207-
plain = f"[{caller}]{tag_str}[{types}] {msg}" if tag_str else f"[{caller}][{types}] {msg}"
207+
formatted = f"[{caller}]{tag_str}{colors}[{types}]{self.RESET} {msg}"
208+
plain = f"[{caller}]{tag_str}[{types}] {msg}"
208209

209210
else: # detailed
210211
time = self._GetTime()
211-
formatted = f"[{time}]{colors}[{types}]{self.RESET}{tag_str}" if tag_str else f"[{time}]{colors}[{types}]{self.RESET}"
212-
plain = f"[{time}][{types}]{tag_str}" if tag_str else f"[{time}][{types}]"
213-
if self.show_file:
214-
formatted += f"[{caller}]"
215-
plain += f"[{caller}]"
216-
formatted += f" : {msg}"
217-
plain += f" : {msg}"
212+
caller_part = f"[{caller}]" if self.show_file else ""
213+
formatted = f"[{time}]{colors}[{types}]{self.RESET}{tag_str}{caller_part} : {msg}"
214+
plain = f"[{time}][{types}]{tag_str}{caller_part} : {msg}"
218215

219216
if not self.silent:
220217
print(formatted)

0 commit comments

Comments
 (0)