Search in sources :

Example 1 with Logger

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);
}
Also used : Logger(org.apache.syncope.core.persistence.api.entity.Logger) JPALogger(org.apache.syncope.core.persistence.jpa.entity.JPALogger)

Example 2 with 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;
}
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 3 with Logger

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

Example 4 with Logger

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();
}
Also used : HashMap(java.util.HashMap) Logger(org.apache.syncope.core.persistence.api.entity.Logger) HashMap(java.util.HashMap) Map(java.util.Map) LoggerConfig(org.apache.logging.log4j.core.config.LoggerConfig) Transactional(org.springframework.transaction.annotation.Transactional)

Aggregations

Logger (org.apache.syncope.core.persistence.api.entity.Logger)4 LoggerContext (org.apache.logging.log4j.core.LoggerContext)2 LoggerConfig (org.apache.logging.log4j.core.config.LoggerConfig)2 LoggerTO (org.apache.syncope.common.lib.log.LoggerTO)2 HashMap (java.util.HashMap)1 Map (java.util.Map)1 AuditAppender (org.apache.syncope.core.logic.audit.AuditAppender)1 NotFoundException (org.apache.syncope.core.persistence.api.dao.NotFoundException)1 JPALogger (org.apache.syncope.core.persistence.jpa.entity.JPALogger)1 Transactional (org.springframework.transaction.annotation.Transactional)1