跳到主要内容

openhands.core.logger

NoColorFormatter Objects

class NoColorFormatter(logging.Formatter)

Formatter for non-colored logging in files.

strip_ansi

def strip_ansi(s: str) -> str

Remove ANSI escape sequences (terminal color/formatting codes) from string.

Removes ANSI escape sequences from str, as defined by ECMA-048 in http://www.ecma-international.org/publications/files/ECMA-ST/Ecma-048.pdf https://github.com/ewen-lbh/python-strip-ansi/blob/master/strip_ansi/__init__.py

RollingLogger Objects

class RollingLogger()
def print_lines() -> None

Display the last n log_lines in the console (not for file logging).

This will create the effect of a rolling display in the console.

move_back

def move_back(amount: int = -1) -> None

'\033[F' moves the cursor up one line.

replace_current_line

def replace_current_line(line: str = '') -> None

'\033[2K\r' clears the line and moves the cursor to the beginning of the line.

get_console_handler

def get_console_handler(
log_level: int = logging.INFO) -> logging.StreamHandler

Returns a console handler for logging.

get_file_handler

def get_file_handler(log_dir: str,
log_level: int = logging.INFO) -> logging.FileHandler

Returns a file handler for logging.

json_log_handler

def json_log_handler(level: int = logging.INFO,
_out: TextIO = sys.stdout) -> logging.Handler

Configure logger instance for structured logging as json lines.

log_uncaught_exceptions

def log_uncaught_exceptions(ex_cls: type[BaseException], ex: BaseException,
tb: TracebackType | None) -> Any

Logs uncaught exceptions along with the traceback.

Arguments:

  • ex_cls - The type of the exception.
  • ex - The exception instance.
  • tb - The traceback object.

Returns:

None

LlmFileHandler Objects

class LlmFileHandler(logging.FileHandler)

LLM prompt and response logging.

__init__

def __init__(filename: str,
mode: str = 'a',
encoding: str = 'utf-8',
delay: bool = False) -> None

Initializes an instance of LlmFileHandler.

Arguments:

  • filename str - The name of the log file.
  • mode str, optional - The file mode. Defaults to 'a'.
  • encoding str, optional - The file encoding. Defaults to None.
  • delay bool, optional - Whether to delay file opening. Defaults to False.

emit

def emit(record: logging.LogRecord) -> None

Emits a log record.

Arguments:

  • record logging.LogRecord - The log record to emit.

OpenHandsLoggerAdapter Objects

class OpenHandsLoggerAdapter(logging.LoggerAdapter)

process

def process(msg, kwargs)

If 'extra' is supplied in kwargs, merge it with the adapters 'extra' dict Starting in Python 3.13, LoggerAdapter's merge_extra option will do this.