public static boolean setLogBackLevel ( String loggerName , String logLevel ) { String logLevelUpper = ( logLevel == null ) ? "OFF" : logLevel . toUpperCase ( ) ; try { Package logbackPackage = Package . getPackage ( LOGBACK_CLASSIC ) ; if ( logbackPackage == null ) { LOG . warn ( "Logback is not in the classpath!" ) ; return false ; } // Use ROOT logger if given logger name is blank. if ( ( loggerName == null ) || loggerName . trim ( ) . isEmpty ( ) ) { loggerName = ( String ) getFieldVaulue ( LOGBACK_CLASSIC_LOGGER , "ROOT_LOGGER_NAME" ) ; } // Obtain logger by the name Logger loggerObtained = LoggerFactory . getLogger ( loggerName ) ; if ( loggerObtained == null ) { // I don't know if this case occurs LOG . warn ( "No logger for the name: {}" , loggerName ) ; return false ; } Object logLevelObj = getFieldVaulue ( LOGBACK_CLASSIC_LEVEL , logLevelUpper ) ; if ( logLevelObj == null ) { LOG . warn ( "No such log level: {}" , logLevelUpper ) ; return false ; } Class < ? > [ ] paramTypes = { logLevelObj . getClass ( ) } ; Object [ ] params = { logLevelObj } ; Class < ? > clz = Class . forName ( LOGBACK_CLASSIC_LOGGER ) ; Method method = clz . getMethod ( "setLevel" , paramTypes ) ; method . invoke ( loggerObtained , params ) ; LOG . info ( "LogBack level set to {} for the logger '{}'" , logLevelUpper , loggerName ) ; return true ; } catch ( NoClassDefFoundError e ) { LOG . warn ( "Couldn't set logback level to {} for the logger '{}'" , logLevelUpper , loggerName , e ) ; return false ; } catch ( Exception e ) { LOG . warn ( "Couldn't set logback level to {} for the logger '{}'" , logLevelUpper , loggerName , e ) ; return false ; } }
Dynamically sets the logback log level for the given class to the specified level .