[LOG DB]
configurazione dimensione massima db
This commit is contained in:
@@ -11,6 +11,7 @@ public class LoggerConfigurationModel {
|
||||
private final ObservableField<String> level = new ObservableField<>();
|
||||
private final ObservableField<Integer> deleteDays = new ObservableField<>();
|
||||
private final ObservableField<Integer> dbDeleteDays = new ObservableField<>();
|
||||
private final ObservableField<String> dbMaxSize = new ObservableField<>();
|
||||
|
||||
|
||||
public String getLevel() {
|
||||
@@ -51,4 +52,13 @@ public class LoggerConfigurationModel {
|
||||
this.dbDeleteDays.set(dbDeleteDays);
|
||||
return this;
|
||||
}
|
||||
|
||||
public String getDbMaxSize() {
|
||||
return dbMaxSize.get();
|
||||
}
|
||||
public LoggerConfigurationModel setDbMaxSize(String dbMaxSize) {
|
||||
this.dbMaxSize.set(dbMaxSize);
|
||||
return this;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -107,6 +107,8 @@ public class SettingsController implements IFileWatcherEvents {
|
||||
|| settingsModel.getLoggerConfiguration().getLevel() == null
|
||||
|| settingsModel.getLoggerConfiguration().getDeleteDays() == null
|
||||
|| settingsModel.getLoggerConfiguration().getDbDeleteDays() == null
|
||||
|| settingsModel.getLoggerConfiguration().getDbDeleteDays() == null
|
||||
|| UtilityString.isNullOrEmpty(settingsModel.getLoggerConfiguration().getDbMaxSize())
|
||||
) {
|
||||
LoggerConfigurationModel loggerConfigurationModel = settingsConverter.getConvertedLoggerConfiguration();
|
||||
|
||||
|
||||
@@ -153,6 +153,7 @@ class SettingsConverter {
|
||||
loggerConfigurationModel.setLogLevelEnum(Level.DEBUG);
|
||||
loggerConfigurationModel.setDeleteDays(30);
|
||||
loggerConfigurationModel.setDbDeleteDays(30);
|
||||
loggerConfigurationModel.setDbMaxSize("1G");
|
||||
|
||||
return loggerConfigurationModel;
|
||||
}
|
||||
|
||||
@@ -9,17 +9,29 @@ import it.integry.core.log.dto.*;
|
||||
import it.integry.core.log.enums.FilterMatchMode;
|
||||
import it.integry.ems.looper.service.LooperService;
|
||||
import it.integry.ems.settings.Model.SettingsModel;
|
||||
import it.integry.ems.utility.UtilityDirs;
|
||||
import it.integry.ems_model.utility.UtilityBigDecimal;
|
||||
import it.integry.ems_model.utility.UtilityDB;
|
||||
import it.integry.ems_model.utility.UtilityString;
|
||||
import org.apache.logging.log4j.LogManager;
|
||||
import org.apache.logging.log4j.Logger;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import java.io.File;
|
||||
import java.math.BigDecimal;
|
||||
import java.sql.Connection;
|
||||
import java.sql.PreparedStatement;
|
||||
import java.util.Arrays;
|
||||
import java.util.Collections;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
|
||||
@Service
|
||||
public class LogService {
|
||||
|
||||
private final Logger logger = LogManager.getLogger();
|
||||
|
||||
@Autowired
|
||||
private LooperService looperService;
|
||||
@Autowired
|
||||
@@ -31,9 +43,17 @@ public class LogService {
|
||||
looperService.add(() -> {
|
||||
try {
|
||||
deleteLogs(settingsModel.getLoggerConfiguration().getDbDeleteDays());
|
||||
} catch (Exception ignored) {
|
||||
} catch (Exception exception) {
|
||||
logger.error(exception);
|
||||
}
|
||||
}, 24 * 60 * 60 * 1000, LogService.class.getName());
|
||||
looperService.add(() -> {
|
||||
try {
|
||||
checkLogSize(settingsModel.getLoggerConfiguration().getDbMaxSize());
|
||||
} catch (Exception exception) {
|
||||
logger.error(exception);
|
||||
}
|
||||
}, 5 * 60 * 1000, LogService.class.getName());
|
||||
}
|
||||
|
||||
public List<LogDTO> retrieveLogs(Integer page, Integer pageSize, LogDataDTO logData) throws Exception {
|
||||
@@ -239,4 +259,39 @@ public class LogService {
|
||||
return ps.executeUpdate();
|
||||
}
|
||||
}
|
||||
|
||||
public void checkLogSize(String maxSize) throws Exception {
|
||||
String um = Arrays.stream(maxSize.split("[0-9-.]")).filter(x -> !UtilityString.isNullOrEmpty(x)).findFirst().orElse(null);
|
||||
BigDecimal numericValue = BigDecimal.ONE;
|
||||
if (UtilityString.isNullOrEmpty(um)) {
|
||||
numericValue = new BigDecimal(maxSize);
|
||||
um = "G";
|
||||
} else if (!Arrays.asList("M", "MB", "G", "GB").contains(um.toUpperCase()) || maxSize.indexOf(um) == 0) {
|
||||
um = "G";
|
||||
} else {
|
||||
numericValue = new BigDecimal(maxSize.substring(0, maxSize.indexOf(um)));
|
||||
}
|
||||
|
||||
int pow = 3;
|
||||
if (Arrays.asList("M", "MB").contains(um.toUpperCase())) {
|
||||
pow = 2;
|
||||
}
|
||||
|
||||
long maxBytes = numericValue.multiply(BigDecimal.valueOf(1024).pow(pow)).longValue();
|
||||
|
||||
File logDbFile = new File(UtilityDirs.getConfigPath() + "logs.db");
|
||||
if (logDbFile.length() > maxBytes) {
|
||||
cleanOlderLogs();
|
||||
}
|
||||
}
|
||||
|
||||
private int cleanOlderLogs() throws Exception {
|
||||
int hours = 6;
|
||||
String sql = String.format("DELETE FROM app_logs\n" +
|
||||
" WHERE entry_date < (SELECT entry_date FROM app_logs ORDER BY entry_date LIMIT 1) + (%d * 3600000)", hours);
|
||||
try (Connection connection = ConnectionFactory.getDatabaseConnection();
|
||||
PreparedStatement ps = connection.prepareStatement(sql)) {
|
||||
return ps.executeUpdate();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user