@@ -500,6 +500,190 @@ def func():
500500 ]
501501 )
502502
503+ def test_disable_args (self , logger ):
504+ new_logger = mock .Mock (spec = logging .Logger , name = 'logger' )
505+ log = mock .Mock (name = 'log' )
506+ new_logger .attach_mock (log , 'log' )
507+
508+ arg1 = 'test arg 1'
509+ arg2 = 'test arg 2'
510+
511+ @logwrap .logwrap (log = new_logger , log_call_args = False )
512+ def func (test_arg1 , test_arg2 ):
513+ return test_arg1 , test_arg2
514+
515+ result = func (arg1 , arg2 )
516+ self .assertEqual (result , (arg1 , arg2 ))
517+ log .assert_has_calls ((
518+ mock .call (
519+ level = logging .DEBUG ,
520+ msg = "Calling: \n "
521+ "'func'()"
522+ ),
523+ mock .call (
524+ level = logging .DEBUG ,
525+ msg = "Done: 'func' with result:\n {}" .format (
526+ logwrap .pretty_repr (result ))
527+ ),
528+ ))
529+
530+ def test_disable_args_exc (self , logger ):
531+ new_logger = mock .Mock (spec = logging .Logger , name = 'logger' )
532+ log = mock .Mock (name = 'log' )
533+ new_logger .attach_mock (log , 'log' )
534+
535+ arg1 = 'test arg 1'
536+ arg2 = 'test arg 2'
537+
538+ @logwrap .logwrap (log = new_logger , log_call_args_on_exc = False )
539+ def func (test_arg1 , test_arg2 ):
540+ raise TypeError ('Blacklisted' )
541+
542+ with self .assertRaises (TypeError ):
543+ func (arg1 , arg2 )
544+
545+ self .assertEqual (len (logger .mock_calls ), 0 )
546+ log .assert_has_calls ((
547+ mock .call (
548+ level = logging .DEBUG ,
549+ msg = "Calling: \n "
550+ "'func'(\n "
551+ " # POSITIONAL_OR_KEYWORD:\n "
552+ " 'test_arg1'={},\n "
553+ " 'test_arg2'={},\n "
554+ ")" .format (
555+ logwrap .pretty_repr (
556+ arg1 ,
557+ indent = 8 ,
558+ no_indent_start = True
559+ ),
560+ logwrap .pretty_repr (
561+ arg2 ,
562+ indent = 8 ,
563+ no_indent_start = True
564+ ),
565+ )
566+ ),
567+ mock .call (
568+ level = logging .ERROR ,
569+ msg = "Failed: \n 'func'()" ,
570+ exc_info = True
571+ ),
572+ ))
573+
574+ def test_disable_all_args (self , logger ):
575+ new_logger = mock .Mock (spec = logging .Logger , name = 'logger' )
576+ log = mock .Mock (name = 'log' )
577+ new_logger .attach_mock (log , 'log' )
578+
579+ arg1 = 'test arg 1'
580+ arg2 = 'test arg 2'
581+
582+ @logwrap .logwrap (
583+ log = new_logger ,
584+ log_call_args = False ,
585+ log_call_args_on_exc = False
586+ )
587+ def func (test_arg1 , test_arg2 ):
588+ raise TypeError ('Blacklisted' )
589+
590+ with self .assertRaises (TypeError ):
591+ func (arg1 , arg2 )
592+
593+ self .assertEqual (len (logger .mock_calls ), 0 )
594+ log .assert_has_calls ((
595+ mock .call (
596+ level = logging .DEBUG ,
597+ msg = "Calling: \n "
598+ "'func'()"
599+ ),
600+ mock .call (
601+ level = logging .ERROR ,
602+ msg = "Failed: \n 'func'()" ,
603+ exc_info = True
604+ ),
605+ ))
606+
607+ def test_disable_result (self , logger ):
608+ new_logger = mock .Mock (spec = logging .Logger , name = 'logger' )
609+ log = mock .Mock (name = 'log' )
610+ new_logger .attach_mock (log , 'log' )
611+
612+ @logwrap .logwrap (log = new_logger , log_result_obj = False )
613+ def func ():
614+ return 'not logged'
615+
616+ func ()
617+
618+ self .assertEqual (len (logger .mock_calls ), 0 )
619+ self .assertEqual (
620+ log .mock_calls ,
621+ [
622+ mock .call (
623+ level = logging .DEBUG ,
624+ msg = "Calling: \n 'func'()"
625+ ),
626+ mock .call (
627+ level = logging .DEBUG ,
628+ msg = "Done: 'func'"
629+ ),
630+ ]
631+ )
632+
633+
634+ class TestObject (unittest .TestCase ):
635+ def test_basic (self ):
636+ log_call = logwrap .LogWrap ()
637+ self .assertEqual (log_call .log_level , logging .DEBUG )
638+ self .assertEqual (log_call .exc_level , logging .ERROR )
639+ self .assertEqual (log_call .max_indent , 20 )
640+ self .assertEqual (log_call .blacklisted_names , [])
641+ self .assertEqual (log_call .blacklisted_exceptions , [])
642+ self .assertTrue (log_call .log_call_args )
643+ self .assertTrue (log_call .log_call_args_on_exc )
644+ self .assertTrue (log_call .log_result_obj )
645+
646+ log_call .log_level = logging .INFO
647+ log_call .exc_level = logging .CRITICAL
648+ log_call .max_indent = 40
649+ log_call .blacklisted_names .append ('password' )
650+ log_call .blacklisted_exceptions .append (IOError )
651+ log_call .log_call_args = False
652+ log_call .log_call_args_on_exc = False
653+ log_call .log_result_obj = False
654+
655+ self .assertEqual (log_call .log_level , logging .INFO )
656+ self .assertEqual (log_call .exc_level , logging .CRITICAL )
657+ self .assertEqual (log_call .max_indent , 40 )
658+ self .assertEqual (log_call .blacklisted_names , ['password' ])
659+ self .assertEqual (log_call .blacklisted_exceptions , [IOError ])
660+ self .assertFalse (log_call .log_call_args )
661+ self .assertFalse (log_call .log_call_args_on_exc )
662+ self .assertFalse (log_call .log_result_obj )
663+
664+ with self .assertRaises (TypeError ):
665+ log_call .log_level = 'WARNING'
666+
667+ self .assertEqual (
668+ '{cls}('
669+ 'log={logger}, '
670+ 'log_level={obj.log_level}, '
671+ 'exc_level={obj.exc_level}, '
672+ 'max_indent={obj.max_indent}, '
673+ 'spec=None, '
674+ 'blacklisted_names={obj.blacklisted_names}, '
675+ 'blacklisted_exceptions={obj.blacklisted_exceptions}, '
676+ 'log_call_args={obj.log_call_args}, '
677+ 'log_call_args_on_exc={obj.log_call_args_on_exc}, '
678+ 'log_result_obj={obj.log_result_obj}, '
679+ ')' .format (
680+ cls = log_call .__class__ .__name__ ,
681+ logger = log_call ._logger ,
682+ obj = log_call
683+ ),
684+ repr (log_call ),
685+ )
686+
503687
504688@mock .patch ('logwrap._log_wrap_shared.logger' , autospec = True )
505689@unittest .skipUnless (
0 commit comments