<s> import logging <EOL> from robot . api import logger <EOL> from robot import utils <EOL> LEVELS = { '<STR_LIT>' : logging . NOTSET , <EOL> '<STR_LIT>' : logging . DEBUG , <EOL> '<STR_LIT>' : logging . INFO , <EOL> '<STR_LIT>' : logging . WARNING } <EOL> def initialize ( level ) : <EOL> logging . raiseExceptions = False <EOL> logging . getLogger ( ) . addHandler ( RobotHandler ( ) ) <EOL> set_level ( level ) <EOL> def set_level ( level ) : <EOL> try : <EOL> level = LEVELS [ level . upper ( ) ] <EOL> except KeyError : <EOL> return <EOL> logging . getLogger ( ) . setLevel ( level ) <EOL> class RobotHandler ( logging . Handler ) : <EOL> def emit ( self , record ) : <EOL> message , error = self . _get_message ( record ) <EOL> method = self . _get_logger_method ( record . levelno ) <EOL> method ( message ) <EOL> if error : <EOL> logger . debug ( error ) <EOL> def _get_message ( self , record ) : <EOL> try : <EOL> return record . getMessage ( ) , None <EOL> except : <EOL> message = '<STR_LIT>' % utils . unic ( record . msg ) <EOL> error = '<STR_LIT:\n>' . join ( utils . get_error_details ( ) ) <EOL> return message , error <EOL> def _get_logger_method ( self , level ) : <EOL> if level >= logging . WARNING : <EOL> return logger . warn <EOL> if level >= logging . INFO : <EOL> return logger . info <EOL> if level >= logging . DEBUG : <EOL> return logger . debug <EOL> return logger . trace </s>
