Skip to content

Instantly share code, notes, and snippets.

@geryxyz
Created September 9, 2022 14:33
Show Gist options
  • Save geryxyz/9d9f2f4da596d94abd3f257d962b2c64 to your computer and use it in GitHub Desktop.
Save geryxyz/9d9f2f4da596d94abd3f257d962b2c64 to your computer and use it in GitHub Desktop.
import logging
from typing import Callable
class LambdaFormatter(logging.Formatter):
def __init__(
self,
fmt,
pre_format_func: Callable[[logging.LogRecord], logging.LogRecord] = lambda text: text,
post_format_func: Callable[[str, logging.LogRecord], str] = lambda text, _: text
):
super().__init__(fmt)
self.pre_format_func = pre_format_func
self.post_format_func = post_format_func
def format(self, record: logging.LogRecord) -> str:
log_fmt = self.post_format_func(super().format(self.pre_format_func(record)), record)
return log_fmt
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment