Search in sources :

Example 1 with ColumnConfig

use of org.apache.logging.log4j.core.appender.db.jdbc.ColumnConfig 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

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 Appender (org.apache.logging.log4j.core.Appender)1 LoggerContext (org.apache.logging.log4j.core.LoggerContext)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 JdbcAppender (org.apache.logging.log4j.core.appender.db.jdbc.JdbcAppender)1 RewriteAppender (org.apache.logging.log4j.core.appender.rewrite.RewriteAppender)1 LoggerConfig (org.apache.logging.log4j.core.config.LoggerConfig)1 AuditLoggerName (org.apache.syncope.common.lib.types.AuditLoggerName)1 MemoryAppender (org.apache.syncope.core.logic.MemoryAppender)1 AuditAppender (org.apache.syncope.core.logic.audit.AuditAppender)1