hynek / structlog

Simple, powerful, and fast logging for Python.
https://www.structlog.org/
Other
3.6k stars 225 forks source link

dev.ConsoleRenderer modifies my own style variable #643

Closed afeblot closed 3 weeks ago

afeblot commented 3 months ago

Is it expected that dev.ConsoleRenderer changes the value of my style variable passed with the level_styles argument, instead of copying it and modifying its own copy?

Demo:

import structlog

my_styles = structlog.dev.ConsoleRenderer.get_default_level_styles()

def check():
  structlog.dev.ConsoleRenderer(level_styles=my_styles)
  print(my_styles)

check()
check()
check()
check()

Output:

{'critical': '\x1b[31m\x1b[1m', 'exception': '\x1b[31m\x1b[1m', 'error': '\x1b[31m\x1b[1m', 'warn': '\x1b[33m\x1b[1m', 'warning': '\x1b[33m\x1b[1m', 'info': '\x1b[32m\x1b[1m', 'debug': '\x1b[32m\x1b[1m', 'notset': '\x1b[41m\x1b[1m'}
{'critical': '\x1b[31m\x1b[1m\x1b[1m', 'exception': '\x1b[31m\x1b[1m\x1b[1m', 'error': '\x1b[31m\x1b[1m\x1b[1m', 'warn': '\x1b[33m\x1b[1m\x1b[1m', 'warning': '\x1b[33m\x1b[1m\x1b[1m', 'info': '\x1b[32m\x1b[1m\x1b[1m', 'debug': '\x1b[32m\x1b[1m\x1b[1m', 'notset': '\x1b[41m\x1b[1m\x1b[1m'}
{'critical': '\x1b[31m\x1b[1m\x1b[1m\x1b[1m', 'exception': '\x1b[31m\x1b[1m\x1b[1m\x1b[1m', 'error': '\x1b[31m\x1b[1m\x1b[1m\x1b[1m', 'warn': '\x1b[33m\x1b[1m\x1b[1m\x1b[1m', 'warning': '\x1b[33m\x1b[1m\x1b[1m\x1b[1m', 'info': '\x1b[32m\x1b[1m\x1b[1m\x1b[1m', 'debug': '\x1b[32m\x1b[1m\x1b[1m\x1b[1m', 'notset': '\x1b[41m\x1b[1m\x1b[1m\x1b[1m'}
{'critical': '\x1b[31m\x1b[1m\x1b[1m\x1b[1m\x1b[1m', 'exception': '\x1b[31m\x1b[1m\x1b[1m\x1b[1m\x1b[1m', 'error': '\x1b[31m\x1b[1m\x1b[1m\x1b[1m\x1b[1m', 'warn': '\x1b[33m\x1b[1m\x1b[1m\x1b[1m\x1b[1m', 'warning': '\x1b[33m\x1b[1m\x1b[1m\x1b[1m\x1b[1m', 'info': '\x1b[32m\x1b[1m\x1b[1m\x1b[1m\x1b[1m', 'debug': '\x1b[32m\x1b[1m\x1b[1m\x1b[1m\x1b[1m', 'notset': '\x1b[41m\x1b[1m\x1b[1m\x1b[1m\x1b[1m'}

(I'm calling structlog.configure(), in which I construct the dev.ConsoleRenderer, and I do this multiple times in my unittests, whereas my_styles is a global variable, and supposed to remain constant)

hynek commented 3 weeks ago

I believe #669 fixed this; feel free to comment/reopen if not.

afeblot commented 3 weeks ago

Hi Hynek,

Yes, it does, thanks a lot. I also had to update a couple of tests due to https://github.com/hynek/structlog/issues/638