File tree 2 files changed +36
-3
lines changed
2 files changed +36
-3
lines changed Original file line number Diff line number Diff line change @@ -50,8 +50,8 @@ listed below.
50
50
Logger Objects
51
51
--------------
52
52
53
- Loggers have the following attributes and methods. Note that Loggers are never
54
- instantiated directly, but always through the module-level function
53
+ Loggers have the following attributes and methods. Note that Loggers should
54
+ * NEVER * be instantiated directly, but always through the module-level function
55
55
``logging.getLogger(name) ``. Multiple calls to :func: `getLogger ` with the same
56
56
name will always return a reference to the same Logger object.
57
57
@@ -1191,7 +1191,9 @@ functions.
1191
1191
The class should define :meth: `__init__ ` such that only a name argument is
1192
1192
required, and the :meth: `__init__ ` should call :meth: `Logger.__init__ `. This
1193
1193
function is typically called before any loggers are instantiated by applications
1194
- which need to use custom logger behavior.
1194
+ which need to use custom logger behavior. After this call, as at any other
1195
+ time, do not instantiate loggers directly using the subclass: continue to use
1196
+ the :func: `logging.getLogger ` API to get your loggers.
1195
1197
1196
1198
1197
1199
.. function :: setLogRecordFactory(factory)
Original file line number Diff line number Diff line change @@ -3877,6 +3877,37 @@ class MyLogger(logging.Logger):
3877
3877
logging .setLoggerClass (logging .Logger )
3878
3878
self .assertEqual (logging .getLoggerClass (), logging .Logger )
3879
3879
3880
+ def test_subclass_logger_cache (self ):
3881
+ # bpo-37258
3882
+ message = []
3883
+
3884
+ class MyLogger (logging .getLoggerClass ()):
3885
+ def __init__ (self , name = 'MyLogger' , level = logging .NOTSET ):
3886
+ super ().__init__ (name , level )
3887
+ message .append ('initialized' )
3888
+
3889
+ logging .setLoggerClass (MyLogger )
3890
+ logger = logging .getLogger ('just_some_logger' )
3891
+ self .assertEqual (message , ['initialized' ])
3892
+ stream = io .StringIO ()
3893
+ h = logging .StreamHandler (stream )
3894
+ logger .addHandler (h )
3895
+ try :
3896
+ logger .setLevel (logging .DEBUG )
3897
+ logger .debug ("hello" )
3898
+ self .assertEqual (stream .getvalue ().strip (), "hello" )
3899
+
3900
+ stream .truncate (0 )
3901
+ stream .seek (0 )
3902
+
3903
+ logger .setLevel (logging .INFO )
3904
+ logger .debug ("hello" )
3905
+ self .assertEqual (stream .getvalue (), "" )
3906
+ finally :
3907
+ logger .removeHandler (h )
3908
+ h .close ()
3909
+ logging .setLoggerClass (logging .Logger )
3910
+
3880
3911
@support .requires_type_collecting
3881
3912
def test_logging_at_shutdown (self ):
3882
3913
# Issue #20037
You can’t perform that action at this time.
0 commit comments