<s> import inspect <EOL> import logging <EOL> import logging . handlers <EOL> import os <EOL> NOTSET = logging . NOTSET <EOL> VERBOSE = <NUM_LIT:5> <EOL> DEBUG = logging . DEBUG <EOL> INFO = logging . INFO <EOL> WARNING = logging . WARNING <EOL> WARN = WARNING <EOL> ERROR = logging . ERROR <EOL> CRITICAL = logging . CRITICAL <EOL> FATAL = CRITICAL <EOL> LEVEL_NAMES = { <EOL> CRITICAL : '<STR_LIT>' , <EOL> ERROR : '<STR_LIT>' , <EOL> WARNING : '<STR_LIT>' , <EOL> INFO : '<STR_LIT>' , <EOL> DEBUG : '<STR_LIT>' , <EOL> VERBOSE : '<STR_LIT>' , <EOL> NOTSET : '<STR_LIT>' , <EOL> '<STR_LIT>' : CRITICAL , <EOL> '<STR_LIT>' : ERROR , <EOL> '<STR_LIT>' : WARNING , <EOL> '<STR_LIT>' : WARNING , <EOL> '<STR_LIT>' : INFO , <EOL> '<STR_LIT>' : DEBUG , <EOL> '<STR_LIT>' : VERBOSE , <EOL> '<STR_LIT>' : NOTSET , <EOL> } <EOL> DEFAULT_PROMPTS = { <EOL> VERBOSE : "<STR_LIT>" . format ( level = LEVEL_NAMES [ VERBOSE ] ) , <EOL> DEBUG : "<STR_LIT>" . format ( level = LEVEL_NAMES [ DEBUG ] ) , <EOL> INFO : "<STR_LIT>" . format ( level = LEVEL_NAMES [ INFO ] ) , <EOL> WARNING : "<STR_LIT>" . format ( level = LEVEL_NAMES [ WARNING ] ) , <EOL> ERROR : "<STR_LIT>" . format ( level = LEVEL_NAMES [ ERROR ] ) , <EOL> CRITICAL : "<STR_LIT>" . format ( level = LEVEL_NAMES [ CRITICAL ] ) , <EOL> } <EOL> ELEVATED_PATH = '<STR_LIT>' <EOL> LOCAL_PATH = '<STR_LIT>' <EOL> class Logger ( logging . Logger ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , name , level = INFO , print_default = True , log_default = True ) : <EOL> """<STR_LIT>""" <EOL> super ( Logger , self ) . __init__ ( name = name , level = level ) <EOL> self . level_names = LEVEL_NAMES . copy ( ) <EOL> self . prompts = DEFAULT_PROMPTS . copy ( ) <EOL> self . print_default = print_default <EOL> self . log_default = log_default <EOL> def add_level_name ( self , level , level_name ) : <EOL> """<STR_LIT>""" <EOL> self . level_names [ level ] = level_name <EOL> self . level_names [ level_name ] = level <EOL> logging . addLevelName ( level , level_name ) <EOL> def set_prompt ( self , level , prompt ) : <EOL> """<STR_LIT>""" <EOL> self . prompts [ level ] = prompt <EOL> def verbose ( self , message , print_out = None , log = None ) : <EOL> """<STR_LIT>""" <EOL> if print_out is None : <EOL> print_out = self . print_default <EOL> if log is None : <EOL> log = self . log_default <EOL> if print_out and self . level <= VERBOSE : <EOL> print ( "<STR_LIT>" . format ( prompt = self . prompts [ VERBOSE ] , message = message ) ) <EOL> if log : <EOL> super ( Logger , self ) . debug ( message ) <EOL> def debug ( self , message , print_out = None , log = None ) : <EOL> """<STR_LIT>""" <EOL> if print_out is None : <EOL> print_out = self . print_default <EOL> if log is None : <EOL> log = self . log_default <EOL> if print_out and self . level <= DEBUG : <EOL> print ( "<STR_LIT>" . format ( prompt = self . prompts [ DEBUG ] , message = message ) ) <EOL> if log : <EOL> super ( Logger , self ) . debug ( message ) <EOL> def info ( self , message , print_out = None , log = None ) : <EOL> """<STR_LIT>""" <EOL> if print_out is None : <EOL> print_out = self . print_default <EOL> if log is None : <EOL> log = self . log_default <EOL> if print_out and self . level <= INFO : <EOL> print ( "<STR_LIT>" . format ( prompt = self . prompts [ INFO ] , message = message ) ) <EOL> if log : <EOL> super ( Logger , self ) . info ( message ) <EOL> def warning ( self , message , print_out = None , log = None ) : <EOL> """<STR_LIT>""" <EOL> if print_out is None : <EOL> print_out = self . print_default <EOL> if log is None : <EOL> log = self . log_default <EOL> if print_out and self . level <= WARNING : <EOL> print ( "<STR_LIT>" . format ( prompt = self . prompts [ WARNING ] , message = message ) ) <EOL> if log : <EOL> super ( Logger , self ) . warning ( message ) <EOL> warn = warning <EOL> def error ( self , message , print_out = None , log = None ) : <EOL> """<STR_LIT>""" <EOL> if print_out is None : <EOL> print_out = self . print_default <EOL> if log is None : <EOL> log = self . log_default <EOL> if print_out and self . level <= ERROR : <EOL> print ( "<STR_LIT>" . format ( prompt = self . prompts [ ERROR ] , message = message ) ) <EOL> if log : <EOL> super ( Logger , self ) . error ( message ) <EOL> def critical ( self , message , print_out = None , log = None ) : <EOL> """<STR_LIT>""" <EOL> if print_out is None : <EOL> print_out = self . print_default <EOL> if log is None : <EOL> log = self . log_default <EOL> if print_out and self . level <= CRITICAL : <EOL> print ( "<STR_LIT>" . format ( prompt = self . prompts [ CRITICAL ] , message = message ) ) <EOL> if log : <EOL> super ( Logger , self ) . critical ( message ) <EOL> fatal = critical <EOL> def log ( self , level , message , print_out = None , log = None ) : <EOL> """<STR_LIT>""" <EOL> if print_out is None : <EOL> print_out = self . print_default <EOL> if log is None : <EOL> log = self . log_default <EOL> if print_out and self . level <= level : <EOL> prompt = self . prompts . get ( level , '<STR_LIT>' . format ( level ) ) <EOL> print ( "<STR_LIT>" . format ( prompt = prompt , message = message ) ) <EOL> if log : <EOL> super ( Logger , self ) . log ( level , message ) <EOL> class FileLogger ( Logger ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , name = None , level = INFO , path = None , print_default = True , log_default = True , log_size = <NUM_LIT> , backup_count = <NUM_LIT:5> ) : <EOL> """<STR_LIT>""" <EOL> if path : <EOL> if not path . endswith ( '<STR_LIT:/>' ) : <EOL> path += '<STR_LIT:/>' <EOL> else : <EOL> if os . access ( os . path . dirname ( ELEVATED_PATH [ : - <NUM_LIT:1> ] ) , os . W_OK ) : <EOL> path = ELEVATED_PATH <EOL> else : <EOL> path = os . path . expanduser ( LOCAL_PATH ) <EOL> if not name : <EOL> name = inspect . stack ( ) [ <NUM_LIT:1> ] [ <NUM_LIT:3> ] <EOL> destination = os . path . join ( path , name ) <EOL> if not destination . endswith ( '<STR_LIT>' ) : <EOL> destination += '<STR_LIT>' <EOL> if not os . path . exists ( os . path . dirname ( destination ) ) : <EOL> try : <EOL> os . makedirs ( os . path . dirname ( destination ) ) <EOL> except OSError : <EOL> raise ValueError ( "<STR_LIT>" . format ( str ( os . path . dirname ( destination ) ) ) ) <EOL> super ( FileLogger , self ) . __init__ ( name = name , level = level , print_default = print_default , log_default = log_default ) <EOL> self . path = path <EOL> formatter = logging . Formatter ( '<STR_LIT>' ) <EOL> handler = logging . handlers . RotatingFileHandler ( destination , maxBytes = log_size , backupCount = backup_count ) <EOL> handler . setFormatter ( formatter ) <EOL> self . addHandler ( handler ) <EOL> class StreamLogger ( Logger ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , name = None , level = DEBUG , print_default = False , log_default = True ) : <EOL> """<STR_LIT>""" <EOL> if not name : <EOL> name = inspect . stack ( ) [ <NUM_LIT:1> ] [ <NUM_LIT:3> ] <EOL> super ( StreamLogger , self ) . __init__ ( name = name , level = level , print_default = print_default , log_default = log_default ) <EOL> formatter = logging . Formatter ( '<STR_LIT>' ) <EOL> handler = logging . StreamHandler ( ) <EOL> handler . setFormatter ( formatter ) <EOL> self . addHandler ( handler ) <EOL> def get_logger ( name = None , log = False , level = INFO , path = None ) : <EOL> """<STR_LIT>""" <EOL> if log : <EOL> return FileLogger ( name = name , level = level , path = path ) <EOL> else : <EOL> return StreamLogger ( name = name , level = level ) <EOL> def file_logger ( name = None , level = INFO , path = None ) : <EOL> """<STR_LIT>""" <EOL> return FileLogger ( name = name , level = level , path = path ) <EOL> def stream_logger ( level = DEBUG ) : <EOL> """<STR_LIT>""" <EOL> return StreamLogger ( name = None , level = level ) </s>
