net.sourceforge.jbizmo.commons.server.logging.LoggingAspect Maven / Gradle / Ivy
/*
* This file is part of JBizMo, a set of tools, libraries and plug-ins
* for modeling and creating Java-based enterprise applications.
* For more information visit:
*
* http://sourceforge.net/projects/jbizmo/
*
* This software is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This software is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this software; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
package net.sourceforge.jbizmo.commons.server.logging;
import jakarta.inject.Inject;
import net.sourceforge.jbizmo.commons.crud.ConcurrentEntityModificationException;
import net.sourceforge.jbizmo.commons.crud.DuplicateCollectionEntryException;
import net.sourceforge.jbizmo.commons.crud.UniqueConstraintViolationException;
import net.sourceforge.jbizmo.commons.exchange.DataExportException;
import net.sourceforge.jbizmo.commons.exchange.DataImportException;
import net.sourceforge.jbizmo.commons.property.PropertyService;
import net.sourceforge.jbizmo.commons.search.exception.GeneralSearchException;
import net.sourceforge.jbizmo.commons.validation.PropertyConstraintViolationException;
import org.aspectj.lang.ProceedingJoinPoint;
import org.springframework.beans.factory.annotation.Value;
/**
*
* Simple logging aspect
*
*
* Copyright 2014 (C) by Martin Ganserer
*
* @author Martin Ganserer
* @version 1.0.0
*/
public class LoggingAspect {
private @Inject LoggingService logger;
private @Value("${" + PropertyService.PROP_LOGGING_LEVEL + "}") int loggingLevel;
/**
* @param joinPoint
* @return the invocation result
* @throws Throwable if the invocation of the target method has failed
*/
public Object logAround(ProceedingJoinPoint joinPoint) throws Throwable {
final long start = System.currentTimeMillis();
try {
final Object result = joinPoint.proceed();
if (loggingLevel == LoggingService.DEBUG_LEVEL) {
final long end = System.currentTimeMillis();
final String methodName = joinPoint.getSignature().getName();
final String className = joinPoint.getSignature().getDeclaringTypeName();
final var logEntry = new LoggingDTO(className, methodName, null, end - start, null);
logger.debug(logEntry);
}
return result;
}
catch (final Exception e) {
final String methodName = joinPoint.getSignature().getName();
final String className = joinPoint.getSignature().getDeclaringTypeName();
final long end = System.currentTimeMillis();
boolean isError = true;
final var logEntry = new LoggingDTO(className, methodName, e.getMessage(), end - start, e);
// Internal runtime exceptions should be logged as warnings!
if (e.getClass() == ConcurrentEntityModificationException.class)
isError = false;
if (e.getClass() == UniqueConstraintViolationException.class)
isError = false;
if (e.getClass() == DuplicateCollectionEntryException.class)
isError = false;
if (e.getClass() == PropertyConstraintViolationException.class)
isError = false;
if (e.getClass() == GeneralSearchException.class)
isError = false;
if (e.getClass() == DataExportException.class)
isError = false;
if (e.getClass() == DataImportException.class)
isError = false;
if (isError) {
if (loggingLevel >= LoggingService.ERROR_LEVEL)
logger.error(logEntry);
}
else if (loggingLevel >= LoggingService.WARN_LEVEL)
logger.warn(logEntry);
// We just rethrow the original exception!
throw e;
}
}
/**
* @return the logging level
*/
public int getLoggingLevel() {
return loggingLevel;
}
/**
* @param loggingLevel
*/
public void setLoggingLevel(int loggingLevel) {
this.loggingLevel = loggingLevel;
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy