<s> import logbook <EOL> from . utils import capturing_stderr_context , make_fake_mail_handler <EOL> def test_custom_logger ( activation_strategy , logger ) : <EOL> client_ip = '<STR_LIT:127.0.0.1>' <EOL> class CustomLogger ( logbook . Logger ) : <EOL> def process_record ( self , record ) : <EOL> record . extra [ '<STR_LIT>' ] = client_ip <EOL> custom_log = CustomLogger ( '<STR_LIT>' ) <EOL> fmt = ( '<STR_LIT>' <EOL> '<STR_LIT>' ) <EOL> handler = logbook . TestHandler ( format_string = fmt ) <EOL> assert handler . format_string == fmt <EOL> with activation_strategy ( handler ) : <EOL> custom_log . warn ( '<STR_LIT>' ) <EOL> logger . warn ( '<STR_LIT>' ) <EOL> assert handler . formatted_records == [ <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' ] <EOL> def test_custom_handling ( activation_strategy , logger ) : <EOL> class MyTestHandler ( logbook . TestHandler ) : <EOL> def handle ( self , record ) : <EOL> if record . extra . get ( '<STR_LIT>' ) != '<STR_LIT>' : <EOL> return False <EOL> return logbook . TestHandler . handle ( self , record ) <EOL> assert logbook . Handler . __class__ == logbook . handlers . _HandlerType <EOL> class MyLogger ( logbook . Logger ) : <EOL> def process_record ( self , record ) : <EOL> logbook . Logger . process_record ( self , record ) <EOL> record . extra [ '<STR_LIT>' ] = '<STR_LIT>' <EOL> log = MyLogger ( ) <EOL> handler = MyTestHandler ( ) <EOL> with capturing_stderr_context ( ) as captured : <EOL> with activation_strategy ( handler ) : <EOL> log . warn ( '<STR_LIT>' ) <EOL> logger . warn ( '<STR_LIT>' ) <EOL> assert handler . has_warning ( '<STR_LIT>' ) <EOL> assert '<STR_LIT>' in captured . getvalue ( ) <EOL> def test_nested_setups ( activation_strategy ) : <EOL> with capturing_stderr_context ( ) as captured : <EOL> logger = logbook . Logger ( '<STR_LIT>' ) <EOL> test_handler = logbook . TestHandler ( level = '<STR_LIT>' ) <EOL> mail_handler = make_fake_mail_handler ( bubble = True ) <EOL> handlers = logbook . NestedSetup ( [ <EOL> logbook . NullHandler ( ) , <EOL> test_handler , <EOL> mail_handler <EOL> ] ) <EOL> with activation_strategy ( handlers ) : <EOL> logger . warn ( '<STR_LIT>' ) <EOL> logger . error ( '<STR_LIT>' ) <EOL> try : <EOL> <NUM_LIT:1> / <NUM_LIT:0> <EOL> except Exception : <EOL> logger . exception ( ) <EOL> logger . warn ( '<STR_LIT>' ) <EOL> assert test_handler . has_warning ( '<STR_LIT>' ) <EOL> assert test_handler . has_error ( '<STR_LIT>' ) <EOL> assert len ( mail_handler . mails ) == <NUM_LIT:2> <EOL> assert '<STR_LIT>' in mail_handler . mails [ <NUM_LIT:0> ] [ <NUM_LIT:2> ] <EOL> assert '<STR_LIT>' in mail_handler . mails [ <NUM_LIT:1> ] [ <NUM_LIT:2> ] <EOL> assert '<STR_LIT>' in captured . getvalue ( ) <EOL> with activation_strategy ( handlers ) : <EOL> logger . warn ( '<STR_LIT>' ) <EOL> handlers . push_application ( ) <EOL> try : <EOL> logger . warn ( '<STR_LIT>' ) <EOL> finally : <EOL> handlers . pop_application ( ) <EOL> def test_filtering ( activation_strategy ) : <EOL> logger1 = logbook . Logger ( '<STR_LIT>' ) <EOL> logger2 = logbook . Logger ( '<STR_LIT>' ) <EOL> handler = logbook . TestHandler ( ) <EOL> outer_handler = logbook . TestHandler ( ) <EOL> def only_1 ( record , handler ) : <EOL> return record . dispatcher is logger1 <EOL> handler . filter = only_1 <EOL> with activation_strategy ( outer_handler ) : <EOL> with activation_strategy ( handler ) : <EOL> logger1 . warn ( '<STR_LIT:foo>' ) <EOL> logger2 . warn ( '<STR_LIT:bar>' ) <EOL> assert handler . has_warning ( '<STR_LIT:foo>' , channel = '<STR_LIT>' ) <EOL> assert ( not handler . has_warning ( '<STR_LIT:bar>' , channel = '<STR_LIT>' ) ) <EOL> assert ( not outer_handler . has_warning ( '<STR_LIT:foo>' , channel = '<STR_LIT>' ) ) <EOL> assert outer_handler . has_warning ( '<STR_LIT:bar>' , channel = '<STR_LIT>' ) <EOL> def test_different_context_pushing ( activation_strategy ) : <EOL> h1 = logbook . TestHandler ( level = logbook . DEBUG ) <EOL> h2 = logbook . TestHandler ( level = logbook . INFO ) <EOL> h3 = logbook . TestHandler ( level = logbook . WARNING ) <EOL> logger = logbook . Logger ( '<STR_LIT>' ) <EOL> with activation_strategy ( h1 ) : <EOL> with activation_strategy ( h2 ) : <EOL> with activation_strategy ( h3 ) : <EOL> logger . warn ( '<STR_LIT>' ) <EOL> logger . info ( '<STR_LIT>' ) <EOL> logger . debug ( '<STR_LIT>' ) <EOL> assert h1 . has_debug ( '<STR_LIT>' ) <EOL> assert h2 . has_info ( '<STR_LIT>' ) <EOL> assert h3 . has_warning ( '<STR_LIT>' ) <EOL> for handler in h1 , h2 , h3 : <EOL> assert len ( handler . records ) == <NUM_LIT:1> <EOL> def test_default_handlers ( logger ) : <EOL> with capturing_stderr_context ( ) as stream : <EOL> logger . warn ( '<STR_LIT>' ) <EOL> captured = stream . getvalue ( ) <EOL> assert '<STR_LIT>' in captured </s>
