Skip to content

Commit

Permalink
clear/erase console line before logging by default
Browse files Browse the repository at this point in the history
By default, clear the current terminal line, before printing the log message.
  • Loading branch information
HeinrichAD committed Jul 29, 2024
1 parent 80e566b commit fa240ee
Show file tree
Hide file tree
Showing 4 changed files with 5 additions and 3 deletions.
2 changes: 1 addition & 1 deletion dlr/ki/logging/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ def get_default_logging_dict(log_filepath: Optional[str] = None, ensure_log_dir:
"formatters": {
"console_formatter": {
"class": "dlr.ki.logging.formatter.colored_console.ColoredConsoleFormatter",
"format": "[%(levelcolor)s%(levelname)-8s%(colorreset)s] %(ansi.fg.grey)s[%(name)s][%(filename)s:%(lineno)d]%(ansi.fx.reset)s %(message)s" # noqa: E501
"format": "%(clearline)s[%(levelcolor)s%(levelname)-8s%(colorreset)s] %(ansi.fg.grey)s[%(name)s][%(filename)s:%(lineno)d]%(ansi.fx.reset)s %(message)s" # noqa: E501
},
"file_formatter": {
"class": "dlr.ki.logging.formatter.term_escape_code.TermEscapeCodeFormatter",
Expand Down
2 changes: 2 additions & 0 deletions dlr/ki/logging/formatter/colored_console.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

from ansi.colour.base import Graphic as AnsiGraphic
import ansi.color as ansi
from ansi.cursor import erase_line
from copy import copy
from logging import Formatter # type: ignore [attr-defined]
from typing import Dict
Expand Down Expand Up @@ -50,5 +51,6 @@ def format(self, record):
ColoredConsoleFormatter.MAPPING["DEBUG"]
)
colored_record.colorreset = ansi.fx.reset
colored_record.clearline = erase_line() + "\r"
colored_record.__dict__.update(self.ansi_dict)
return super().format(colored_record)
2 changes: 1 addition & 1 deletion examples/logging.conf
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ keys=consoleFormatter,fileFormatter

[formatter_consoleFormatter]
class=dlr.ki.logging.formatter.colored_console.ColoredConsoleFormatter
format=[%(levelcolor)s%(levelname)-8s%(colorreset)s] %(ansi.fg.grey)s[%(name)s][%(filename)s:%(lineno)d]%(ansi.fx.reset)s %(message)s
format=%(clearline)s[%(levelcolor)s%(levelname)-8s%(colorreset)s] %(ansi.fg.grey)s[%(name)s][%(filename)s:%(lineno)d]%(ansi.fx.reset)s %(message)s

[formatter_fileFormatter]
class=dlr.ki.logging.formatter.term_escape_code.TermEscapeCodeFormatter
Expand Down
2 changes: 1 addition & 1 deletion examples/logging.yml
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ handlers:
formatters:
console_formatter:
class: dlr.ki.logging.formatter.colored_console.ColoredConsoleFormatter
format: '[%(levelcolor)s%(levelname)-8s%(colorreset)s] %(ansi.fg.grey)s[%(name)s][%(filename)s:%(lineno)d]%(ansi.fx.reset)s %(message)s'
format: '%(clearline)s[%(levelcolor)s%(levelname)-8s%(colorreset)s] %(ansi.fg.grey)s[%(name)s][%(filename)s:%(lineno)d]%(ansi.fx.reset)s %(message)s'
file_formatter:
class: dlr.ki.logging.formatter.term_escape_code.TermEscapeCodeFormatter
format: '%(asctime)s [%(levelname)-8s] [%(name)s][%(filename)s:%(lineno)d] %(message)s'

0 comments on commit fa240ee

Please sign in to comment.