Skip to content

Commit

Permalink
Merge pull request #363 from openego/bug/fix_logging_add_grid_id
Browse files Browse the repository at this point in the history
Fix bug in logging add grid id.
  • Loading branch information
birgits committed Mar 24, 2023
2 parents 2966e3c + 389fd52 commit e04a4d8
Showing 1 changed file with 7 additions and 18 deletions.
25 changes: 7 additions & 18 deletions edisgo/edisgo.py
Original file line number Diff line number Diff line change
Expand Up @@ -166,26 +166,15 @@ def add_grid_id_filter(record):
record.grid_id = self.topology.id
return True

file_formatter = logging.Formatter(
"%(asctime)s - %(name)s - %(levelname)s - "
"MVGrid(%(grid_id)s): %(message)s"
)
stream_formatter = logging.Formatter(
"%(name)s - %(levelname)s - MVGrid(%(grid_id)s): %(message)s"
)

logger_edisgo = logging.getLogger("edisgo")
for handler in logger_edisgo.handlers:
if isinstance(logger_edisgo.handlers[0], logging.StreamHandler):
handler.setFormatter(stream_formatter)
elif isinstance(logger_edisgo.handlers[0], logging.FileHandler):
handler.setFormatter(file_formatter)
else:
raise ValueError(
"Disable the log_grid_id function when using other"
" handlers than StreamHandler or FileHandler"
)
handler.filters.clear()
fmt = handler.formatter._fmt
colon_idx = fmt.index(":")
formatter_str = (
f"{fmt[:colon_idx]} - MVGrid(%(grid_id)s){fmt[colon_idx:]}"
)
formatter = logging.Formatter(formatter_str)
handler.setFormatter(formatter)
handler.addFilter(add_grid_id_filter)

@property
Expand Down

0 comments on commit e04a4d8

Please sign in to comment.