Search in sources :

Example 6 with LoggerTO

use of org.apache.syncope.common.lib.log.LoggerTO 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 7 with LoggerTO

use of org.apache.syncope.common.lib.log.LoggerTO 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 8 with LoggerTO

use of org.apache.syncope.common.lib.log.LoggerTO in project syncope by apache.

the class ReportITCase method auditReport.

@Test
public void auditReport() throws IOException {
    AuditLoggerName auditLoggerName = new AuditLoggerName(AuditElements.EventCategoryType.LOGIC, "UserLogic", null, "selfRead", AuditElements.Result.SUCCESS);
    try {
        LoggerTO loggerTO = new LoggerTO();
        loggerTO.setKey(auditLoggerName.toLoggerName());
        loggerTO.setLevel(LoggerLevel.DEBUG);
        loggerService.update(LoggerType.AUDIT, loggerTO);
        ImplementationTO auditReportlet = new ImplementationTO();
        auditReportlet.setKey("UserReportletConf" + getUUIDString());
        auditReportlet.setEngine(ImplementationEngine.JAVA);
        auditReportlet.setType(ImplementationType.REPORTLET);
        auditReportlet.setBody(POJOHelper.serialize(new AuditReportletConf("auditReportlet" + getUUIDString())));
        Response response = implementationService.create(auditReportlet);
        auditReportlet.setKey(response.getHeaderString(RESTHeaders.RESOURCE_KEY));
        ReportTO report = new ReportTO();
        report.setName("auditReport" + getUUIDString());
        report.setActive(true);
        report.getReportlets().add(auditReportlet.getKey());
        report.setTemplate("sample");
        report = createReport(report);
        String execKey = execReport(report.getKey());
        checkExport(execKey, ReportExecExportFormat.XML);
        report = reportService.read(report.getKey());
        assertNotNull(report.getLastExec());
    } finally {
        loggerService.delete(LoggerType.AUDIT, auditLoggerName.toLoggerName());
    }
}
Also used : LoggerTO(org.apache.syncope.common.lib.log.LoggerTO) ImplementationTO(org.apache.syncope.common.lib.to.ImplementationTO) Response(javax.ws.rs.core.Response) AuditReportletConf(org.apache.syncope.common.lib.report.AuditReportletConf) ReportTO(org.apache.syncope.common.lib.to.ReportTO) AuditLoggerName(org.apache.syncope.common.lib.types.AuditLoggerName) Test(org.junit.jupiter.api.Test)

Example 9 with LoggerTO

use of org.apache.syncope.common.lib.log.LoggerTO in project syncope by apache.

the class LoggerITCase method enableDisableAudit.

@Test
public void enableDisableAudit() {
    AuditLoggerName auditLoggerName = new AuditLoggerName(EventCategoryType.LOGIC, ReportLogic.class.getSimpleName(), null, "deleteExecution", AuditElements.Result.FAILURE);
    List<AuditLoggerName> audits = LoggerWrapper.wrap(loggerService.list(LoggerType.AUDIT));
    assertNotNull(audits);
    assertFalse(audits.contains(auditLoggerName));
    LoggerTO loggerTO = new LoggerTO();
    loggerTO.setKey(auditLoggerName.toLoggerName());
    loggerTO.setLevel(LoggerLevel.DEBUG);
    loggerService.update(LoggerType.AUDIT, loggerTO);
    audits = LoggerWrapper.wrap(loggerService.list(LoggerType.AUDIT));
    assertNotNull(audits);
    assertTrue(audits.contains(auditLoggerName));
    loggerService.delete(LoggerType.AUDIT, auditLoggerName.toLoggerName());
    audits = LoggerWrapper.wrap(loggerService.list(LoggerType.AUDIT));
    assertNotNull(audits);
    assertFalse(audits.contains(auditLoggerName));
}
Also used : LoggerTO(org.apache.syncope.common.lib.log.LoggerTO) ReportLogic(org.apache.syncope.core.logic.ReportLogic) AuditLoggerName(org.apache.syncope.common.lib.types.AuditLoggerName) Test(org.junit.jupiter.api.Test)

Example 10 with LoggerTO

use of org.apache.syncope.common.lib.log.LoggerTO in project syncope by apache.

the class LoggerITCase method customAuditAppender.

