<s> import datetime <EOL> import itertools <EOL> import logging <EOL> import logging . config <EOL> import logging . handlers <EOL> import socket <EOL> import sys <EOL> import traceback <EOL> from dateutil import tz <EOL> import six <EOL> from six import moves <EOL> from oslo_context import context as context_utils <EOL> from oslo_serialization import jsonutils <EOL> def _dictify_context ( context ) : <EOL> if context is None : <EOL> return { } <EOL> if not isinstance ( context , dict ) and getattr ( context , '<STR_LIT>' , None ) : <EOL> context = context . to_dict ( ) <EOL> return context <EOL> _CONF = None <EOL> def _store_global_conf ( conf ) : <EOL> global _CONF <EOL> _CONF = conf <EOL> def _update_record_with_context ( record ) : <EOL> """<STR_LIT>""" <EOL> context = record . __dict__ . get ( <EOL> '<STR_LIT>' , <EOL> context_utils . get_current ( ) <EOL> ) <EOL> d = _dictify_context ( context ) <EOL> for k , v in d . items ( ) : <EOL> setattr ( record , k , v ) <EOL> return context <EOL> class _ReplaceFalseValue ( dict ) : <EOL> def __getitem__ ( self , key ) : <EOL> return dict . get ( self , key , None ) or '<STR_LIT:->' <EOL> class JSONFormatter ( logging . Formatter ) : <EOL> def __init__ ( self , fmt = None , datefmt = None ) : <EOL> self . datefmt = datefmt <EOL> try : <EOL> self . hostname = socket . gethostname ( ) <EOL> except socket . error : <EOL> self . hostname = None <EOL> def formatException ( self , ei , strip_newlines = True ) : <EOL> lines = traceback . format_exception ( * ei ) <EOL> if strip_newlines : <EOL> lines = [ moves . filter ( <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> '<STR_LIT>' : self . hostname } <EOL> context = _update_record_with_context ( record ) <EOL> if hasattr ( record , '<STR_LIT>' ) : <EOL> extra = record . extra . copy ( ) <EOL> else : <EOL> extra = { } <EOL> for key in getattr ( record , '<STR_LIT>' , [ ] ) : <EOL> if key not in extra : <EOL> extra [ key ] = getattr ( record , key ) <EOL> if '<STR_LIT>' in extra : <EOL> extra . update ( _dictify_context ( context ) ) <EOL> del extra [ '<STR_LIT>' ] <EOL> message [ '<STR_LIT>' ] = extra <EOL> if record . exc_info : <EOL> message [ '<STR_LIT>' ] = self . formatException ( record . exc_info ) <EOL> return jsonutils . dumps ( message ) <EOL> class ContextFormatter ( logging . Formatter ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , * args , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> self . project = kwargs . pop ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> self . version = kwargs . pop ( '<STR_LIT:version>' , '<STR_LIT>' ) <EOL> self . conf = kwargs . pop ( '<STR_LIT>' , _CONF ) <EOL> logging . Formatter . __init__ ( self , * args , ** kwargs ) <EOL> def format ( self , record ) : <EOL> """<STR_LIT>""" <EOL> if not isinstance ( record . msg , six . text_type ) : <EOL> record . msg = six . text_type ( record . msg ) <EOL> record . project = self . project <EOL> record . version = self . version <EOL> instance_extra = '<STR_LIT>' <EOL> instance = getattr ( record , '<STR_LIT>' , None ) <EOL> instance_uuid = getattr ( record , '<STR_LIT>' , None ) <EOL> context = _update_record_with_context ( record ) <EOL> if instance : <EOL> try : <EOL> instance_extra = ( self . conf . instance_format <EOL> % instance ) <EOL> except TypeError : <EOL> instance_extra = instance <EOL> elif instance_uuid : <EOL> instance_extra = ( self . conf . instance_uuid_format <EOL> % { '<STR_LIT>' : instance_uuid } ) <EOL> elif context : <EOL> instance = getattr ( context , '<STR_LIT>' , None ) <EOL> instance_uuid = getattr ( context , '<STR_LIT>' , None ) <EOL> resource_uuid = getattr ( context , '<STR_LIT>' , None ) <EOL> if instance : <EOL> instance_extra = ( self . conf . instance_format <EOL> % { '<STR_LIT>' : instance } ) <EOL> elif instance_uuid : <EOL> instance_extra = ( self . conf . instance_uuid_format <EOL> % { '<STR_LIT>' : instance_uuid } ) <EOL> elif resource_uuid : <EOL> instance_extra = ( self . conf . instance_uuid_format <EOL> % { '<STR_LIT>' : resource_uuid } ) <EOL> record . instance = instance_extra <EOL> for key in ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' ) : <EOL> if key not in record . __dict__ : <EOL> record . __dict__ [ key ] = '<STR_LIT>' <EOL> if context : <EOL> record . user_identity = ( <EOL> self . conf . logging_user_identity_format % <EOL> _ReplaceFalseValue ( context . __dict__ ) <EOL> ) <EOL> if record . __dict__ . get ( '<STR_LIT>' ) : <EOL> fmt = self . conf . logging_context_format_string <EOL> else : <EOL> fmt = self . conf . logging_default_format_string <EOL> if ( record . levelno == logging . DEBUG and <EOL> self . conf . logging_debug_format_suffix ) : <EOL> fmt += "<STR_LIT:U+0020>" + self . conf . logging_debug_format_suffix <EOL> self . _compute_iso_time ( record ) <EOL> if sys . version_info < ( <NUM_LIT:3> , <NUM_LIT:2> ) : <EOL> self . _fmt = fmt <EOL> else : <EOL> self . _style = logging . PercentStyle ( fmt ) <EOL> self . _fmt = self . _style . _fmt <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 = moves . 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 self . conf . logging_exception_prefix . find ( '<STR_LIT>' ) != - <NUM_LIT:1> : <EOL> record . asctime = self . formatTime ( record , self . datefmt ) <EOL> self . _compute_iso_time ( record ) <EOL> formatted_lines = [ ] <EOL> for line in lines : <EOL> pl = self . 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> def _compute_iso_time ( self , record ) : <EOL> localtz = tz . tzlocal ( ) <EOL> record . isotime = datetime . datetime . fromtimestamp ( <EOL> record . created ) . replace ( tzinfo = localtz ) . isoformat ( ) <EOL> if record . created == int ( record . created ) : <EOL> record . isotime = "<STR_LIT>" % ( record . isotime [ : - <NUM_LIT:6> ] , <EOL> record . isotime [ - <NUM_LIT:6> : ] ) </s>
