Search in sources :

Example 1 with AuditAppender

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;
}
Also used : LoggerTO(org.apache.syncope.common.lib.log.LoggerTO) AuditAppender(org.apache.syncope.core.logic.audit.AuditAppender) Logger(org.apache.syncope.core.persistence.api.entity.Logger) LoggerContext(org.apache.logging.log4j.core.LoggerContext) LoggerConfig(org.apache.logging.log4j.core.config.LoggerConfig)

Example 2 with AuditAppender

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);
    }
}
Also used : Appender(org.apache.logging.log4j.core.Appender) JdbcAppender(org.apache.logging.log4j.core.appender.db.jdbc.JdbcAppender) RewriteAppender(org.apache.logging.log4j.core.appender.rewrite.RewriteAppender) MemoryAppender(org.apache.syncope.core.logic.MemoryAppender) AuditAppender(org.apache.syncope.core.logic.audit.AuditAppender) RewriteAppender(org.apache.logging.log4j.core.appender.rewrite.RewriteAppender)

Example 3 with AuditAppender

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();
}
Also used : LoggerConfig(org.apache.logging.log4j.core.config.LoggerConfig) Connection(java.sql.Connection) ColumnMapping(org.apache.logging.log4j.core.appender.db.ColumnMapping) DataSourceUtils(org.springframework.jdbc.datasource.DataSourceUtils) SyncopeLoader(org.apache.syncope.core.persistence.api.SyncopeLoader) LoggerContext(org.apache.logging.log4j.core.LoggerContext) ColumnConfig(org.apache.logging.log4j.core.appender.db.jdbc.ColumnConfig) Appender(org.apache.logging.log4j.core.Appender) Level(org.apache.logging.log4j.Level) Autowired(org.springframework.beans.factory.annotation.Autowired) HashMap(java.util.HashMap) AbstractConnectionSource(org.apache.logging.log4j.core.appender.db.jdbc.AbstractConnectionSource) AbstractBeanDefinition(org.springframework.beans.factory.support.AbstractBeanDefinition) JdbcAppender(org.apache.logging.log4j.core.appender.db.jdbc.JdbcAppender) ArrayList(java.util.ArrayList) RewriteAppender(org.apache.logging.log4j.core.appender.rewrite.RewriteAppender) MemoryAppender(org.apache.syncope.core.logic.MemoryAppender) SQLException(java.sql.SQLException) Map(java.util.Map) DataSource(javax.sql.DataSource) AuthContextUtils(org.apache.syncope.core.spring.security.AuthContextUtils) BeansException(org.springframework.beans.BeansException) ImplementationLookup(org.apache.syncope.core.persistence.api.ImplementationLookup) List(java.util.List) Component(org.springframework.stereotype.Component) DomainsHolder(org.apache.syncope.core.persistence.api.DomainsHolder) AuditLoggerName(org.apache.syncope.common.lib.types.AuditLoggerName) AuditAppender(org.apache.syncope.core.logic.audit.AuditAppender) ApplicationContextProvider(org.apache.syncope.core.spring.ApplicationContextProvider) LogManager(org.apache.logging.log4j.LogManager) MemoryAppender(org.apache.syncope.core.logic.MemoryAppender) Appender(org.apache.logging.log4j.core.Appender) JdbcAppender(org.apache.logging.log4j.core.appender.db.jdbc.JdbcAppender) RewriteAppender(org.apache.logging.log4j.core.appender.rewrite.RewriteAppender) MemoryAppender(org.apache.syncope.core.logic.MemoryAppender) AuditAppender(org.apache.syncope.core.logic.audit.AuditAppender) ColumnConfig(org.apache.logging.log4j.core.appender.db.jdbc.ColumnConfig) LoggerContext(org.apache.logging.log4j.core.LoggerContext) DataSource(javax.sql.DataSource) LoggerConfig(org.apache.logging.log4j.core.config.LoggerConfig) HashMap(java.util.HashMap) Map(java.util.Map) ColumnMapping(org.apache.logging.log4j.core.appender.db.ColumnMapping)

Aggregations

AuditAppender (org.apache.syncope.core.logic.audit.AuditAppender)3 Appender (org.apache.logging.log4j.core.Appender)2 LoggerContext (org.apache.logging.log4j.core.LoggerContext)2 JdbcAppender (org.apache.logging.log4j.core.appender.db.jdbc.JdbcAppender)2 RewriteAppender (org.apache.logging.log4j.core.appender.rewrite.RewriteAppender)2 LoggerConfig (org.apache.logging.log4j.core.config.LoggerConfig)2 MemoryAppender (org.apache.syncope.core.logic.MemoryAppender)2 Connection (java.sql.Connection)1 SQLException (java.sql.SQLException)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 List (java.util.List)1 Map (java.util.Map)1 DataSource (javax.sql.DataSource)1 Level (org.apache.logging.log4j.Level)1 LogManager (org.apache.logging.log4j.LogManager)1 ColumnMapping (org.apache.logging.log4j.core.appender.db.ColumnMapping)1 AbstractConnectionSource (org.apache.logging.log4j.core.appender.db.jdbc.AbstractConnectionSource)1 ColumnConfig (org.apache.logging.log4j.core.appender.db.jdbc.ColumnConfig)1 LoggerTO (org.apache.syncope.common.lib.log.LoggerTO)1