Skip to content

Commit df79aee

Browse files
committed
multithreaded logger
1 parent 03295d8 commit df79aee

19 files changed

+424
-1
lines changed

src/com/lld/kafka/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<b> Design a message broker which has the following properties: </b>
1+
<b> Design a applicationAwareMessage broker which has the following properties: </b>
22

33
1. Producers should be able to produce messages.
44
2. Producers can be divided into multiple partitions.
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package com.lld.multithreadedlogging;
2+
3+
import com.lld.multithreadedlogging.service.QueueService;
4+
import com.lld.multithreadedlogging.service.impl.QueueServiceImpl;
5+
import com.lld.multithreadedlogging.worker.Consumer;
6+
import com.lld.multithreadedlogging.worker.Producer;
7+
8+
import java.util.concurrent.ExecutorService;
9+
import java.util.concurrent.Executors;
10+
11+
public class MainApplication {
12+
private static void test1() {
13+
QueueService queueService = new QueueServiceImpl(100);
14+
ExecutorService executorService = Executors.newFixedThreadPool(3);
15+
executorService.execute(new Producer(queueService));
16+
executorService.execute(new Producer(queueService));
17+
executorService.execute(new Consumer(queueService));
18+
executorService.shutdown();
19+
}
20+
21+
public static void main(String[] args) {
22+
test1();
23+
}
24+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
Design a logging system:
2+
3+
1. The system supports multiple log level like error, debug, warning, info etc.
4+
2. Multiple log mediums are supported like file based logs, network logs, db logs etc.
5+
3. Multiple producers can produce to the queue simultaneously.
6+
4. Irrespective of the rate of production, the system should consume messages as fast
7+
as possible from the queue and flush them to the log medium.
8+
5. A single applicationAwareMessage can get published to multiple medium.
9+
6.
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package com.lld.multithreadedlogging.configuration;
2+
3+
public class Config {
4+
public final static int DEFAULT_PUBLISH_TIMEOUT_MS = 100;
5+
public final static int DEFAULT_CONSUMPTION_TIMEOUT_MS = 100;
6+
public final static int DEFAULT_CAPACITY = 100;
7+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package com.lld.multithreadedlogging.constant;
2+
3+
public enum LogLevel {
4+
INFO,
5+
DEBUG,
6+
WARN,
7+
ERROR
8+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package com.lld.multithreadedlogging.constant;
2+
3+
public enum SupportedLogMedium {
4+
FILE,
5+
NETWORK,
6+
DB
7+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package com.lld.multithreadedlogging.logger;
2+
3+
import com.lld.multithreadedlogging.model.message.ApplicationAwareMessage;
4+
5+
public interface ApplicationAwareLogger {
6+
void info(ApplicationAwareMessage applicationAwareMessage) throws InterruptedException;
7+
void debug(ApplicationAwareMessage applicationAwareMessage) throws InterruptedException;
8+
void warn(ApplicationAwareMessage applicationAwareMessage) throws InterruptedException;
9+
void error(ApplicationAwareMessage applicationAwareMessage) throws InterruptedException;
10+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package com.lld.multithreadedlogging.logger;
2+
3+
import com.lld.multithreadedlogging.model.message.ApplicationAwareMessage;
4+
import com.lld.multithreadedlogging.model.message.WritableMessage;
5+
6+
public interface InternalLogger {
7+
void log(WritableMessage message);
8+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package com.lld.multithreadedlogging.logger.factory;
2+
3+
import com.lld.multithreadedlogging.constant.SupportedLogMedium;
4+
import com.lld.multithreadedlogging.logger.InternalLogger;
5+
import com.lld.multithreadedlogging.logger.impl.DBInternalLogger;
6+
import com.lld.multithreadedlogging.logger.impl.FileInternalLogger;
7+
import com.lld.multithreadedlogging.logger.impl.NetworkInternalLogger;
8+
9+
public class InternalLoggerSimpleFactory {
10+
public static InternalLogger getLoggerInstance(SupportedLogMedium medium) {
11+
switch (medium) {
12+
case DB:
13+
return DBInternalLogger.getInstance();
14+
case FILE:
15+
return FileInternalLogger.getInstance();
16+
case NETWORK:
17+
return NetworkInternalLogger.getInstance();
18+
default:
19+
return null;
20+
}
21+
}
22+
}
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
package com.lld.multithreadedlogging.logger.impl;
2+
3+
import com.lld.multithreadedlogging.constant.LogLevel;
4+
import com.lld.multithreadedlogging.constant.SupportedLogMedium;
5+
import com.lld.multithreadedlogging.model.message.ApplicationAwareMessage;
6+
import com.lld.multithreadedlogging.model.message.WritableMessage;
7+
import com.lld.multithreadedlogging.logger.ApplicationAwareLogger;
8+
import com.lld.multithreadedlogging.service.QueueService;
9+
10+
import java.util.ArrayList;
11+
import java.util.List;
12+
13+
/**
14+
* Applications will talk to this logger instance to publish message to the system
15+
*/
16+
public class ApplicationAwareLoggerImpl implements ApplicationAwareLogger {
17+
QueueService queueService;
18+
19+
public ApplicationAwareLoggerImpl(QueueService queueService) {
20+
this.queueService = queueService;
21+
}
22+
@Override
23+
public void info(ApplicationAwareMessage applicationAwareMessage) throws InterruptedException {
24+
this.queueService.publish(getWritableMessages(applicationAwareMessage, LogLevel.INFO));
25+
}
26+
27+
@Override
28+
public void debug(ApplicationAwareMessage applicationAwareMessage) throws InterruptedException {
29+
this.queueService.publish(getWritableMessages(applicationAwareMessage, LogLevel.DEBUG));
30+
}
31+
32+
@Override
33+
public void warn(ApplicationAwareMessage applicationAwareMessage) throws InterruptedException {
34+
this.queueService.publish(getWritableMessages(applicationAwareMessage, LogLevel.WARN));
35+
}
36+
37+
@Override
38+
public void error(ApplicationAwareMessage applicationAwareMessage) throws InterruptedException {
39+
this.queueService.publish(getWritableMessages(applicationAwareMessage, LogLevel.ERROR));
40+
}
41+
42+
private List<WritableMessage> getWritableMessages(ApplicationAwareMessage applicationAwareMessage, LogLevel logLevel) {
43+
List<WritableMessage> writableMessages = new ArrayList<>();
44+
for (SupportedLogMedium supportedLogMedium: applicationAwareMessage.getMediums()) {
45+
writableMessages.add(new WritableMessage(applicationAwareMessage, logLevel, supportedLogMedium));
46+
}
47+
48+
return writableMessages;
49+
}
50+
}

0 commit comments

Comments
 (0)