Easier Debug Mode¶
The dataclass-wizard library provides a convenient way to enable logging for debugging. While one approach is to enable the debug_enabled flag in JSONWizard.Meta, this requires proper setup of the logging module, as shown below:
import logging
from dataclasses import dataclass
from dataclass_wizard import JSONWizard
# Manually set logging level
logging.basicConfig(level=logging.DEBUG)
@dataclass
class MyClass(JSONWizard):
class _(JSONWizard.Meta):
debug_enabled = True
Simpler Debugging with debug¶
A simpler and more flexible approach is to pass the debug argument directly when subclassing JSONWizard. This not only sets the logging.basicConfig(level=logging.DEBUG) automatically, but also lets you customize the log level by passing a value like logging.INFO or logging.DEBUG:
import logging
from dataclasses import dataclass
from dataclass_wizard import JSONWizard
@dataclass
class MyClass(JSONWizard, debug=logging.INFO):
class _(JSONWizard.Meta):
skip_defaults = True
key_transform_with_dump = 'PASCAL'
my_bool: bool
my_int: int = 2
@classmethod
def _pre_from_dict(cls, o):
o['myBool'] = True
return o
# Setting `debug=logging.INFO` automatically configures the logger:
# logging.getLogger('dataclass_wizard').setLevel(logging.INFO)
c = MyClass.from_dict({'myBool': 'false'})
print(c)
# {
# "MyBool": true
# }
Key Points¶
Automatic Logging Setup: When
debug=True(ordebug=logging.DEBUG, etc.),logging.basicConfig(level=logging.DEBUG)is automatically configured for the library.Custom Log Levels: - Pass a boolean (
True) to enableDEBUGlevel logs. - Pass a logging level (e.g.,logging.INFO,logging.WARNING) to set a custom log level.This internally maps to
JSONWizard.Meta.debug_enabled, configuring the library’s logger with the specified level.Library Logger: The library logger (
dataclass_wizard) is dynamically set vialogging.getLogger('dataclass_wizard').setLevel(input_level)based on thedebugargument.Convenient Defaults: No need to manually configure
logging.basicConfigor adjust log levels outside your class definition.
Examples of Log Levels¶
import logging
from dataclasses import dataclass
from dataclass_wizard import JSONWizard
@dataclass
class DebugExample(JSONWizard, debug=True):
... # DEBUG level (default for boolean True)
@dataclass
class InfoExample(JSONWizard, debug="INFO"):
... # INFO level
@dataclass
class WarningExample(JSONWizard, debug=logging.WARNING):
... # WARNING level