Hacktoberfest 2026: los issues que los mantenedores marcaron para octubre, abiertos y aptos para principiantes. Explorar issues de Hacktoberfest

KeyError: "Attempt to overwrite 'filename' in LogRecord"

Abierto
#486 4 comentarios 1 reacción 0 asignados Ver en GitHub

Nadie ha tomado este issue todavía.

Evaluación

Dificultad
3/5
Tiempo estimado
1-2 días
Aptitud para principiantes
35/100
Tipo de issue
Error
Claridad
Bastante claro
Estado de actividad
Estancado
Stack tecnológico
python
Área
backend

Línea de trabajo

Start by reading the issue’s traceback and the CallsiteParameterAdder configuration shown in the body, especially how its filename field reaches Python’s logging call. Reproduce the failure with the provided Gunicorn and structlog setup, then check the relevant structlog tests for callsite parameters. Done means the setup no longer raises the LogRecord overwrite error and the existing tests still pass.

Escrito por el modelo de indexación a partir del texto del issue.

Descripción

stdlib

This is what I am using. Gunicorn + uvicorn app.

elog = structlog.get_logger("gunicorn.error", )
alog = structlog.get_logger("gunicorn.error", )


class GunicornLogger:

    def __init__(self, cfg):
        self.cfg = cfg
        self.error_log = elog.bind()
        self.access_log = alog.bind()
        logging.getLogger().setLevel(logging.DEBUG) # bit hacky but only required to emit logs

    def debug(self, msg, *args, **kwargs) -> None:
        self.error_log.debug(msg, *args, **kwargs)

Traceback (most recent call last):
  File "/usr/local/bin/gunicorn", line 8, in <module>
    sys.exit(run())
  File "/usr/local/lib/python3.10/site-packages/gunicorn/app/wsgiapp.py", line 67, in run
    WSGIApplication("%(prog)s [OPTIONS] [APP_MODULE]").run()
  File "/usr/local/lib/python3.10/site-packages/gunicorn/app/base.py", line 231, in run
    super().run()
  File "/usr/local/lib/python3.10/site-packages/gunicorn/app/base.py", line 72, in run
    Arbiter(self).run()
  File "/usr/local/lib/python3.10/site-packages/gunicorn/arbiter.py", line 58, in __init__
    self.setup(app)
  File "/usr/local/lib/python3.10/site-packages/gunicorn/arbiter.py", line 105, in setup
    self.log.debug('Current configuration:\n{0}'.format(
  File "/app/src/myapp/logs.py", line 80, in debug
    self.error_log.debug(msg, *args, **kwargs)
  File "/usr/local/lib/python3.10/site-packages/structlog/stdlib.py", line 180, in debug
    return self._proxy_to_logger("debug", event, *args, **kw)
  File "/usr/local/lib/python3.10/site-packages/structlog/stdlib.py", line 247, in _proxy_to_logger
    return super()._proxy_to_logger(method_name, event=event, **event_kw)
  File "/usr/local/lib/python3.10/site-packages/structlog/_base.py", line 206, in _proxy_to_logger
    return getattr(self._logger, method_name)(*args, **kw)
  File "/usr/local/lib/python3.10/logging/__init__.py", line 1465, in debug
    self._log(DEBUG, msg, args, **kwargs)
  File "/usr/local/lib/python3.10/logging/__init__.py", line 1622, in _log
    record = self.makeRecord(self.name, level, fn, lno, msg, args,
  File "/usr/local/lib/python3.10/logging/__init__.py", line 1596, in makeRecord
    raise KeyError("Attempt to overwrite %r in LogRecord" % key)
KeyError: "Attempt to overwrite 'filename' in LogRecord"
processors = [
    structlog.contextvars.merge_contextvars,
    # If log level is too low, abort pipeline and throw away log entry.
    structlog.stdlib.filter_by_level,
    # Add the name of the logger to event dict.
    structlog.stdlib.add_logger_name,
    # Add log level to event dict.
    structlog.stdlib.add_log_level,
    # Perform %-style formatting.
    structlog.stdlib.PositionalArgumentsFormatter(),
    # Add a timestamp in ISO 8601 format.
    structlog.processors.TimeStamper(fmt="iso"),
    # If the "stack_info" key in the event dict is true, remove it and
    # render the current stack trace in the "stack" key.
    structlog.processors.StackInfoRenderer(),
    # add exception frame as dict
    # structlog.processors.dict_tracebacks,
    # If the "exc_info" key in the event dict is either true or a
    # sys.exc_info() tuple, remove "exc_info" and render the exception
    # with traceback into the "exception" key.
    structlog.processors.format_exc_info,
    structlog.processors.ExceptionRenderer(),
    # Add callsite parameters.
    structlog.processors.CallsiteParameterAdder({
        structlog.processors.CallsiteParameter.FILENAME,
        structlog.processors.CallsiteParameter.FUNC_NAME,
        structlog.processors.CallsiteParameter.LINENO,
    }),
    # structlog.processors.JSONRenderer(),
    structlog.stdlib.render_to_log_kwargs,
]

structlog.configure(
    processors=processors,
    # `wrapper_class` is the bound logger that you get back from
    # get_logger(). This one imitates the API of `logging.Logger`.
    wrapper_class=structlog.stdlib.BoundLogger,
    # `logger_factory` is used to create wrapped loggers that are used for
    # OUTPUT. This one returns a `logging.Logger`. The final value (a JSON
    # string) from the final processor (`JSONRenderer`) will be passed to
    # the method of the same name as that you've called on the bound logger.
    logger_factory=structlog.stdlib.LoggerFactory(),
    # Effectively freeze configuration after creating the first bound
    # logger.
    cache_logger_on_first_use=True,
)
Lenguaje dominante
Python
Estrellas
5k
Forks
302
Merge medio
17 min
PR fusionados (30 d)
4

Preparar el entorno

Este proyecto no incluye contenedor de desarrollo, Dockerfile ni guía de contribución, así que la configuración corre por tu cuenta: empieza por su README y consulta nuestra guía para la primera contribución para los pasos generales.

Primeros pasos

  1. Lee el issue completo y luego la guía de contribución del proyecto.
  2. Comenta en el issue que vas a ocuparte — evita que dos personas hagan lo mismo.
  3. Haz un fork del repositorio y trabaja en una rama.
  4. Abre un pull request que haga referencia al número del issue.

Más de hynek/structlog

Todos los issues de hynek/structlog

Issues similares

Más issues de Python

Recibe los nuevos issues en tu correo

Un resumen breve de issues de GitHub para principiantes.