Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

logging.basicConfig throwing ValueError Unrecognised arguments

logging.basicConfig(filename='example.log', level=logging.DEBUG, encoding='utf-8')

When I try the above code I get this error

 File "/Library/Developer/CommandLineTools/Library/Frameworks/Python3.framework/Versions/3.8/lib/python3.8/logging/__init__.py", line 2009, in basicConfig
    raise ValueError('Unrecognised argument(s): %s' % keys)
ValueError: Unrecognised argument(s): encoding

I tried going through the docs only to find that encoding is a valid argument for basicConfig().

Can someone explain?

like image 371
prat2194 Avatar asked Feb 03 '26 15:02

prat2194


1 Answers

From the error message, I can assume that you're using Python 3.8, which means that you should stick to the keywords that are supported by this version: filename, filemode, format, datefmt, style, level, stream, handlers, force.

The encoding keyword was added in Python 3.9

To explicitly define the encoding of a log file in Python 3.8, we can use a file handler:

log_handler = logging.FileHandler(filename='example.log', encoding='utf-8')
logging.basicConfig(handlers=[log_handler], level=logging.DEBUG)
like image 89
Vitalizzare Avatar answered Feb 06 '26 07:02

Vitalizzare



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!