Skip to content

Commit b5a5e07

Browse files
committed
added dependency injection to sugared logger
1 parent 976748e commit b5a5e07

File tree

7 files changed

+33
-21
lines changed

7 files changed

+33
-21
lines changed

java-projects/output.txt

-4.75 KB
Binary file not shown.

java-projects/src/main/java/Main.java

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,19 @@
11
import com.google.inject.Guice;
22
import com.google.inject.Injector;
3+
import logger.FastLogger;
34
import modules.LoggingModule;
45
import modules.PropertiesModule;
56

67
import java.util.concurrent.ExecutionException;
78

89
public class Main {
9-
public static void main(String[] args) throws ExecutionException, InterruptedException {
10-
final Injector injector = Guice.createInjector(new PropertiesModule(), new LoggingModule());
10+
public static void main(String[] args) {
11+
Injector injector = Guice.createInjector(new PropertiesModule(), new LoggingModule());
1112
final TaskManager taskManager = injector.getInstance(TaskManager.class);
12-
taskManager.execute().get();
13+
try {
14+
taskManager.execute().get();
15+
} catch (InterruptedException | ExecutionException e) {
16+
e.printStackTrace();
17+
}
1318
}
1419
}

java-projects/src/main/java/TaskManager.java

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,17 @@
1+
import com.google.inject.Inject;
2+
import com.google.inject.Singleton;
3+
import logger.Logger;
14
import logger.SugaredLogger;
25

36
import java.util.concurrent.CompletableFuture;
47

8+
@Singleton
59
public class TaskManager {
6-
private final SugaredLogger logger;
10+
private final Logger logger;
711

8-
public TaskManager() {
9-
this.logger = SugaredLogger.getLogger(System.out);
12+
@Inject
13+
public TaskManager(final Logger logger) {
14+
this.logger = logger;
1015
}
1116

1217
public CompletableFuture<Void> execute() {

java-projects/src/main/java/logger/FastLogger.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ public class FastLogger implements Logger {
1818

1919
@Inject
2020
public FastLogger(@Named("logger.fast.identifier") final String identifier,
21-
final OutputStream stream,
21+
@Named("console-output-stream") final OutputStream stream,
2222
@Named("logger.fast.buffer.size") final Integer bufferSize) {
2323
this.identifier = identifier;
2424
this.stream = stream;

java-projects/src/main/java/logger/Logger.java

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
package logger;
22

3-
import exceptions.LoggingException;
4-
53
import java.util.concurrent.CompletableFuture;
64

75
public interface Logger {

java-projects/src/main/java/logger/SugaredLogger.java

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
package logger;
22

3+
import com.google.inject.Inject;
4+
import com.google.inject.Singleton;
5+
import com.google.inject.name.Named;
36
import exceptions.LoggingException;
47

58
import java.io.IOException;
@@ -10,26 +13,19 @@
1013
import java.util.concurrent.ExecutorService;
1114
import java.util.concurrent.Executors;
1215

13-
public class SugaredLogger {
14-
private static SugaredLogger SUGARED_LOGGER;
16+
@Singleton
17+
public class SugaredLogger implements Logger {
1518
private final String identifier;
1619
private final OutputStream stream;
1720
private final List<String> buffer;
1821

19-
private SugaredLogger(final OutputStream stream) {
22+
@Inject
23+
public SugaredLogger(final OutputStream stream, @Named("logger.fast.identifier") String identifier) {
2024
this.stream = stream;
21-
identifier = "service:";
25+
this.identifier = identifier;
2226
buffer = new ArrayList<>();
2327
}
2428

25-
26-
public static synchronized SugaredLogger getLogger(OutputStream out) {
27-
if (SUGARED_LOGGER == null) {
28-
SUGARED_LOGGER = new SugaredLogger(out);
29-
}
30-
return SUGARED_LOGGER;
31-
}
32-
3329
public boolean write(final String word) {
3430
buffer.add(identifier + " " + word + "\n");
3531
return true;

java-projects/src/main/java/modules/LoggingModule.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import com.google.inject.name.Named;
66
import logger.FastLogger;
77
import logger.Logger;
8+
import logger.SugaredLogger;
89

910
import java.io.FileNotFoundException;
1011
import java.io.FileOutputStream;
@@ -19,7 +20,14 @@ public void configure() {
1920
}
2021

2122
@Provides
23+
@Named("file-output-stream")
2224
public OutputStream getOutputStream(@Named("output-file.name") String fileLocation) throws FileNotFoundException {
2325
return new FileOutputStream(fileLocation);
2426
}
27+
28+
@Provides
29+
@Named("console-output-stream")
30+
public OutputStream getConsoleOutputStream(@Named("output-file.name") String fileLocation) throws FileNotFoundException {
31+
return System.out;
32+
}
2533
}

0 commit comments

Comments
 (0)