structlog icon indicating copy to clipboard operation
structlog copied to clipboard

dev.ConsoleRenderer modifies my own style variable

Open afeblot opened this issue 1 year ago • 0 comments

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)

afeblot avatar Aug 31 '24 19:08 afeblot