<s> import datetime <EOL> import logging <EOL> from logging import config <EOL> from logging import handlers <EOL> import os <EOL> import sys <EOL> from fabric import api <EOL> from oslo_config import cfg <EOL> import yaml <EOL> from cloudferry . lib . utils import sizeof_format <EOL> getLogger = logging . getLogger <EOL> CONF = cfg . CONF <EOL> class StdoutLogger ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , name = None ) : <EOL> self . log = logging . getLogger ( name or '<STR_LIT>' ) <EOL> def write ( self , message ) : <EOL> message = message . strip ( ) <EOL> if message : <EOL> self . log . info ( message ) <EOL> def flush ( self ) : <EOL> pass <EOL> def configure_logging ( log_config = None , debug = None , forward_stdout = None ) : <EOL> """<STR_LIT>""" <EOL> if log_config is None : <EOL> log_config = CONF . migrate . log_config <EOL> if debug is None : <EOL> debug = CONF . migrate . debug <EOL> if forward_stdout is None : <EOL> forward_stdout = CONF . migrate . forward_stdout <EOL> with open ( log_config , '<STR_LIT:r>' ) as f : <EOL> config . dictConfig ( yaml . load ( f ) ) <EOL> if debug : <EOL> logger = logging . getLogger ( '<STR_LIT>' ) <EOL> for handler in logger . handlers : <EOL> if handler . name == '<STR_LIT>' : <EOL> handler . setLevel ( logging . DEBUG ) <EOL> if forward_stdout : <EOL> sys . stdout = StdoutLogger ( ) <EOL> class RunRotatingFileHandler ( handlers . RotatingFileHandler ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , <EOL> filename = '<STR_LIT>' , <EOL> date_format = '<STR_LIT>' , <EOL> ** kwargs ) : <EOL> self . date_format = date_format <EOL> max_bytes = sizeof_format . parse_size ( kwargs . pop ( '<STR_LIT>' , <NUM_LIT:0> ) ) <EOL> super ( RunRotatingFileHandler , self ) . __init__ ( <EOL> filename = self . get_filename ( filename ) , <EOL> maxBytes = max_bytes , <EOL> ** kwargs ) <EOL> def get_filename ( self , filename ) : <EOL> """<STR_LIT>""" <EOL> if hasattr ( CONF , '<STR_LIT>' ) and hasattr ( CONF . migrate , '<STR_LIT>' ) : <EOL> scenario_filename = os . path . basename ( CONF . migrate . scenario ) <EOL> scenario = os . path . splitext ( scenario_filename ) [ <NUM_LIT:0> ] <EOL> else : <EOL> scenario = '<STR_LIT:none>' <EOL> dt = datetime . datetime . now ( ) . strftime ( self . date_format ) <EOL> return filename % { <EOL> '<STR_LIT>' : scenario , <EOL> '<STR_LIT:date>' : dt <EOL> } <EOL> class CurrentTaskFilter ( logging . Filter ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , name_format = '<STR_LIT>' , ** kwargs ) : <EOL> super ( CurrentTaskFilter , self ) . __init__ ( ** kwargs ) <EOL> self . name_format = name_format <EOL> def filter ( self , record ) : <EOL> current_task = self . name_format % { <EOL> '<STR_LIT:name>' : api . env . current_task or '<STR_LIT>' , <EOL> } <EOL> record . current_task = current_task <EOL> return True </s>
