File tree Expand file tree Collapse file tree 7 files changed +33
-21
lines changed Expand file tree Collapse file tree 7 files changed +33
-21
lines changed Original file line number Diff line number Diff line change 1
1
import com .google .inject .Guice ;
2
2
import com .google .inject .Injector ;
3
+ import logger .FastLogger ;
3
4
import modules .LoggingModule ;
4
5
import modules .PropertiesModule ;
5
6
6
7
import java .util .concurrent .ExecutionException ;
7
8
8
9
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 ());
11
12
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
+ }
13
18
}
14
19
}
Original file line number Diff line number Diff line change
1
+ import com .google .inject .Inject ;
2
+ import com .google .inject .Singleton ;
3
+ import logger .Logger ;
1
4
import logger .SugaredLogger ;
2
5
3
6
import java .util .concurrent .CompletableFuture ;
4
7
8
+ @ Singleton
5
9
public class TaskManager {
6
- private final SugaredLogger logger ;
10
+ private final Logger logger ;
7
11
8
- public TaskManager () {
9
- this .logger = SugaredLogger .getLogger (System .out );
12
+ @ Inject
13
+ public TaskManager (final Logger logger ) {
14
+ this .logger = logger ;
10
15
}
11
16
12
17
public CompletableFuture <Void > execute () {
Original file line number Diff line number Diff line change @@ -18,7 +18,7 @@ public class FastLogger implements Logger {
18
18
19
19
@ Inject
20
20
public FastLogger (@ Named ("logger.fast.identifier" ) final String identifier ,
21
- final OutputStream stream ,
21
+ @ Named ( "console-output-stream" ) final OutputStream stream ,
22
22
@ Named ("logger.fast.buffer.size" ) final Integer bufferSize ) {
23
23
this .identifier = identifier ;
24
24
this .stream = stream ;
Original file line number Diff line number Diff line change 1
1
package logger ;
2
2
3
- import exceptions .LoggingException ;
4
-
5
3
import java .util .concurrent .CompletableFuture ;
6
4
7
5
public interface Logger {
Original file line number Diff line number Diff line change 1
1
package logger ;
2
2
3
+ import com .google .inject .Inject ;
4
+ import com .google .inject .Singleton ;
5
+ import com .google .inject .name .Named ;
3
6
import exceptions .LoggingException ;
4
7
5
8
import java .io .IOException ;
10
13
import java .util .concurrent .ExecutorService ;
11
14
import java .util .concurrent .Executors ;
12
15
13
- public class SugaredLogger {
14
- private static SugaredLogger SUGARED_LOGGER ;
16
+ @ Singleton
17
+ public class SugaredLogger implements Logger {
15
18
private final String identifier ;
16
19
private final OutputStream stream ;
17
20
private final List <String > buffer ;
18
21
19
- private SugaredLogger (final OutputStream stream ) {
22
+ @ Inject
23
+ public SugaredLogger (final OutputStream stream , @ Named ("logger.fast.identifier" ) String identifier ) {
20
24
this .stream = stream ;
21
- identifier = "service:" ;
25
+ this . identifier = identifier ;
22
26
buffer = new ArrayList <>();
23
27
}
24
28
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
-
33
29
public boolean write (final String word ) {
34
30
buffer .add (identifier + " " + word + "\n " );
35
31
return true ;
Original file line number Diff line number Diff line change 5
5
import com .google .inject .name .Named ;
6
6
import logger .FastLogger ;
7
7
import logger .Logger ;
8
+ import logger .SugaredLogger ;
8
9
9
10
import java .io .FileNotFoundException ;
10
11
import java .io .FileOutputStream ;
@@ -19,7 +20,14 @@ public void configure() {
19
20
}
20
21
21
22
@ Provides
23
+ @ Named ("file-output-stream" )
22
24
public OutputStream getOutputStream (@ Named ("output-file.name" ) String fileLocation ) throws FileNotFoundException {
23
25
return new FileOutputStream (fileLocation );
24
26
}
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
+ }
25
33
}
You can’t perform that action at this time.
0 commit comments