Skip to content

Commit

Permalink
system log updates
Browse files Browse the repository at this point in the history
  • Loading branch information
seed-master committed Aug 26, 2023
1 parent cf781cc commit 43e9f5c
Show file tree
Hide file tree
Showing 46 changed files with 238 additions and 123 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@

import org.seed.C;
import org.seed.InternalException;
import org.seed.core.config.SystemLog;
import org.seed.core.data.FileObject;
import org.seed.core.user.Authorisation;
import org.seed.core.user.User;
Expand Down Expand Up @@ -103,6 +104,7 @@ public static FileObject toFileObject(MultipartFile multipartFile) {
return fileObject;
}
catch (IOException ex) {
SystemLog.logError(ex);
throw new InternalException(ex);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import org.seed.C;
import org.seed.InternalException;
import org.seed.core.application.AbstractApplicationEntity;
import org.seed.core.config.SystemLog;
import org.seed.core.data.AbstractSystemEntityService;
import org.seed.core.data.ValidationException;
import org.seed.core.util.Assert;
Expand Down Expand Up @@ -99,6 +100,7 @@ public Module readModule(InputStream inputStream) {
return transfer.readModule(inputStream);
}
catch (IOException ioex) {
SystemLog.logError(ioex);
throw new InternalException(ioex);
}
}
Expand All @@ -110,6 +112,7 @@ public Module readModuleFromDir(String moduleName) {
return transfer.readModuleFromDir(moduleName);
}
catch (IOException ioex) {
SystemLog.logError(ioex);
throw new InternalException(ioex);
}
}
Expand All @@ -121,6 +124,7 @@ public byte[] exportModule(Module module) {
return transfer.exportModule(module);
}
catch (IOException ioex) {
SystemLog.logError(ioex);
throw new InternalException(ioex);
}
}
Expand All @@ -132,6 +136,7 @@ public void exportModuleToDir(Module module) {
transfer.exportModuleToDir(module);
}
catch (IOException ioex) {
SystemLog.logError(ioex);
throw new InternalException(ioex);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@
import org.seed.core.config.ApplicationProperties;
import org.seed.core.config.SchemaVersion;
import org.seed.core.config.SessionProvider;
import org.seed.core.config.SystemLog;
import org.seed.core.config.UpdatableConfiguration;
import org.seed.core.customcode.CustomLib;
import org.seed.core.customcode.CustomLibMetadata;
Expand Down Expand Up @@ -88,6 +89,9 @@ public class ModuleTransfer {
@Autowired
private SessionProvider sessionProvider;

@Autowired
private SystemLog systemLog;

@Autowired
private List<ApplicationEntityService<?>> applicationServices;

Expand Down Expand Up @@ -338,6 +342,7 @@ void importModule(Module module) throws ValidationException {
tx = session.beginTransaction();
importModule(module, session, sortedServices);
tx.commit();
systemLog.logInfo("systemlog.info.moduleimported", module.getName());
}
catch (Exception ex) {
if (tx != null) {
Expand All @@ -346,6 +351,7 @@ void importModule(Module module) throws ValidationException {
if (ex instanceof ValidationException) {
throw (ValidationException) ex;
}
systemLog.logError("systemlog.error.moduleimport", ex, module.getName());
throw new InternalException(ex);
}
}
Expand Down Expand Up @@ -469,6 +475,7 @@ private synchronized Jaxb2Marshaller getMarshaller() {
marshaller.afterPropertiesSet();
}
catch (Exception ex) {
SystemLog.logError(ex);
throw new InternalException(ex);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import org.seed.C;
import org.seed.InternalException;
import org.seed.Seed;
import org.seed.core.config.SystemLog;
import org.seed.core.config.UpdatableConfiguration;
import org.seed.core.data.ValidationException;
import org.seed.core.util.Assert;
Expand Down Expand Up @@ -141,6 +142,7 @@ else if (appSetting != null) {
if (tx != null) {
tx.rollback();
}
SystemLog.logError(ex);
throw new InternalException(ex);
}
}
Expand Down
37 changes: 31 additions & 6 deletions src/main/java/org/seed/core/config/SystemLog.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,16 +21,20 @@
import java.util.List;
import java.util.concurrent.CopyOnWriteArrayList;

import org.apache.commons.lang3.exception.ExceptionUtils;

import org.seed.Seed;
import org.seed.core.util.Assert;
import org.seed.core.util.ExceptionUtils;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

@Component
public class SystemLog {

private static final Logger log = LoggerFactory.getLogger(SystemLog.class);

private final List<LogEntry> entries = new CopyOnWriteArrayList<>();

private volatile boolean errorOccured = false;
Expand Down Expand Up @@ -74,14 +78,35 @@ private void log(LogLevel level, String labelKey,
errorOccured = true;
}
if (throwable != null) {
details = StringUtils.hasText(throwable.getMessage())
? throwable.getMessage()
: ExceptionUtils.getStackTraceAsString(throwable);
details = ExceptionUtils.getStackTrace(throwable);
}
entries.add(new LogEntry(level, labelKey, details, params));
}

public static void logError(Throwable throwable) {
try {
final var systemLog = Seed.getBean(SystemLog.class);
final var callerInfo =
StackWalker.getInstance(StackWalker.Option.RETAIN_CLASS_REFERENCE)
.walk(frames ->
frames.skip(1)
.findFirst()
.map(frame -> frame.getDeclaringClass().getSimpleName() + '.' +
frame.getMethodName() + " line:" +
frame.getLineNumber()));
if (callerInfo.isPresent()) {
systemLog.logError("systemlog.error.message", throwable, callerInfo.get());
}
else {
systemLog.logError("systemlog.error.unknown", throwable);
}
}
catch (Exception ex) {
log.warn("log error", ex);
}
}

public class LogEntry {
public static class LogEntry {

private final LogLevel level;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@

import org.seed.C;
import org.seed.InternalException;
import org.seed.core.config.SystemLog;
import org.seed.core.data.SystemEntity;
import org.seed.core.util.Assert;
import org.seed.core.util.MiscUtils;
Expand Down Expand Up @@ -132,6 +133,7 @@ protected void addChange(AbstractCustomChange customChange) {
getChangeSet().addChange(changeWrapper);
}
catch (CustomChangeException ccex) {
SystemLog.logError(ccex);
throw new InternalException(ccex);
}
}
Expand Down Expand Up @@ -162,6 +164,7 @@ private static ChangeLog createChangeLog(ChangeSet changeSet) {
return changeLog;
}
catch (IOException ioex) {
SystemLog.logError(ioex);
throw new InternalException(ioex);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import org.seed.C;
import org.seed.InternalException;
import org.seed.core.config.SessionProvider;
import org.seed.core.config.SystemLog;
import org.seed.core.util.Assert;

import org.springframework.beans.factory.annotation.Autowired;
Expand Down Expand Up @@ -191,6 +192,7 @@ protected Session getSession() {
}

protected static void handleException(Exception ex) {
SystemLog.logError(ex);
if (ex instanceof RuntimeException) {
throw (RuntimeException) ex;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@

import org.seed.C;
import org.seed.InternalException;
import org.seed.core.config.SystemLog;
import org.seed.core.util.Assert;
import org.seed.core.util.BeanUtils;

Expand All @@ -45,6 +46,7 @@ public T createInstance(@Nullable Options options) {
return instance;
}
catch (Exception ex) {
SystemLog.logError(ex);
throw new InternalException(ex);
}
}
Expand Down Expand Up @@ -121,6 +123,7 @@ protected void saveObjectDirectly(T object, Session session) {
tx.commit();
}
catch (Exception ex) {
SystemLog.logError(ex);
if (tx != null) {
tx.rollback();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
import org.hibernate.query.internal.AbstractProducedQuery;

import org.seed.InternalException;
import org.seed.core.config.SystemLog;
import org.seed.core.data.AbstractSystemEntityRepository;
import org.seed.core.data.SystemObject;
import org.seed.core.util.Assert;
Expand Down Expand Up @@ -69,6 +70,7 @@ public DataSourceResult query(IDataSource dataSource, Map<String, Object> parame
getMetadata(dataSource, parameters, session));
}
catch (SQLException ex) {
SystemLog.logError(ex);
throw new InternalException(ex);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import org.seed.core.api.AbstractFunctionContext;
import org.seed.core.api.DataSource;
import org.seed.core.api.DataSourceProvider;
import org.seed.core.config.SystemLog;
import org.seed.core.data.ValidationException;
import org.seed.core.util.Assert;

Expand Down Expand Up @@ -58,6 +59,7 @@ public List<Object[]> query(DataSource dataSource, Map<String, Object> parameter
.getResultList();
}
catch (ValidationException vex) {
SystemLog.logError(vex);
throw new InternalException(vex);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ public class DBObjectMetadata extends AbstractApplicationEntity

private static final String NAME_SUFFIX = "($|" + PATTERN_NEIGHBOR + ")";

private static final String SQL_COMMENT_PATTERN = "--.*|(\"(?:\\\\[^\"]|\\\\\"|.)*?\")|(?s)/\\*.*?\\*/";
private static final String SQL_COMMENT_PATTERN = "--.*|(\"(?:\\\\[^\"]|\\\\\"|.)*+\")|(?s)/\\*.*?\\*/";

private DBObjectType type;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import org.hibernate.Session;
import org.seed.C;
import org.seed.InternalException;
import org.seed.core.config.SystemLog;
import org.seed.core.data.AbstractSystemEntityService;
import org.seed.core.data.SystemEntityValidator;
import org.seed.core.data.ValidationException;
Expand Down Expand Up @@ -70,6 +71,7 @@ public String getNextValue(EntityField entityField, @Nullable Session session) {
super.saveObject(autonum, session);
}
catch (ValidationException e) {
SystemLog.logError(e);
throw new InternalException(e);
}
return pattern != null
Expand Down
46 changes: 33 additions & 13 deletions src/main/java/org/seed/core/entity/filter/FilterCriterion.java
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,9 @@ public class FilterCriterion extends AbstractTransferableObject {

@XmlAttribute
public String getEntityFieldUid() {
return entityField != null ? entityField.getUid() : entityFieldUid;
return entityField != null
? entityField.getUid()
: entityFieldUid;
}

public void setEntityFieldUid(String entityFieldUid) {
Expand Down Expand Up @@ -241,7 +243,9 @@ public TransferableObject getReference() {

public void setReference(TransferableObject reference) {
this.reference = reference;
referenceUid = reference != null ? reference.getUid() : null;
referenceUid = reference != null
? reference.getUid()
: null;
}

void setValueObject(ValueObject valueObject) {
Expand Down Expand Up @@ -368,38 +372,54 @@ public void setValue(Object value) {
case AUTONUM:
case TEXT:
case TEXTLONG:
if (entityField != null && entityField.isUidField()) {
referenceUid = (String) value;
if (value instanceof String) {
if (entityField != null && entityField.isUidField()) {
referenceUid = (String) value;
}
stringValue = (String) value;
}
stringValue = (String) value;
break;

case BOOLEAN:
booleanValue = (Boolean) value;
if (value instanceof Boolean) {
booleanValue = (Boolean) value;
}
break;

case INTEGER:
integerValue = (Integer) value;
if (value instanceof Integer) {
integerValue = (Integer) value;
}
break;

case LONG:
longValue = (Long) value;
if (value instanceof Long) {
longValue = (Long) value;
}
break;

case DOUBLE:
doubleValue = (Double) value;
if (value instanceof Double) {
doubleValue = (Double) value;
}
break;

case DECIMAL:
decimalValue = (BigDecimal) value;
if (value instanceof BigDecimal) {
decimalValue = (BigDecimal) value;
}
break;

case DATE:
dateValue = (Date) value;
if (value instanceof Date) {
dateValue = (Date) value;
}
break;

case DATETIME:
dateTimeValue = (Date) value;
if (value instanceof Date) {
dateTimeValue = (Date) value;
}
break;

case REFERENCE:
Expand All @@ -409,7 +429,7 @@ public void setValue(Object value) {
else if (value instanceof ValueObject) {
valueObject = (ValueObject) value;
}
else {
else if (value instanceof String) {
referenceUid = (String) value;
}
break;
Expand Down
Loading

0 comments on commit 43e9f5c

Please sign in to comment.