I am currently tasked with changing all occurrences of LOG4J to SLF4J including logback when necessary.
I already managed converting all the old log4j.xml to logback.xml and to force the usage of logback appenders, however, I just discovered a line in the code that I can't translate to slf4j / logback just yet.
Googling the issue didn't turn up anything usable, so I hope that some of you can help me here.
The code line is:
for ( final Enumeration e = LogManager.getLoggerRepository().getCurrentLoggers(); e.hasMoreElements(); ) {
final Logger logger = (Logger) e.nextElement();
final Level level = logger.getLevel();
if ( level != null ) {
final String name = logger.getName();
final Integer intLvl = new Integer( level.toInt() );
logLevelLocal.put( name, intLvl );
}
The main issue seems to be the LogManager class. I couldn't come up with any logback equivalent. Does anyone know if there is something similar in logback or if there's some kind of workaround?
Best regards, daZza
edit:
1:
public static void toggleLogging( final boolean enable ) {
if ( enable ) {
Properties.LOGGING_ENABLED = Boolean.TRUE;
System.out.println( ConstantsCommon.SYSOUT_PREFIX + LOG_LOGGING + LOG_ON );
for ( final Iterator it = ConstantsCommon.LOGGER_LEVELS.keySet().iterator(); it.hasNext(); ) {
final String logger = (String) it.next();
final int logLevel = ( (Integer) ConstantsCommon.LOGGER_LEVELS.get( logger ) ).intValue();
( (Logger) LoggerFactory.getLogger( logger ) ).setLevel( Level.toLevel( logLevel ) );
}
root.setLevel( Level.toLevel( ConstantsCommon.LOGGER_ROOTLEVEL ) );
} else {
Properties.LOGGING_ENABLED = Boolean.FALSE;
System.out.println( ConstantsCommon.SYSOUT_PREFIX + LOG_LOGGING + LOG_OFF );
for ( final Iterator it = ConstantsCommon.LOGGER_LEVELS.keySet().iterator(); it.hasNext(); ) {
final String logger = (String) it.next();
( (Logger) LoggerFactory.getLogger( logger ) ).setLevel( Level.OFF );
}
root.setLevel( Level.OFF );
}
}
2:
public void setLogLevel( final String loggerName, final int level ) throws ModelRemoteException {
if ( ( level == Priority.ALL_INT ) || ( level == Priority.DEBUG_INT ) || ( level == Priority.ERROR_INT )
|| ( level == Priority.FATAL_INT ) || ( level == Priority.INFO_INT ) || ( level == Priority.WARN_INT )
|| ( level == Priority.OFF_INT ) ) {
final Level targetLevel = Level.toLevel( level );
final Logger logger = ( (Logger) LoggerFactory.getLogger( loggerName ) );
if ( logger != null ) {
( (Logger) LoggerFactory.getLogger( loggerName ) ).setLevel( targetLevel );
ConstantsCommon.LOGGER_LEVELS.put( loggerName, new Integer( level ) );
} else {
throw new ModelRemoteException( PropertiesErrorCodes.ec2024 );
}
} else {
throw new ModelRemoteException( PropertiesErrorCodes.ec2025 );
}
}
After going through tons of documentations and forum posts again, I think that I've found a working solution in logback.
Instead of using the (nonexistent) LogManager class I am now using the logback LoggerContext:
And instead of using getLoggerRepository().getCurrentLoggers() I switched to using