@Test
public void customAuditAppender() throws IOException, InterruptedException {
    try (InputStream propStream = getClass().getResourceAsStream("/core-test.properties")) {
        Properties props = new Properties();
        props.load(propStream);
        String auditFilePath = props.getProperty("test.log.dir") + File.separator + "audit_for_Master_file.log";
        String auditNoRewriteFilePath = props.getProperty("test.log.dir") + File.separator + "audit_for_Master_norewrite_file.log";
        // 1. Enable audit for resource update -> catched by FileRewriteAuditAppender
        AuditLoggerName auditLoggerResUpd = new AuditLoggerName(EventCategoryType.LOGIC, ResourceLogic.class.getSimpleName(), null, "update", AuditElements.Result.SUCCESS);
        LoggerTO loggerTOUpd = new LoggerTO();
        loggerTOUpd.setKey(auditLoggerResUpd.toLoggerName());
        loggerTOUpd.setLevel(LoggerLevel.DEBUG);
        loggerService.update(LoggerType.AUDIT, loggerTOUpd);
        // 2. Enable audit for connector update -> NOT catched by FileRewriteAuditAppender
        AuditLoggerName auditLoggerConnUpd = new AuditLoggerName(EventCategoryType.LOGIC, ConnectorLogic.class.getSimpleName(), null, "update", AuditElements.Result.SUCCESS);
        LoggerTO loggerTOConnUpd = new LoggerTO();
        loggerTOConnUpd.setKey(auditLoggerConnUpd.toLoggerName());
        loggerTOConnUpd.setLevel(LoggerLevel.DEBUG);
        loggerService.update(LoggerType.AUDIT, loggerTOConnUpd);
        // 3. check that resource update is transformed and logged onto an audit file.
        ResourceTO resource = resourceService.read(RESOURCE_NAME_CSV);
        assertNotNull(resource);
        resource.setPropagationPriority(100);
        resourceService.update(resource);
        ConnInstanceTO connector = connectorService.readByResource(RESOURCE_NAME_CSV, null);
        assertNotNull(connector);
        connector.setPoolConf(new ConnPoolConfTO());
        connectorService.update(connector);
        File auditTempFile = new File(auditFilePath);
        // check audit_for_Master_file.log, it should contain only a static message
        String auditLog = FileUtils.readFileToString(auditTempFile, Charset.defaultCharset());
        assertTrue(StringUtils.contains(auditLog, "DEBUG Master.syncope.audit.[LOGIC]:[ResourceLogic]:[]:[update]:[SUCCESS]" + " - This is a static test message"));
        File auditNoRewriteTempFile = new File(auditNoRewriteFilePath);
        // check audit_for_Master_file.log, it should contain only a static message
        String auditLogNoRewrite = FileUtils.readFileToString(auditNoRewriteTempFile, Charset.defaultCharset());
        assertFalse(StringUtils.contains(auditLogNoRewrite, "DEBUG Master.syncope.audit.[LOGIC]:[ResourceLogic]:[]:[update]:[SUCCESS]" + " - This is a static test message"));
        // clean audit_for_Master_file.log
        FileUtils.writeStringToFile(auditTempFile, StringUtils.EMPTY, Charset.defaultCharset());
        loggerService.delete(LoggerType.AUDIT, "syncope.audit.[LOGIC]:[ResourceLogic]:[]:[update]:[SUCCESS]");
        resource = resourceService.read(RESOURCE_NAME_CSV);
        assertNotNull(resource);
        resource.setPropagationPriority(200);
        resourceService.update(resource);
        // check that nothing has been written to audit_for_Master_file.log
        assertTrue(StringUtils.isEmpty(FileUtils.readFileToString(auditTempFile, Charset.defaultCharset())));
    } catch (IOException e) {
        fail("Unable to read/write log files" + e.getMessage());
    }
}
Also used : LoggerTO(org.apache.syncope.common.lib.log.LoggerTO) ResourceLogic(org.apache.syncope.core.logic.ResourceLogic) ResourceTO(org.apache.syncope.common.lib.to.ResourceTO) InputStream(java.io.InputStream) ConnInstanceTO(org.apache.syncope.common.lib.to.ConnInstanceTO) ConnPoolConfTO(org.apache.syncope.common.lib.to.ConnPoolConfTO) IOException(java.io.IOException) Properties(java.util.Properties) ConnectorLogic(org.apache.syncope.core.logic.ConnectorLogic) File(java.io.File) AuditLoggerName(org.apache.syncope.common.lib.types.AuditLoggerName) Test(org.junit.jupiter.api.Test)

Aggregations

LoggerTO (org.apache.syncope.common.lib.log.LoggerTO)15 AuditLoggerName (org.apache.syncope.common.lib.types.AuditLoggerName)5 Test (org.junit.jupiter.api.Test)5 ArrayList (java.util.ArrayList)4 SyncopeClientException (org.apache.syncope.common.lib.SyncopeClientException)4 WebServiceException (javax.xml.ws.WebServiceException)3 LoggerContext (org.apache.logging.log4j.core.LoggerContext)2 NotFoundException (org.apache.syncope.core.persistence.api.dao.NotFoundException)2 Logger (org.apache.syncope.core.persistence.api.entity.Logger)2 File (java.io.File)1 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 InvocationTargetException (java.lang.reflect.InvocationTargetException)1 LinkedHashMap (java.util.LinkedHashMap)1 Properties (java.util.Properties)1 Response (javax.ws.rs.core.Response)1 LoggerConfig (org.apache.logging.log4j.core.config.LoggerConfig)1 AuditReportletConf (org.apache.syncope.common.lib.report.AuditReportletConf)1 ConnInstanceTO (org.apache.syncope.common.lib.to.ConnInstanceTO)1 ConnPoolConfTO (org.apache.syncope.common.lib.to.ConnPoolConfTO)1