<s> import json <EOL> import logging <EOL> from pygments import highlight <EOL> from pygments . formatters import Terminal256Formatter <EOL> from pygments . lexers . data import JsonLexer <EOL> from datetime import datetime <EOL> from pytz import utc <EOL> try : <EOL> bytes = bytes <EOL> except : <EOL> bytes = str <EOL> str = unicode <EOL> def utcnow ( ) : <EOL> return datetime . utcnow ( ) . replace ( microsecond = <NUM_LIT:0> , tzinfo = utc ) <EOL> def D_ ( trn ) : <EOL> if '<STR_LIT>' in trn : <EOL> return trn [ '<STR_LIT>' ] <EOL> return next ( trn . values ( ) ) <EOL> def _ ( s ) : <EOL> return s <EOL> def L_ ( s ) : <EOL> return s <EOL> def annotate ( _obj = None , ** kw ) : <EOL> def inner ( obj ) : <EOL> for key in kw : <EOL> setattr ( obj , key , kw [ key ] ) <EOL> return obj <EOL> if _obj is None : <EOL> return inner <EOL> return inner ( _obj ) <EOL> class JSONFormatter ( logging . Formatter ) : <EOL> REPR_FAIL_PLACEHOLDER = '<STR_LIT>' <EOL> BASE_TYPES = ( int , float , bool , bytes , str , list , dict ) <EOL> default_exclude_attrs = { <EOL> '<STR_LIT:args>' , '<STR_LIT:name>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT:filename>' , <EOL> '<STR_LIT>' , '<STR_LIT>' , '<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:message>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> } <EOL> def __init__ ( self , limit_keys_to = None , force_keys = None , ** kwargs ) : <EOL> super ( JSONFormatter , self ) . __init__ ( ** kwargs ) <EOL> self . limit_keys_to = limit_keys_to <EOL> if force_keys : <EOL> self . exclude_attrs = self . default_exclude_attrs - set ( force_keys ) <EOL> else : <EOL> self . exclude_attrs = self . default_exclude_attrs <EOL> def get_json ( self , record , ** kw ) : <EOL> extra = { } <EOL> for attr , value in record . __dict__ . items ( ) : <EOL> if ( ( self . limit_keys_to and attr in self . limit_keys_to ) or <EOL> ( not self . limit_keys_to and attr not in self . exclude_attrs ) ) : <EOL> if isinstance ( value , self . BASE_TYPES ) : <EOL> extra [ attr ] = value <EOL> else : <EOL> try : <EOL> extra [ attr ] = repr ( value ) <EOL> except Exception : <EOL> extra [ attr ] = self . REPR_FAIL_PLACEHOLDER <EOL> if extra : <EOL> try : <EOL> return json . dumps ( extra , ** kw ) <EOL> except Exception as e : <EOL> return json . dumps ( { <EOL> '<STR_LIT>' : repr ( e ) , <EOL> } , ** kw ) <EOL> return '<STR_LIT>' <EOL> def format ( self , record ) : <EOL> formatted = logging . Formatter . format ( self , record ) <EOL> json_string = self . get_json ( record , indent = '<STR_LIT:\t>' , separators = ( '<STR_LIT:U+002C>' , '<STR_LIT>' ) , sort_keys = True , skipkeys = True ) <EOL> if json_string : <EOL> json_string = json_string . replace ( '<STR_LIT:\n>' , '<STR_LIT>' ) <EOL> return '<STR_LIT>' . join ( [ formatted , highlight ( json_string , JsonLexer ( tabsize = <NUM_LIT:4> ) , Terminal256Formatter ( style = '<STR_LIT>' ) ) . strip ( ) ] ) <EOL> return formatted </s>
