<s> from __future__ import absolute_import , unicode_literals <EOL> import logging <EOL> from logging import getLogger , Formatter , StreamHandler <EOL> from simiki import utils <EOL> from simiki . compat import is_linux , is_osx <EOL> class ANSIFormatter ( Formatter ) : <EOL> """<STR_LIT>""" <EOL> def format ( self , record ) : <EOL> try : <EOL> msg = super ( ANSIFormatter , self ) . format ( record ) <EOL> except : <EOL> msg = Formatter . format ( self , record ) <EOL> lvl2color = { <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : "<STR_LIT>" <EOL> } <EOL> rln = record . levelname <EOL> if rln in lvl2color : <EOL> return "<STR_LIT>" . format ( <EOL> utils . color_msg ( lvl2color [ rln ] , rln ) , <EOL> msg <EOL> ) <EOL> else : <EOL> return msg <EOL> class NonANSIFormatter ( Formatter ) : <EOL> '''<STR_LIT>''' <EOL> def format ( self , record ) : <EOL> try : <EOL> msg = super ( NonANSIFormatter , self ) . format ( record ) <EOL> except : <EOL> msg = Formatter . format ( self , record ) <EOL> rln = record . levelname <EOL> return "<STR_LIT>" . format ( rln , msg ) <EOL> def _is_platform_allowed_ansi ( ) : <EOL> '''<STR_LIT>''' <EOL> if is_linux or is_osx : <EOL> return True <EOL> else : <EOL> return False <EOL> def logging_init ( level = None , logger = getLogger ( ) , <EOL> handler = StreamHandler ( ) , use_color = True ) : <EOL> if use_color and _is_platform_allowed_ansi ( ) : <EOL> fmt = ANSIFormatter ( ) <EOL> else : <EOL> fmt = NonANSIFormatter ( ) <EOL> handler . setFormatter ( fmt ) <EOL> logger . addHandler ( handler ) <EOL> if level : <EOL> logger . setLevel ( level ) <EOL> if __name__ == "<STR_LIT:__main__>" : <EOL> logging_init ( level = logging . DEBUG ) <EOL> root_logger = logging . getLogger ( ) <EOL> root_logger . debug ( "<STR_LIT>" ) <EOL> root_logger . info ( "<STR_LIT:info>" ) <EOL> root_logger . warning ( "<STR_LIT>" ) <EOL> root_logger . error ( "<STR_LIT:error>" ) <EOL> root_logger . critical ( "<STR_LIT>" ) </s>
