use of org.apache.syncope.core.persistence.api.entity.Logger in project syncope by apache.
the class JPALoggerDAO method delete.
@Override
public void delete(final String key) {
Logger logger = find(key);
if (logger == null) {
return;
}
delete(logger);
}
use of org.apache.syncope.core.persistence.api.entity.Logger in project syncope by apache.
the class LoggerLogic method setLevel.
private LoggerTO setLevel(final String name, final Level level, final LoggerType expectedType) {
Logger syncopeLogger = loggerDAO.find(name);
if (syncopeLogger == null) {
LOG.debug("Logger {} not found: creating new...", name);
syncopeLogger = entityFactory.newEntity(Logger.class);
syncopeLogger.setKey(name);
syncopeLogger.setType(name.startsWith(LoggerType.AUDIT.getPrefix()) ? LoggerType.AUDIT : LoggerType.LOG);
}
if (expectedType != syncopeLogger.getType()) {
throwInvalidLogger(expectedType);
}
syncopeLogger.setLevel(LoggerLevel.fromLevel(level));
syncopeLogger = loggerDAO.save(syncopeLogger);
LoggerContext ctx = (LoggerContext) LogManager.getContext(false);
LoggerConfig logConf;
if (LoggerType.AUDIT.equals(syncopeLogger.getType())) {
String auditLoggerName = AuditLoggerName.getAuditEventLoggerName(AuthContextUtils.getDomain(), syncopeLogger.getKey());
logConf = ctx.getConfiguration().getLoggerConfig(auditLoggerName);
// SYNCOPE-1144 For each custom audit appender class add related appenders to log4j logger
boolean isRootLogConf = LogManager.ROOT_LOGGER_NAME.equals(logConf.getName());
if (isRootLogConf) {
logConf = new LoggerConfig(auditLoggerName, null, false);
}
for (AuditAppender auditAppender : loggerLoader.auditAppenders(AuthContextUtils.getDomain())) {
if (auditAppender.getEvents().stream().anyMatch(event -> name.equalsIgnoreCase(event.toLoggerName()))) {
loggerLoader.addAppenderToContext(ctx, auditAppender, logConf);
}
}
if (isRootLogConf) {
ctx.getConfiguration().addLogger(auditLoggerName, logConf);
}
} else {
logConf = SyncopeConstants.ROOT_LOGGER.equals(name) ? ctx.getConfiguration().getLoggerConfig(LogManager.ROOT_LOGGER_NAME) : ctx.getConfiguration().getLoggerConfig(name);
}
logConf.setLevel(level);
ctx.updateLoggers();
LoggerTO result = new LoggerTO();
BeanUtils.copyProperties(syncopeLogger, result);
return result;
}
use of org.apache.syncope.core.persistence.api.entity.Logger in project syncope by apache.
the class LoggerLogic method delete.
private LoggerTO delete(final String name, final LoggerType expectedType) {
Logger syncopeLogger = loggerDAO.find(name);
if (syncopeLogger == null) {
throw new NotFoundException("Logger " + name);
}
if (expectedType != syncopeLogger.getType()) {
throwInvalidLogger(expectedType);
}
LoggerTO loggerToDelete = new LoggerTO();
BeanUtils.copyProperties(syncopeLogger, loggerToDelete);
// remove SyncopeLogger from local storage, so that LoggerLoader won't load this next time
loggerDAO.delete(syncopeLogger);
// set log level to OFF in order to disable configured logger until next reboot
LoggerContext ctx = (LoggerContext) LogManager.getContext(false);
String auditLoggerName = AuditLoggerName.getAuditEventLoggerName(AuthContextUtils.getDomain(), syncopeLogger.getKey());
org.apache.logging.log4j.core.Logger logger = SyncopeConstants.ROOT_LOGGER.equals(name) ? ctx.getLogger(LogManager.ROOT_LOGGER_NAME) : LoggerType.AUDIT.equals(syncopeLogger.getType()) ? ctx.getLogger(auditLoggerName) : ctx.getLogger(name);
logger.setLevel(Level.OFF);
ctx.updateLoggers();
return loggerToDelete;
}
use of org.apache.syncope.core.persistence.api.entity.Logger in project syncope by apache.
the class LoggerAccessor method synchronizeLog4J.
@Transactional
public void synchronizeLog4J(final LoggerContext ctx) {
Map<String, Logger> syncopeLoggers = new HashMap<>();
if (SyncopeConstants.MASTER_DOMAIN.equals(AuthContextUtils.getDomain())) {
for (Logger syncopeLogger : loggerDAO.findAll(LoggerType.LOG)) {
syncopeLoggers.put(syncopeLogger.getKey(), syncopeLogger);
}
}
for (Logger syncopeLogger : loggerDAO.findAll(LoggerType.AUDIT)) {
syncopeLoggers.put(AuditLoggerName.getAuditEventLoggerName(AuthContextUtils.getDomain(), syncopeLogger.getKey()), syncopeLogger);
}
/*
* Traverse all defined log4j loggers: if there is a matching SyncopeLogger, set log4j level accordingly,
* otherwise create a SyncopeLogger instance with given name and level.
*/
for (LoggerConfig logConf : ctx.getConfiguration().getLoggers().values()) {
String loggerName = LogManager.ROOT_LOGGER_NAME.equals(logConf.getName()) ? SyncopeConstants.ROOT_LOGGER : logConf.getName();
if (logConf.getLevel() != null) {
if (syncopeLoggers.containsKey(loggerName)) {
logConf.setLevel(syncopeLoggers.get(loggerName).getLevel().getLevel());
syncopeLoggers.remove(loggerName);
} else if (!loggerName.startsWith(LoggerType.AUDIT.getPrefix()) || !loggerName.startsWith(AuthContextUtils.getDomain() + "." + LoggerType.AUDIT.getPrefix())) {
Logger syncopeLogger = entityFactory.newEntity(Logger.class);
syncopeLogger.setKey(loggerName);
syncopeLogger.setLevel(LoggerLevel.fromLevel(logConf.getLevel()));
syncopeLogger.setType(LoggerType.LOG);
loggerDAO.save(syncopeLogger);
}
}
}
/*
* Foreach SyncopeLogger not found in log4j create a new log4j logger with given name and level.
*/
for (Map.Entry<String, Logger> entry : syncopeLoggers.entrySet()) {
LoggerConfig logConf = ctx.getConfiguration().getLoggerConfig(entry.getKey());
logConf.setLevel(entry.getValue().getLevel().getLevel());
}
ctx.updateLoggers();
}
Aggregations