@@ -1524,6 +1524,32 @@ class ConfigFileTest(BaseTest):
1524
1524
kwargs={{"encoding": "utf-8"}}
1525
1525
"""
1526
1526
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
+
1527
1553
disable_test = """
1528
1554
[loggers]
1529
1555
keys=root
@@ -1687,6 +1713,16 @@ def test_config8_ok(self):
1687
1713
handler = logging .root .handlers [0 ]
1688
1714
self .addCleanup (closeFileHandler , handler , fn )
1689
1715
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
+
1690
1726
def test_logger_disabling (self ):
1691
1727
self .apply_config (self .disable_test )
1692
1728
logger = logging .getLogger ('some_pristine_logger' )
@@ -2909,6 +2945,30 @@ class ConfigDictTest(BaseTest):
2909
2945
},
2910
2946
}
2911
2947
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
+
2912
2972
bad_format = {
2913
2973
"version" : 1 ,
2914
2974
"formatters" : {
@@ -3021,7 +3081,7 @@ class ConfigDictTest(BaseTest):
3021
3081
}
3022
3082
}
3023
3083
3024
- # Configuration with custom function and 'validate' set to False
3084
+ # Configuration with custom function, 'validate' set to False and no defaults
3025
3085
custom_formatter_with_function = {
3026
3086
'version' : 1 ,
3027
3087
'formatters' : {
@@ -3048,6 +3108,33 @@ class ConfigDictTest(BaseTest):
3048
3108
}
3049
3109
}
3050
3110
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
+
3051
3138
config_queue_handler = {
3052
3139
'version' : 1 ,
3053
3140
'handlers' : {
@@ -3349,6 +3436,22 @@ def test_config15_ok(self):
3349
3436
handler = logging .root .handlers [0 ]
3350
3437
self .addCleanup (closeFileHandler , handler , fn )
3351
3438
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
+
3352
3455
def setup_via_listener (self , text , verify = None ):
3353
3456
text = text .encode ("utf-8" )
3354
3457
# Ask for a randomly assigned port (by using port 0)
@@ -3516,6 +3619,9 @@ def test_custom_formatter_class_with_validate3(self):
3516
3619
def test_custom_formatter_function_with_validate (self ):
3517
3620
self .assertRaises (ValueError , self .apply_config , self .custom_formatter_with_function )
3518
3621
3622
+ def test_custom_formatter_function_with_defaults (self ):
3623
+ self .assertRaises (ValueError , self .apply_config , self .custom_formatter_with_defaults )
3624
+
3519
3625
def test_baseconfig (self ):
3520
3626
d = {
3521
3627
'atuple' : (1 , 2 , 3 ),
0 commit comments