org.daisy.pipeline.persistence.logging.Slf4jSessionLogger Maven / Gradle / Ivy
package org.daisy.pipeline.persistence.logging;
import java.util.HashMap;
import java.util.Map;
import org.eclipse.persistence.logging.AbstractSessionLog;
import org.eclipse.persistence.logging.SessionLog;
import org.eclipse.persistence.logging.SessionLogEntry;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.google.common.base.Strings;
/**
* This file was taken from https://gist.github.com/msosvi/1325764
*
* This is a wrapper class for SLF4J. It is used when messages need to be logged
* through SLF4J.
*
*
* The Spanish part explains which properties have to be configured to make this run...
*
* Para usar SLF4j para los logs de EclipseLink configuramos la propiedad
* eclipselink.logging.logger
con el valor
* org.eclipse.persistence.logging.Slf4jSessionLogger
*
*
* La configuración del nivel de los logs no se realiza en EclipseLink (con la
* propiedad eclipselink.logging.level), sino en la implementación de SLF4J.
*
* Se puede usar el resto de las propiedades de logging de EclipseLink
* (eclipselink.logging.timestamp, eclipselink.logging.thread,
* eclipselink.logging.session, eclipselink.logging.connection
* y eclipselink.logging.parameters) para configurar el formato de salida.
*
* Se usan las siguientes categorias de log:
*
*
* - org.eclipse.persistence.logging.default
*
- org.eclipse.persistence.logging.sql
*
- org.eclipse.persistence.logging.transaction
*
- org.eclipse.persistence.logging.event
*
- org.eclipse.persistence.logging.connection
*
- org.eclipse.persistence.logging.query
*
- org.eclipse.persistence.logging.cache
*
- org.eclipse.persistence.logging.propagation
*
- org.eclipse.persistence.logging.sequencing
*
- org.eclipse.persistence.logging.ejb
*
- org.eclipse.persistence.logging.ejb_or_metadata
*
- org.eclipse.persistence.logging.weaver
*
- org.eclipse.persistence.logging.properties
*
- org.eclipse.persistence.logging.server
*
*
*
* Los niveles de log de EclipseLink y SLF4J son distintos, se realiza la
* siguiente correspondencia:
*
*
* - ALL,FINER,FINEST -> TRACE
*
- FINE -> DEBUG
*
- CONFIG,INFO -> INFO
*
- WARNING -> WARN
*
- SEVERE -> ERROR
*
*
*
*
* @author Miguel Angel Sosvilla Luis.
*
*/
public class Slf4jSessionLogger extends AbstractSessionLog {
public static final String ECLIPSELINK_NAMESPACE = "org.eclipse.persistence.logging";
public static final String DEFAULT_CATEGORY = "default";
public static final String DEFAULT_ECLIPSELINK_NAMESPACE = ECLIPSELINK_NAMESPACE
+ "." + DEFAULT_CATEGORY;
private Map mapLevels;
private Map categoryLoggers = new HashMap();
public Slf4jSessionLogger() {
super();
createCategoryLoggers();
initMapLevels();
}
@Override
public void log(SessionLogEntry entry) {
if (!shouldLog(entry.getLevel(), entry.getNameSpace())) {
return;
}
Logger logger = getLogger(entry.getNameSpace());
LogLevel logLevel = getLogLevel(entry.getLevel());
StringBuilder message = new StringBuilder();
message.append(getSupplementDetailString(entry));
message.append(formatMessage(entry));
switch (logLevel) {
case TRACE:
logger.trace(message.toString());
break;
case DEBUG:
logger.debug(message.toString());
break;
case INFO:
logger.info(message.toString());
break;
case WARN:
logger.warn(message.toString());
break;
case ERROR:
logger.error(message.toString());
break;
case OFF:
break;
}
}
@Override
public boolean shouldLog(int level, String category) {
Logger logger = getLogger(category);
boolean resp = false;
LogLevel logLevel = getLogLevel(level);
switch (logLevel) {
case TRACE:
resp = logger.isTraceEnabled();
break;
case DEBUG:
resp = logger.isDebugEnabled();
break;
case INFO:
resp = logger.isInfoEnabled();
break;
case WARN:
resp = logger.isWarnEnabled();
break;
case ERROR:
resp = logger.isErrorEnabled();
break;
case OFF:
resp = logger.isErrorEnabled();
break;
}
return resp;
}
@Override
public boolean shouldLog(int level) {
return shouldLog(level, "default");
}
/**
* Return true if SQL logging should log visible bind parameters. If the
* shouldDisplayData is not set, return false.
*/
//@Override
//public boolean shouldDisplayData() {
//if (this.shouldDisplayData != null) {
//return shouldDisplayData.booleanValue();
//} else {
//return false;
//}
//}
/**
* Initialize loggers eagerly
*/
private void createCategoryLoggers() {
for (String category : SessionLog.loggerCatagories) {
addLogger(category, ECLIPSELINK_NAMESPACE + "." + category);
}
// Logger default para cuando no hay categoría.
addLogger(DEFAULT_CATEGORY, DEFAULT_ECLIPSELINK_NAMESPACE);
}
/**
* INTERNAL: Add Logger to the categoryLoggers.
*/
private void addLogger(String loggerCategory, String loggerNameSpace) {
categoryLoggers.put(loggerCategory,
LoggerFactory.getLogger(loggerNameSpace));
}
/**
* INTERNAL: Return the Logger for the given category
*/
private Logger getLogger(String category) {
if (!Strings.isNullOrEmpty(category)
|| !this.categoryLoggers.containsKey(category)) {
category = DEFAULT_CATEGORY;
}
return categoryLoggers.get(category);
}
/**
* Return the corresponding Slf4j Level for a given EclipseLink level.
*/
private LogLevel getLogLevel(Integer level) {
LogLevel logLevel = mapLevels.get(level);
if (logLevel == null)
logLevel = LogLevel.OFF;
return logLevel;
}
/**
* SLF4J log levels.
*
* @author Miguel Angel Sosvilla Luis
*
*/
enum LogLevel {
TRACE, DEBUG, INFO, WARN, ERROR, OFF
}
/**
* Relación de los niveles de log de EclipseLink y los de SLF4J
*/
private void initMapLevels() {
mapLevels = new HashMap();
mapLevels.put(SessionLog.ALL, LogLevel.TRACE);
mapLevels.put(SessionLog.FINEST, LogLevel.TRACE);
mapLevels.put(SessionLog.FINER, LogLevel.TRACE);
mapLevels.put(SessionLog.FINE, LogLevel.DEBUG);
mapLevels.put(SessionLog.CONFIG, LogLevel.INFO);
mapLevels.put(SessionLog.INFO, LogLevel.INFO);
mapLevels.put(SessionLog.WARNING, LogLevel.WARN);
mapLevels.put(SessionLog.SEVERE, LogLevel.ERROR);
}
}