Skip to content

Commit

Permalink
Merge pull request #1 from ymesika/test-utils-log4j
Browse files Browse the repository at this point in the history
Added support for Log4j test utils
  • Loading branch information
denis-itskovich authored Dec 12, 2019
2 parents 2286fb0 + 0632fb3 commit bbc9abb
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 2 deletions.
5 changes: 5 additions & 0 deletions libs.properties
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,11 @@ logbackVer = 1.2.3
logbackClassic = ch.qos.logback:logback-classic:$logbackVer
logbackCore = ch.qos.logback:logback-core:$logbackVer

log4jVer = 2.12.1
log4jApi = org.apache.logging.log4j:log4j-api:$log4jVer
log4jCore = org.apache.logging.log4j:log4j-core:$log4jVer
log4jSlf4jBridge = org.apache.logging.log4j:log4j-slf4j-impl:$log4jVer

junit = junit:junit:4.12
mockito = org.mockito:mockito-core:2.21.0
jsr305 = com.google.code.findbugs:jsr305:3.0.2
Expand Down
14 changes: 12 additions & 2 deletions test-utils/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,17 @@ sourceSets {
logback {
java.srcDir 'src/logback/java'
}
log4j {
java.srcDir 'src/log4j/java'
}
}

dependencies {
implementation libs.junit, libs.mockito
implementation project(':stream-utils')
implementation project(':reflect-utils')
implementation project(':generic-utils')
implementation libs.slf4j, libs.logbackClassic
implementation libs.slf4j
implementation libs.guava
compileOnly libs.autoService
annotationProcessor libs.autoService
Expand All @@ -22,6 +25,11 @@ dependencies {
logbackAnnotationProcessor libs.autoService
logbackImplementation project.sourceSetDependency(sourceSets.main)

log4jImplementation libs.slf4j, libs.log4jApi, libs.log4jCore, libs.log4jSlf4jBridge
log4jCompileOnly libs.autoService
log4jAnnotationProcessor libs.autoService
log4jImplementation project.sourceSetDependency(sourceSets.main)

guiceImplementation project.sourceSetDependency(sourceSets.main)
guiceImplementation project.sourceSetDependency(project(':guice-utils').sourceSets.main)
guiceImplementation libs.guice
Expand All @@ -32,13 +40,15 @@ dependencies {
testImplementation libs.guice
testImplementation project.sourceSetDependency(sourceSets.guice)
testImplementation project.sourceSetDependency(sourceSets.logback)
testImplementation project.sourceSetDependency(sourceSets.log4j)
}

configurations {
guiceOutput
guiceImplementation.extendsFrom implementation
logbackImplementation.extendsFrom implementation
testImplementation.extendsFrom guiceImplementation
log4jImplementation.extendsFrom implementation
testImplementation.extendsFrom guiceImplementation, logbackImplementation, log4jImplementation
}

task guiceOutputJar(type: Jar) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package com.slimgears.util.test.logging;

import com.google.auto.service.AutoService;
import com.google.common.base.Strings;
import org.apache.logging.log4j.Level;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.core.LoggerContext;
import org.apache.logging.log4j.core.config.Configuration;
import org.apache.logging.log4j.core.config.LoggerConfig;

import java.util.Optional;

@AutoService(LogLevelListener.class)
public class Log4jLogLevelListener implements LogLevelListener {
@Override
public Rollback onSetLogLevel(String loggerName, LogLevel logLevel) {
return Optional
.of(setLogLevel(loggerName, logLevel))
.orElseGet(Rollback::empty);
}

private Rollback setLogLevel(String loggerName, LogLevel level) {
LoggerContext context = (LoggerContext) LogManager.getContext(false);
Configuration config = context.getConfiguration();
LoggerConfig loggerConfig = config.getLoggerConfig(Strings.isNullOrEmpty(loggerName) ? LogManager.ROOT_LOGGER_NAME : loggerName);
Level newLevel = Level.toLevel(level.name());
Level prevLevel = loggerConfig.getLevel();
loggerConfig.setLevel(newLevel);
context.updateLoggers();
return () -> {
loggerConfig.setLevel(prevLevel);
context.updateLoggers();
};
}
}

0 comments on commit bbc9abb

Please sign in to comment.