@@ -1524,6 +1524,32 @@ class ConfigFileTest(BaseTest):
15241524 kwargs={{"encoding": "utf-8"}}
15251525 """
15261526
1527+
1528+ config9 = """
1529+ [loggers]
1530+ keys=root
1531+
1532+ [handlers]
1533+ keys=hand1
1534+
1535+ [formatters]
1536+ keys=form1
1537+
1538+ [logger_root]
1539+ level=WARNING
1540+ handlers=hand1
1541+
1542+ [handler_hand1]
1543+ class=StreamHandler
1544+ level=NOTSET
1545+ formatter=form1
1546+ args=(sys.stdout,)
1547+
1548+ [formatter_form1]
1549+ format=%(message)s ++ %(customfield)s
1550+ defaults={"customfield": "defaultvalue"}
1551+ """
1552+
15271553 disable_test = """
15281554 [loggers]
15291555 keys=root
@@ -1687,6 +1713,16 @@ def test_config8_ok(self):
16871713 handler = logging .root .handlers [0 ]
16881714 self .addCleanup (closeFileHandler , handler , fn )
16891715
1716+ def test_config9_ok (self ):
1717+ self .apply_config (self .config9 )
1718+ formatter = logging .root .handlers [0 ].formatter
1719+ result = formatter .format (logging .makeLogRecord ({'msg' : 'test' }))
1720+ self .assertEqual (result , 'test ++ defaultvalue' )
1721+ result = formatter .format (logging .makeLogRecord (
1722+ {'msg' : 'test' , 'customfield' : "customvalue" }))
1723+ self .assertEqual (result , 'test ++ customvalue' )
1724+
1725+
16901726 def test_logger_disabling (self ):
16911727 self .apply_config (self .disable_test )
16921728 logger = logging .getLogger ('some_pristine_logger' )
@@ -2909,6 +2945,30 @@ class ConfigDictTest(BaseTest):
29092945 },
29102946 }
29112947
2948+ # config0 but with default values for formatter. Skipped 15, it is defined
2949+ # in the test code.
2950+ config16 = {
2951+ 'version' : 1 ,
2952+ 'formatters' : {
2953+ 'form1' : {
2954+ 'format' : '%(message)s ++ %(customfield)s' ,
2955+ 'defaults' : {"customfield" : "defaultvalue" }
2956+ },
2957+ },
2958+ 'handlers' : {
2959+ 'hand1' : {
2960+ 'class' : 'logging.StreamHandler' ,
2961+ 'formatter' : 'form1' ,
2962+ 'level' : 'NOTSET' ,
2963+ 'stream' : 'ext://sys.stdout' ,
2964+ },
2965+ },
2966+ 'root' : {
2967+ 'level' : 'WARNING' ,
2968+ 'handlers' : ['hand1' ],
2969+ },
2970+ }
2971+
29122972 bad_format = {
29132973 "version" : 1 ,
29142974 "formatters" : {
@@ -3021,7 +3081,7 @@ class ConfigDictTest(BaseTest):
30213081 }
30223082 }
30233083
3024- # Configuration with custom function and 'validate' set to False
3084+ # Configuration with custom function, 'validate' set to False and no defaults
30253085 custom_formatter_with_function = {
30263086 'version' : 1 ,
30273087 'formatters' : {
@@ -3048,6 +3108,33 @@ class ConfigDictTest(BaseTest):
30483108 }
30493109 }
30503110
3111+ # Configuration with custom function, and defaults
3112+ custom_formatter_with_defaults = {
3113+ 'version' : 1 ,
3114+ 'formatters' : {
3115+ 'form1' : {
3116+ '()' : formatFunc ,
3117+ 'format' : '%(levelname)s:%(name)s:%(message)s:%(customfield)s' ,
3118+ 'defaults' : {"customfield" : "myvalue" }
3119+ },
3120+ },
3121+ 'handlers' : {
3122+ 'hand1' : {
3123+ 'class' : 'logging.StreamHandler' ,
3124+ 'formatter' : 'form1' ,
3125+ 'level' : 'NOTSET' ,
3126+ 'stream' : 'ext://sys.stdout' ,
3127+ },
3128+ },
3129+ "loggers" : {
3130+ "my_test_logger_custom_formatter" : {
3131+ "level" : "DEBUG" ,
3132+ "handlers" : ["hand1" ],
3133+ "propagate" : "true"
3134+ }
3135+ }
3136+ }
3137+
30513138 config_queue_handler = {
30523139 'version' : 1 ,
30533140 'handlers' : {
@@ -3349,6 +3436,22 @@ def test_config15_ok(self):
33493436 handler = logging .root .handlers [0 ]
33503437 self .addCleanup (closeFileHandler , handler , fn )
33513438
3439+ def test_config16_ok (self ):
3440+ self .apply_config (self .config16 )
3441+ h = logging ._handlers ['hand1' ]
3442+
3443+ # Custom value
3444+ result = h .formatter .format (logging .makeLogRecord (
3445+ {'msg' : 'Hello' , 'customfield' : 'customvalue' }))
3446+ self .assertEqual (result , 'Hello ++ customvalue' )
3447+
3448+ # Default value
3449+ result = h .formatter .format (logging .makeLogRecord (
3450+ {'msg' : 'Hello' }))
3451+ self .assertEqual (result , 'Hello ++ defaultvalue' )
3452+
3453+
3454+
33523455 def setup_via_listener (self , text , verify = None ):
33533456 text = text .encode ("utf-8" )
33543457 # Ask for a randomly assigned port (by using port 0)
@@ -3516,6 +3619,9 @@ def test_custom_formatter_class_with_validate3(self):
35163619 def test_custom_formatter_function_with_validate (self ):
35173620 self .assertRaises (ValueError , self .apply_config , self .custom_formatter_with_function )
35183621
3622+ def test_custom_formatter_function_with_defaults (self ):
3623+ self .assertRaises (ValueError , self .apply_config , self .custom_formatter_with_defaults )
3624+
35193625 def test_baseconfig (self ):
35203626 d = {
35213627 'atuple' : (1 , 2 , 3 ),
0 commit comments