def log_to_history(logger, name):
    """Decorate function, adding a logger handler stored in FITS."""

    def log_to_history_decorator(method):

        def l2h_method(self, ri):
            history_header = fits.Header()

            fh = FITSHistoryHandler(history_header)
            fh.setLevel(logging.INFO)
            logger.addHandler(fh)

            try:
                result = method(self, ri)
                field = getattr(result, name, None)
                if field:
                    with field.open() as hdulist:
                        hdr = hdulist[0].header
                        hdr.extend(history_header.cards)
                return result
            finally:
                logger.removeHandler(fh)
        return l2h_method

    return log_to_history_decorator