Skip to content

Commit

Permalink
[Feature] New method setLogLevel & getLogger for CompositeLogger
Browse files Browse the repository at this point in the history
  • Loading branch information
rh-id committed Nov 6, 2021
1 parent 9678aba commit 34fc3c3
Show file tree
Hide file tree
Showing 3 changed files with 85 additions and 1 deletion.
3 changes: 2 additions & 1 deletion logger/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@ android {
}

dependencies {

testImplementation 'junit:junit:4.13.2'
testImplementation 'org.mockito:mockito-core:3.12.4'
}

afterEvaluate {
Expand Down
29 changes: 29 additions & 0 deletions logger/src/main/java/m/co/rh/id/alogger/CompositeLogger.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,35 @@ public void setLogLevel(int logLevel) {
}
}

/**
* Set log level to all clazz type
*
* @param clazz logger type
* @param logLevel log level to be set
*/
public <L extends ILogger> void setLogLevel(Class<L> clazz, int logLevel) {
if (clazz == null) {
return;
}
for (ILogger iLogger : mILoggerList) {
if (clazz.isInstance(iLogger)) {
iLogger.setLogLevel(logLevel);
}
}
}

public <L extends ILogger> L getLogger(Class<L> clazz) {
if (clazz == null) {
return null;
}
for (ILogger iLogger : mILoggerList) {
if (clazz.isInstance(iLogger)) {
return (L) iLogger;
}
}
return null;
}

@Override
public void v(String tag, String message) {
for (ILogger iLogger : mILoggerList) {
Expand Down
54 changes: 54 additions & 0 deletions logger/src/test/java/m/co/rh/id/alogger/CompositeLoggerTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package m.co.rh.id.alogger;

import org.junit.Assert;
import org.junit.Test;
import org.mockito.Mockito;

import java.util.ArrayList;
import java.util.List;

public class CompositeLoggerTest {

@Test
public void getLogger_gettingExactLoggerType() {
AndroidLogger androidLogger = Mockito.mock(AndroidLogger.class);
FileLogger fileLogger = Mockito.mock(FileLogger.class);
ToastLogger toastLogger = Mockito.mock(ToastLogger.class);
List<ILogger> iLoggerList = new ArrayList<>();
iLoggerList.add(androidLogger);
iLoggerList.add(fileLogger);
iLoggerList.add(toastLogger);

CompositeLogger compositeLogger = new CompositeLogger(iLoggerList);
Assert.assertSame(compositeLogger.getLogger(AndroidLogger.class), androidLogger);
Assert.assertSame(compositeLogger.getLogger(FileLogger.class), fileLogger);
Assert.assertSame(compositeLogger.getLogger(ToastLogger.class), toastLogger);
}

@Test
public void setLogLevel_settingBaseOnLoggerType() {
AndroidLogger androidLogger = Mockito.mock(AndroidLogger.class);
FileLogger fileLogger = Mockito.mock(FileLogger.class);
ToastLogger toastLogger = Mockito.mock(ToastLogger.class);
List<ILogger> iLoggerList = new ArrayList<>();
iLoggerList.add(androidLogger);
iLoggerList.add(fileLogger);
iLoggerList.add(toastLogger);

CompositeLogger compositeLogger = new CompositeLogger(iLoggerList);
compositeLogger.setLogLevel(AndroidLogger.class, ILogger.ERROR);
Mockito.verify(androidLogger, Mockito.times(1)).setLogLevel(ILogger.ERROR);
compositeLogger.setLogLevel(FileLogger.class, ILogger.DEBUG);
Mockito.verify(fileLogger, Mockito.times(1)).setLogLevel(ILogger.DEBUG);
compositeLogger.setLogLevel(ToastLogger.class, ILogger.INFO);
Mockito.verify(toastLogger, Mockito.times(1)).setLogLevel(ILogger.INFO);

Mockito.clearInvocations(androidLogger, fileLogger, toastLogger);

compositeLogger.setLogLevel(ILogger.class, ILogger.VERBOSE);
Mockito.verify(androidLogger, Mockito.times(1)).setLogLevel(ILogger.VERBOSE);
Mockito.verify(fileLogger, Mockito.times(1)).setLogLevel(ILogger.VERBOSE);
Mockito.verify(toastLogger, Mockito.times(1)).setLogLevel(ILogger.VERBOSE);

}
}

0 comments on commit 34fc3c3

Please sign in to comment.