<s> """<STR_LIT>""" <EOL> import ConfigParser <EOL> import cStringIO <EOL> import inspect <EOL> import itertools <EOL> import logging <EOL> import logging . config <EOL> import logging . handlers <EOL> import os <EOL> import sys <EOL> import traceback <EOL> from oslo_config import cfg <EOL> from heat_cfnclient . openstack . common . gettextutils import _ <EOL> from heat_cfnclient . openstack . common import importutils <EOL> from heat_cfnclient . openstack . common import jsonutils <EOL> from heat_cfnclient . openstack . common import local <EOL> _DEFAULT_LOG_DATE_FORMAT = "<STR_LIT>" <EOL> common_cli_opts = [ <EOL> cfg . BoolOpt ( '<STR_LIT>' , <EOL> short = '<STR_LIT:d>' , <EOL> default = False , <EOL> help = '<STR_LIT>' <EOL> '<STR_LIT>' ) , <EOL> cfg . BoolOpt ( '<STR_LIT>' , <EOL> short = '<STR_LIT:v>' , <EOL> default = False , <EOL> help = '<STR_LIT>' <EOL> '<STR_LIT>' ) , <EOL> ] <EOL> logging_cli_opts = [ <EOL> cfg . StrOpt ( '<STR_LIT>' , <EOL> metavar = '<STR_LIT>' , <EOL> help = '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' ) , <EOL> cfg . StrOpt ( '<STR_LIT>' , <EOL> default = None , <EOL> metavar = '<STR_LIT>' , <EOL> help = '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' ) , <EOL> cfg . StrOpt ( '<STR_LIT>' , <EOL> default = _DEFAULT_LOG_DATE_FORMAT , <EOL> metavar = '<STR_LIT>' , <EOL> help = '<STR_LIT>' <EOL> '<STR_LIT>' ) , <EOL> cfg . StrOpt ( '<STR_LIT>' , <EOL> metavar = '<STR_LIT>' , <EOL> deprecated_name = '<STR_LIT>' , <EOL> help = '<STR_LIT>' <EOL> '<STR_LIT>' ) , <EOL> cfg . StrOpt ( '<STR_LIT>' , <EOL> deprecated_name = '<STR_LIT>' , <EOL> help = '<STR_LIT>' <EOL> '<STR_LIT>' ) , <EOL> cfg . BoolOpt ( '<STR_LIT>' , <EOL> default = False , <EOL> help = '<STR_LIT>' ) , <EOL> cfg . StrOpt ( '<STR_LIT>' , <EOL> default = '<STR_LIT>' , <EOL> help = '<STR_LIT>' ) <EOL> ] <EOL> generic_log_opts = [ <EOL> cfg . BoolOpt ( '<STR_LIT>' , <EOL> default = True , <EOL> help = '<STR_LIT>' ) <EOL> ] <EOL> log_opts = [ <EOL> cfg . StrOpt ( '<STR_LIT>' , <EOL> default = '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' , <EOL> help = '<STR_LIT>' ) , <EOL> cfg . StrOpt ( '<STR_LIT>' , <EOL> default = '<STR_LIT>' <EOL> '<STR_LIT>' , <EOL> help = '<STR_LIT>' ) , <EOL> cfg . StrOpt ( '<STR_LIT>' , <EOL> default = '<STR_LIT>' , <EOL> help = '<STR_LIT>' ) , <EOL> cfg . StrOpt ( '<STR_LIT>' , <EOL> default = '<STR_LIT>' <EOL> '<STR_LIT>' , <EOL> help = '<STR_LIT>' ) , <EOL> cfg . ListOpt ( '<STR_LIT>' , <EOL> default = [ <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' <EOL> ] , <EOL> help = '<STR_LIT>' ) , <EOL> cfg . BoolOpt ( '<STR_LIT>' , <EOL> default = False , <EOL> help = '<STR_LIT>' ) , <EOL> cfg . BoolOpt ( '<STR_LIT>' , <EOL> default = False , <EOL> help = '<STR_LIT>' ) , <EOL> cfg . StrOpt ( '<STR_LIT>' , <EOL> default = '<STR_LIT>' , <EOL> help = '<STR_LIT>' <EOL> '<STR_LIT>' ) , <EOL> cfg . StrOpt ( '<STR_LIT>' , <EOL> default = '<STR_LIT>' , <EOL> help = '<STR_LIT>' <EOL> '<STR_LIT>' ) , <EOL> ] <EOL> CONF = cfg . CONF <EOL> CONF . register_cli_opts ( common_cli_opts ) <EOL> CONF . register_cli_opts ( logging_cli_opts ) <EOL> CONF . register_opts ( generic_log_opts ) <EOL> CONF . register_opts ( log_opts ) <EOL> logging . AUDIT = logging . INFO + <NUM_LIT:1> <EOL> logging . addLevelName ( logging . AUDIT , '<STR_LIT>' ) <EOL> try : <EOL> NullHandler = logging . NullHandler <EOL> except AttributeError : <EOL> class NullHandler ( logging . Handler ) : <EOL> def handle ( self , record ) : <EOL> pass <EOL> def emit ( self , record ) : <EOL> pass <EOL> def createLock ( self ) : <EOL> self . lock = None <EOL> def _dictify_context ( context ) : <EOL> if context is None : <EOL> return None <EOL> if not isinstance ( context , dict ) and getattr ( context , '<STR_LIT>' , None ) : <EOL> context = context . to_dict ( ) <EOL> return context <EOL> def _get_binary_name ( ) : <EOL> return os . path . basename ( inspect . stack ( ) [ - <NUM_LIT:1> ] [ <NUM_LIT:1> ] ) <EOL> def _get_log_file_path ( binary = None ) : <EOL> logfile = CONF . log_file <EOL> logdir = CONF . log_dir <EOL> if logfile and not logdir : <EOL> return logfile <EOL> if logfile and logdir : <EOL> return os . path . join ( logdir , logfile ) <EOL> if logdir : <EOL> binary = binary or _get_binary_name ( ) <EOL> return '<STR_LIT>' % ( os . path . join ( logdir , binary ) , ) <EOL> class BaseLoggerAdapter ( logging . LoggerAdapter ) : <EOL> def audit ( self , msg , * args , ** kwargs ) : <EOL> self . log ( logging . AUDIT , msg , * args , ** kwargs ) <EOL> class LazyAdapter ( BaseLoggerAdapter ) : <EOL> def __init__ ( self , name = '<STR_LIT>' , version = '<STR_LIT>' ) : <EOL> self . _logger = None <EOL> self . extra = { } <EOL> self . name = name <EOL> self . version = version <EOL> @ property <EOL> def logger ( self ) : <EOL> if not self . _logger : <EOL> self . _logger = getLogger ( self . name , self . version ) <EOL> return self . _logger <EOL> class ContextAdapter ( BaseLoggerAdapter ) : <EOL> warn = logging . LoggerAdapter . warning <EOL> def __init__ ( self , logger , project_name , version_string ) : <EOL> self . logger = logger <EOL> self . project = project_name <EOL> self . version = version_string <EOL> @ property <EOL> def handlers ( self ) : <EOL> return self . logger . handlers <EOL> def deprecated ( self , msg , * args , ** kwargs ) : <EOL> stdmsg = _ ( "<STR_LIT>" ) % msg <EOL> if CONF . fatal_deprecations : <EOL> self . critical ( stdmsg , * args , ** kwargs ) <EOL> raise DeprecatedConfig ( msg = stdmsg ) <EOL> else : <EOL> self . warn ( stdmsg , * args , ** kwargs ) <EOL> def process ( self , msg , kwargs ) : <EOL> if '<STR_LIT>' not in kwargs : <EOL> kwargs [ '<STR_LIT>' ] = { } <EOL> extra = kwargs [ '<STR_LIT>' ] <EOL> context = kwargs . pop ( '<STR_LIT>' , None ) <EOL> if not context : <EOL> context = getattr ( local . store , '<STR_LIT>' , None ) <EOL> if context : <EOL> extra . update ( _dictify_context ( context ) ) <EOL> instance = kwargs . pop ( '<STR_LIT>' , None ) <EOL> instance_extra = '<STR_LIT>' <EOL> if instance : <EOL> instance_extra = CONF . instance_format % instance <EOL> else : <EOL> instance_uuid = kwargs . pop ( '<STR_LIT>' , None ) <EOL> if instance_uuid : <EOL> instance_extra = ( CONF . instance_uuid_format <EOL> % { '<STR_LIT>' : instance_uuid } ) <EOL> extra . update ( { '<STR_LIT>' : instance_extra } ) <EOL> extra . update ( { "<STR_LIT>" : self . project } ) <EOL> extra . update ( { "<STR_LIT:version>" : self . version } ) <EOL> extra [ '<STR_LIT>' ] = extra . copy ( ) <EOL> return msg , kwargs <EOL> class JSONFormatter ( logging . Formatter ) : <EOL> def __init__ ( self , fmt = None , datefmt = None ) : <EOL> self . datefmt = datefmt <EOL> def formatException ( self , ei , strip_newlines = True ) : <EOL> lines = traceback . format_exception ( * ei ) <EOL> if strip_newlines : <EOL> lines = [ itertools . ifilter ( <EOL> lambda x : x , <EOL> line . rstrip ( ) . splitlines ( ) ) for line in lines ] <EOL> lines = list ( itertools . chain ( * lines ) ) <EOL> return lines <EOL> def format ( self , record ) : <EOL> message = { '<STR_LIT:message>' : record . getMessage ( ) , <EOL> '<STR_LIT>' : self . formatTime ( record , self . datefmt ) , <EOL> '<STR_LIT:name>' : record . name , <EOL> '<STR_LIT>' : record . msg , <EOL> '<STR_LIT:args>' : record . args , <EOL> '<STR_LIT>' : record . levelname , <EOL> '<STR_LIT>' : record . levelno , <EOL> '<STR_LIT>' : record . pathname , <EOL> '<STR_LIT:filename>' : record . filename , <EOL> '<STR_LIT>' : record . module , <EOL> '<STR_LIT>' : record . lineno , <EOL> '<STR_LIT>' : record . funcName , <EOL> '<STR_LIT>' : record . created , <EOL> '<STR_LIT>' : record . msecs , <EOL> '<STR_LIT>' : record . relativeCreated , <EOL> '<STR_LIT>' : record . thread , <EOL> '<STR_LIT>' : record . threadName , <EOL> '<STR_LIT>' : record . processName , <EOL> '<STR_LIT>' : record . process , <EOL> '<STR_LIT>' : None } <EOL> if hasattr ( record , '<STR_LIT>' ) : <EOL> message [ '<STR_LIT>' ] = record . extra <EOL> if record . exc_info : <EOL> message [ '<STR_LIT>' ] = self . formatException ( record . exc_info ) <EOL> return jsonutils . dumps ( message ) <EOL> def _create_logging_excepthook ( product_name ) : <EOL> def logging_excepthook ( type , value , tb ) : <EOL> extra = { } <EOL> if CONF . verbose : <EOL> extra [ '<STR_LIT>' ] = ( type , value , tb ) <EOL> getLogger ( product_name ) . critical ( str ( value ) , ** extra ) <EOL> return logging_excepthook <EOL> class LogConfigError ( Exception ) : <EOL> message = _ ( '<STR_LIT>' ) <EOL> def __init__ ( self , log_config , err_msg ) : <EOL> self . log_config = log_config <EOL> self . err_msg = err_msg <EOL> def __str__ ( self ) : <EOL> return self . message % dict ( log_config = self . log_config , <EOL> err_msg = self . err_msg ) <EOL> def _load_log_config ( log_config ) : <EOL> try : <EOL> logging . config . fileConfig ( log_config ) <EOL> except ConfigParser . Error as exc : <EOL> raise LogConfigError ( log_config , str ( exc ) ) <EOL> def setup ( product_name ) : <EOL> """<STR_LIT>""" <EOL> if CONF . log_config : <EOL> _load_log_config ( CONF . log_config ) <EOL> else : <EOL> _setup_logging_from_conf ( ) <EOL> sys . excepthook = _create_logging_excepthook ( product_name ) <EOL> def set_defaults ( logging_context_format_string ) : <EOL> cfg . set_defaults ( log_opts , <EOL> logging_context_format_string = <EOL> logging_context_format_string ) <EOL> def _find_facility_from_conf ( ) : <EOL> facility_names = logging . handlers . SysLogHandler . facility_names <EOL> facility = getattr ( logging . handlers . SysLogHandler , <EOL> CONF . syslog_log_facility , <EOL> None ) <EOL> if facility is None and CONF . syslog_log_facility in facility_names : <EOL> facility = facility_names . get ( CONF . syslog_log_facility ) <EOL> if facility is None : <EOL> valid_facilities = facility_names . keys ( ) <EOL> consts = [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] <EOL> valid_facilities . extend ( consts ) <EOL> raise TypeError ( _ ( '<STR_LIT>' ) % <EOL> '<STR_LIT:U+002CU+0020>' . join ( "<STR_LIT>" % fac <EOL> for fac in valid_facilities ) ) <EOL> return facility <EOL> def _setup_logging_from_conf ( ) : <EOL> log_root = getLogger ( None ) . logger <EOL> for handler in log_root . handlers : <EOL> log_root . removeHandler ( handler ) <EOL> if CONF . use_syslog : <EOL> facility = _find_facility_from_conf ( ) <EOL> syslog = logging . handlers . SysLogHandler ( address = '<STR_LIT>' , <EOL> facility = facility ) <EOL> log_root . addHandler ( syslog ) <EOL> logpath = _get_log_file_path ( ) <EOL> if logpath : <EOL> filelog = logging . handlers . WatchedFileHandler ( logpath ) <EOL> log_root . addHandler ( filelog ) <EOL> if CONF . use_stderr : <EOL> streamlog = ColorHandler ( ) <EOL> log_root . addHandler ( streamlog ) <EOL> elif not CONF . log_file : <EOL> streamlog = logging . StreamHandler ( sys . stdout ) <EOL> log_root . addHandler ( streamlog ) <EOL> if CONF . publish_errors : <EOL> handler = importutils . import_object ( <EOL> "<STR_LIT>" , <EOL> logging . ERROR ) <EOL> log_root . addHandler ( handler ) <EOL> datefmt = CONF . log_date_format <EOL> for handler in log_root . handlers : <EOL> if CONF . log_format : <EOL> handler . setFormatter ( logging . Formatter ( fmt = CONF . log_format , <EOL> datefmt = datefmt ) ) <EOL> log_root . info ( '<STR_LIT>' <EOL> '<STR_LIT>' ) <EOL> else : <EOL> handler . setFormatter ( ContextFormatter ( datefmt = datefmt ) ) <EOL> if CONF . debug : <EOL> log_root . setLevel ( logging . DEBUG ) <EOL> elif CONF . verbose : <EOL> log_root . setLevel ( logging . INFO ) <EOL> else : <EOL> log_root . setLevel ( logging . WARNING ) <EOL> for pair in CONF . default_log_levels : <EOL> mod , _sep , level_name = pair . partition ( '<STR_LIT:=>' ) <EOL> level = logging . getLevelName ( level_name ) <EOL> logger = logging . getLogger ( mod ) <EOL> logger . setLevel ( level ) <EOL> _loggers = { } <EOL> def getLogger ( name = '<STR_LIT>' , version = '<STR_LIT>' ) : <EOL> if name not in _loggers : <EOL> _loggers [ name ] = ContextAdapter ( logging . getLogger ( name ) , <EOL> name , <EOL> version ) <EOL> return _loggers [ name ] <EOL> def getLazyLogger ( name = '<STR_LIT>' , version = '<STR_LIT>' ) : <EOL> """<STR_LIT>""" <EOL> return LazyAdapter ( name , version ) <EOL> class WritableLogger ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , logger , level = logging . INFO ) : <EOL> self . logger = logger <EOL> self . level = level <EOL> def write ( self , msg ) : <EOL> self . logger . log ( self . level , msg ) <EOL> class ContextFormatter ( logging . Formatter ) : <EOL> """<STR_LIT>""" <EOL> def format ( self , record ) : <EOL> """<STR_LIT>""" <EOL> for key in ( '<STR_LIT>' , '<STR_LIT>' ) : <EOL> if key not in record . __dict__ : <EOL> record . __dict__ [ key ] = '<STR_LIT>' <EOL> if record . __dict__ . get ( '<STR_LIT>' , None ) : <EOL> self . _fmt = CONF . logging_context_format_string <EOL> else : <EOL> self . _fmt = CONF . logging_default_format_string <EOL> if ( record . levelno == logging . DEBUG and <EOL> CONF . logging_debug_format_suffix ) : <EOL> self . _fmt += "<STR_LIT:U+0020>" + CONF . logging_debug_format_suffix <EOL> if record . exc_info : <EOL> record . exc_text = self . formatException ( record . exc_info , record ) <EOL> return logging . Formatter . format ( self , record ) <EOL> def formatException ( self , exc_info , record = None ) : <EOL> """<STR_LIT>""" <EOL> if not record : <EOL> return logging . Formatter . formatException ( self , exc_info ) <EOL> stringbuffer = cStringIO . StringIO ( ) <EOL> traceback . print_exception ( exc_info [ <NUM_LIT:0> ] , exc_info [ <NUM_LIT:1> ] , exc_info [ <NUM_LIT:2> ] , <EOL> None , stringbuffer ) <EOL> lines = stringbuffer . getvalue ( ) . split ( '<STR_LIT:\n>' ) <EOL> stringbuffer . close ( ) <EOL> if CONF . logging_exception_prefix . find ( '<STR_LIT>' ) != - <NUM_LIT:1> : <EOL> record . asctime = self . formatTime ( record , self . datefmt ) <EOL> formatted_lines = [ ] <EOL> for line in lines : <EOL> pl = CONF . logging_exception_prefix % record . __dict__ <EOL> fl = '<STR_LIT>' % ( pl , line ) <EOL> formatted_lines . append ( fl ) <EOL> return '<STR_LIT:\n>' . join ( formatted_lines ) <EOL> class ColorHandler ( logging . StreamHandler ) : <EOL> LEVEL_COLORS = { <EOL> logging . DEBUG : '<STR_LIT>' , <EOL> logging . INFO : '<STR_LIT>' , <EOL> logging . AUDIT : '<STR_LIT>' , <EOL> logging . WARN : '<STR_LIT>' , <EOL> logging . ERROR : '<STR_LIT>' , <EOL> logging . CRITICAL : '<STR_LIT>' , <EOL> } <EOL> def format ( self , record ) : <EOL> record . color = self . LEVEL_COLORS [ record . levelno ] <EOL> return logging . StreamHandler . format ( self , record ) <EOL> class DeprecatedConfig ( Exception ) : <EOL> message = _ ( "<STR_LIT>" ) <EOL> def __init__ ( self , msg ) : <EOL> super ( Exception , self ) . __init__ ( self . message % dict ( msg = msg ) ) </s>
