mirror of
https://github.com/huggingface/lerobot.git
synced 2026-07-30 04:59:44 +00:00
fix(utils): preserve exc_info/stack_info in init_logging formatter (#4215)
* fix(utils): preserve exc_info/stack_info in init_logging formatter Replacing Formatter.format dropped logging.exception() tracebacks, hurting HIL-SERL actor/learner crash diagnosis. Append formatted exceptions and stack_info like the stdlib formatter. Fixes #3978 * refactor(utils): format logging --------- Co-authored-by: Bartok9 <danielrpike9@gmail.com>
This commit is contained in:
@@ -24,7 +24,6 @@ import sys
|
|||||||
import time
|
import time
|
||||||
from collections.abc import Iterator
|
from collections.abc import Iterator
|
||||||
from copy import copy, deepcopy
|
from copy import copy, deepcopy
|
||||||
from datetime import datetime
|
|
||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
from statistics import mean
|
from statistics import mean
|
||||||
from typing import TYPE_CHECKING, Any
|
from typing import TYPE_CHECKING, Any
|
||||||
@@ -61,14 +60,16 @@ def init_logging(
|
|||||||
accelerator: Optional Accelerator instance (for multi-GPU detection)
|
accelerator: Optional Accelerator instance (for multi-GPU detection)
|
||||||
"""
|
"""
|
||||||
|
|
||||||
def custom_format(record: logging.LogRecord) -> str:
|
class LeRobotFormatter(logging.Formatter):
|
||||||
dt = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
|
def format(self, record: logging.LogRecord) -> str:
|
||||||
fnameline = f"{record.pathname}:{record.lineno}"
|
record.lerobot_location = f"{record.pathname}:{record.lineno}"[-15:]
|
||||||
pid_str = f"[PID: {os.getpid()}] " if display_pid else ""
|
record.lerobot_pid = f"[PID: {os.getpid()}] " if display_pid else ""
|
||||||
return f"{record.levelname} {pid_str}{dt} {fnameline[-15:]:>15} {record.getMessage()}"
|
return super().format(record)
|
||||||
|
|
||||||
formatter = logging.Formatter()
|
formatter = LeRobotFormatter(
|
||||||
formatter.format = custom_format
|
"%(levelname)s %(lerobot_pid)s%(asctime)s %(lerobot_location)15s %(message)s",
|
||||||
|
datefmt="%Y-%m-%d %H:%M:%S",
|
||||||
|
)
|
||||||
|
|
||||||
logger = logging.getLogger()
|
logger = logging.getLogger()
|
||||||
logger.setLevel(logging.NOTSET)
|
logger.setLevel(logging.NOTSET)
|
||||||
|
|||||||
Reference in New Issue
Block a user