Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#277 Limit logs maximum size #716

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@
@Model
public class LogContainer {

private final int LOG_MAX_SIZE = 10_000;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nem lenne jobb konfiguralhatova tenni?

  • doksi is kellene


private final List<LogContainer.Log> logs = new ArrayList<>();
private final Map<String, Object> customParam = new HashMap<>();

Expand All @@ -57,7 +59,7 @@ public LogContainer() {
* log message
*/
public void trace(String msg) {
logs.add(new Log(LogLevel.TRACE, msg));
addLog(new Log(LogLevel.TRACE, msg));
}

/**
Expand All @@ -70,7 +72,7 @@ public void trace(String msg) {
*/
public void trace(String format, Object... arguments) {
String message = format(format, arguments);
logs.add(new Log(LogLevel.TRACE, message));
addLog(new Log(LogLevel.TRACE, message));
}

/**
Expand All @@ -83,7 +85,7 @@ public void trace(String format, Object... arguments) {
*/
public void trace(String msg, Throwable t) {
String message = getFullStackTrace(msg, t);
logs.add(new Log(LogLevel.TRACE, message));
addLog(new Log(LogLevel.TRACE, message));
}

/**
Expand All @@ -93,7 +95,7 @@ public void trace(String msg, Throwable t) {
* log message
*/
public void debug(String msg) {
logs.add(new Log(LogLevel.DEBUG, msg));
addLog(new Log(LogLevel.DEBUG, msg));
}

/**
Expand All @@ -106,7 +108,7 @@ public void debug(String msg) {
*/
public void debug(String format, Object... arguments) {
String message = format(format, arguments);
logs.add(new Log(LogLevel.DEBUG, message));
addLog(new Log(LogLevel.DEBUG, message));
}

/**
Expand All @@ -119,7 +121,7 @@ public void debug(String format, Object... arguments) {
*/
public void debug(String msg, Throwable t) {
String message = getFullStackTrace(msg, t);
logs.add(new Log(LogLevel.DEBUG, message));
addLog(new Log(LogLevel.DEBUG, message));
}

/**
Expand All @@ -129,7 +131,7 @@ public void debug(String msg, Throwable t) {
* log message
*/
public void info(String msg) {
logs.add(new Log(LogLevel.INFO, msg));
addLog(new Log(LogLevel.INFO, msg));
}

/**
Expand All @@ -142,7 +144,7 @@ public void info(String msg) {
*/
public void info(String format, Object... arguments) {
String message = format(format, arguments);
logs.add(new Log(LogLevel.INFO, message));
addLog(new Log(LogLevel.INFO, message));
}

/**
Expand All @@ -155,7 +157,7 @@ public void info(String format, Object... arguments) {
*/
public void info(String msg, Throwable t) {
String message = getFullStackTrace(msg, t);
logs.add(new Log(LogLevel.INFO, message));
addLog(new Log(LogLevel.INFO, message));
}

/**
Expand All @@ -165,7 +167,7 @@ public void info(String msg, Throwable t) {
* log message
*/
public void warn(String msg) {
logs.add(new Log(LogLevel.WARN, msg));
addLog(new Log(LogLevel.WARN, msg));
}

/**
Expand All @@ -178,7 +180,7 @@ public void warn(String msg) {
*/
public void warn(String format, Object... arguments) {
String message = format(format, arguments);
logs.add(new Log(LogLevel.WARN, message));
addLog(new Log(LogLevel.WARN, message));
}

/**
Expand All @@ -191,7 +193,7 @@ public void warn(String format, Object... arguments) {
*/
public void warn(String msg, Throwable t) {
String message = getFullStackTrace(msg, t);
logs.add(new Log(LogLevel.WARN, message));
addLog(new Log(LogLevel.WARN, message));
}

/**
Expand All @@ -201,7 +203,7 @@ public void warn(String msg, Throwable t) {
* log message
*/
public void error(String msg) {
logs.add(new Log(LogLevel.ERROR, msg));
addLog(new Log(LogLevel.ERROR, msg));
}

/**
Expand All @@ -214,7 +216,7 @@ public void error(String msg) {
*/
public void error(String format, Object... arguments) {
String message = format(format, arguments);
logs.add(new Log(LogLevel.ERROR, message));
addLog(new Log(LogLevel.ERROR, message));
}

/**
Expand All @@ -227,7 +229,7 @@ public void error(String format, Object... arguments) {
*/
public void error(String msg, Throwable t) {
String message = getFullStackTrace(msg, t);
logs.add(new Log(LogLevel.ERROR, message));
addLog(new Log(LogLevel.ERROR, message));
}

/**
Expand All @@ -251,7 +253,7 @@ public Object getValue(String key) {
*/
public void setValue(String key, Object value) {
String msg = (customParam.containsKey(key) ? "Replaced" : "Added") + " key: [" + key + "], value: [" + value + "]";
logs.add(new Log(LogLevel.CUSTOM, msg));
addLog(new Log(LogLevel.CUSTOM, msg));
customParam.put(key, value);
}

Expand All @@ -262,7 +264,7 @@ public void setValue(String key, Object value) {
* the key to remove
*/
public void removeValue(String key) {
logs.add(new Log(LogLevel.CUSTOM, "Remove key: [" + key + "]"));
addLog(new Log(LogLevel.CUSTOM, "Remove key: [" + key + "]"));
customParam.remove(key);
}

Expand Down Expand Up @@ -367,4 +369,17 @@ public enum LogLevel {
*/
ERROR,
}

private void cleanOlderLogs() {
if (logs.size() >= LOG_MAX_SIZE) {
for (int i = 1; i <= logs.size() - LOG_MAX_SIZE; i++) {
logs.remove(0);
}
}
}

private void addLog(LogContainer.Log log) {
logs.add(log);
cleanOlderLogs();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,18 @@ public void testErrorLogs() {
testLog(logContainer, logLevel);
}

@Test
@DisplayName("Test logs size")
public void testLogsSize() {
LogContainer logContainer = new LogContainer();
for (int i = 0; i < 90_000; i++) {
logContainer.info("Message [%d]", i);
}

String[] logContainerRows = logContainer.toString().split("\\n");
Assertions.assertTrue(logContainerRows.length <= 10_000);
}

private void testLog(LogContainer logContainer, String logLevel) {
String[] logContainerRows = logContainer.toString().split("\\n");
Assertions.assertTrue(
Expand Down
Loading