use of org.apache.syncope.core.logic.audit.AuditAppender 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.logic.audit.AuditAppender in project syncope by apache.
the class LoggerLoader method addAppenderToContext.
public void addAppenderToContext(final LoggerContext ctx, final AuditAppender auditAppender, final LoggerConfig eventLogConf) {
Appender targetAppender = ctx.getConfiguration().getAppender(auditAppender.getTargetAppenderName());
if (targetAppender == null) {
targetAppender = auditAppender.getTargetAppender();
}
targetAppender.start();
ctx.getConfiguration().addAppender(targetAppender);
if (auditAppender.isRewriteEnabled()) {
RewriteAppender rewriteAppender = ctx.getConfiguration().getAppender(auditAppender.getTargetAppenderName() + "_rewrite");
if (rewriteAppender == null) {
rewriteAppender = auditAppender.getRewriteAppender();
}
rewriteAppender.start();
ctx.getConfiguration().addAppender(rewriteAppender);
eventLogConf.addAppender(rewriteAppender, Level.DEBUG, null);
} else {
eventLogConf.addAppender(targetAppender, Level.DEBUG, null);
}
}
use of org.apache.syncope.core.logic.audit.AuditAppender in project syncope by apache.
the class LoggerLoader method load.
@Override
public void load() {
final LoggerContext ctx = (LoggerContext) LogManager.getContext(false);
ctx.getConfiguration().getAppenders().entrySet().stream().filter(entry -> (entry.getValue() instanceof MemoryAppender)).forEachOrdered(entry -> {
memoryAppenders.put(entry.getKey(), (MemoryAppender) entry.getValue());
});
// Audit table and DataSource for each configured domain
ColumnConfig[] columnConfigs = { ColumnConfig.newBuilder().setConfiguration(ctx.getConfiguration()).setName("EVENT_DATE").setEventTimestamp(true).build(), ColumnConfig.newBuilder().setUnicode(false).setConfiguration(ctx.getConfiguration()).setName("LOGGER_LEVEL").setPattern("%level").build(), ColumnConfig.newBuilder().setUnicode(false).setConfiguration(ctx.getConfiguration()).setName("LOGGER").setPattern("%logger").build(), ColumnConfig.newBuilder().setUnicode(false).setConfiguration(ctx.getConfiguration()).setName("MESSAGE").setPattern("%message").build(), ColumnConfig.newBuilder().setUnicode(false).setConfiguration(ctx.getConfiguration()).setName("THROWABLE").setPattern("%ex{full}").build() };
ColumnMapping[] columnMappings = new ColumnMapping[0];
for (Map.Entry<String, DataSource> entry : domainsHolder.getDomains().entrySet()) {
Appender appender = ctx.getConfiguration().getAppender("audit_for_" + entry.getKey());
if (appender == null) {
appender = JdbcAppender.newBuilder().withName("audit_for_" + entry.getKey()).withIgnoreExceptions(false).setConnectionSource(new DataSourceConnectionSource(entry.getKey(), entry.getValue())).setBufferSize(0).setTableName("SYNCOPEAUDIT").setColumnConfigs(columnConfigs).setColumnMappings(columnMappings).build();
appender.start();
ctx.getConfiguration().addAppender(appender);
}
LoggerConfig logConf = new LoggerConfig(AuditLoggerName.getAuditLoggerName(entry.getKey()), null, false);
logConf.addAppender(appender, Level.DEBUG, null);
logConf.setLevel(Level.DEBUG);
ctx.getConfiguration().addLogger(AuditLoggerName.getAuditLoggerName(entry.getKey()), logConf);
// SYNCOPE-1144 For each custom audit appender class add related appenders to log4j logger
auditAppenders(entry.getKey()).forEach(auditAppender -> {
auditAppender.getEvents().stream().map(event -> AuditLoggerName.getAuditEventLoggerName(entry.getKey(), event.toLoggerName())).forEachOrdered(domainAuditLoggerName -> {
LoggerConfig eventLogConf = ctx.getConfiguration().getLoggerConfig(domainAuditLoggerName);
if (LogManager.ROOT_LOGGER_NAME.equals(eventLogConf.getName())) {
eventLogConf = new LoggerConfig(domainAuditLoggerName, null, false);
}
addAppenderToContext(ctx, auditAppender, eventLogConf);
eventLogConf.setLevel(Level.DEBUG);
if (LogManager.ROOT_LOGGER_NAME.equals(eventLogConf.getName())) {
ctx.getConfiguration().addLogger(domainAuditLoggerName, eventLogConf);
}
});
});
AuthContextUtils.execWithAuthContext(entry.getKey(), () -> {
loggerAccessor.synchronizeLog4J(ctx);
return null;
});
}
ctx.updateLoggers();
}
Aggregations