Search in sources :

Example 56 with Configuration

use of org.apache.logging.log4j.core.config.Configuration in project cas by apereo.

the class LoggingConfigController method getConfiguration.

/**
     * Gets configuration as JSON.
     * Depends on the log4j core API.
     *
     * @param request  the request
     * @param response the response
     * @return the configuration
     * @throws Exception the exception
     */
@GetMapping(value = "/getConfiguration")
@ResponseBody
public Map<String, Object> getConfiguration(final HttpServletRequest request, final HttpServletResponse response) throws Exception {
    ensureEndpointAccessIsAuthorized(request, response);
    Assert.notNull(this.loggerContext);
    final Collection<Map<String, Object>> configuredLoggers = new HashSet<>();
    getLoggerConfigurations().forEach(config -> {
        final Map<String, Object> loggerMap = new HashMap<>();
        loggerMap.put("name", StringUtils.defaultIfBlank(config.getName(), LOGGER_NAME_ROOT));
        loggerMap.put("state", config.getState());
        if (config.getProperties() != null) {
            loggerMap.put("properties", config.getProperties());
        }
        loggerMap.put("additive", config.isAdditive());
        loggerMap.put("level", config.getLevel().name());
        final Collection<String> appenders = new HashSet<>();
        config.getAppenders().keySet().stream().map(key -> config.getAppenders().get(key)).forEach(appender -> {
            final ToStringBuilder builder = new ToStringBuilder(this, ToStringStyle.JSON_STYLE);
            builder.append("name", appender.getName());
            builder.append("state", appender.getState());
            builder.append("layoutFormat", appender.getLayout().getContentFormat());
            builder.append("layoutContentType", appender.getLayout().getContentType());
            if (appender instanceof FileAppender) {
                builder.append(FILE_PARAM, ((FileAppender) appender).getFileName());
                builder.append(FILE_PATTERN_PARAM, "(none)");
            }
            if (appender instanceof RandomAccessFileAppender) {
                builder.append(FILE_PARAM, ((RandomAccessFileAppender) appender).getFileName());
                builder.append(FILE_PATTERN_PARAM, "(none)");
            }
            if (appender instanceof RollingFileAppender) {
                builder.append(FILE_PARAM, ((RollingFileAppender) appender).getFileName());
                builder.append(FILE_PATTERN_PARAM, ((RollingFileAppender) appender).getFilePattern());
            }
            if (appender instanceof MemoryMappedFileAppender) {
                builder.append(FILE_PARAM, ((MemoryMappedFileAppender) appender).getFileName());
                builder.append(FILE_PATTERN_PARAM, "(none)");
            }
            if (appender instanceof RollingRandomAccessFileAppender) {
                builder.append(FILE_PARAM, ((RollingRandomAccessFileAppender) appender).getFileName());
                builder.append(FILE_PATTERN_PARAM, ((RollingRandomAccessFileAppender) appender).getFilePattern());
            }
            appenders.add(builder.build());
        });
        loggerMap.put("appenders", appenders);
        configuredLoggers.add(loggerMap);
    });
    final Map<String, Object> responseMap = new HashMap<>();
    responseMap.put("loggers", configuredLoggers);
    return responseMap;
}
Also used : LoggerConfig(org.apache.logging.log4j.core.config.LoggerConfig) CasConfigurationProperties(org.apereo.cas.configuration.CasConfigurationProperties) RollingRandomAccessFileAppender(org.apache.logging.log4j.core.appender.RollingRandomAccessFileAppender) RequestParam(org.springframework.web.bind.annotation.RequestParam) LoggerContext(org.apache.logging.log4j.core.LoggerContext) LoggerFactory(org.slf4j.LoggerFactory) ToStringStyle(org.apache.commons.lang3.builder.ToStringStyle) Level(org.apache.logging.log4j.Level) Autowired(org.springframework.beans.factory.annotation.Autowired) HashMap(java.util.HashMap) AuditActionContext(org.apereo.inspektr.audit.AuditActionContext) StringUtils(org.apache.commons.lang3.StringUtils) Configuration(org.apache.logging.log4j.core.config.Configuration) HashSet(java.util.HashSet) HttpServletRequest(javax.servlet.http.HttpServletRequest) Pair(org.apache.commons.lang3.tuple.Pair) Map(java.util.Map) GetMapping(org.springframework.web.bind.annotation.GetMapping) Log4jLoggerFactory(org.apache.logging.slf4j.Log4jLoggerFactory) Resource(org.springframework.core.io.Resource) PostMapping(org.springframework.web.bind.annotation.PostMapping) Logger(org.slf4j.Logger) ResourceLoader(org.springframework.core.io.ResourceLoader) Collection(java.util.Collection) HttpServletResponse(javax.servlet.http.HttpServletResponse) Set(java.util.Set) IOException(java.io.IOException) DelegatingAuditTrailManager(org.apereo.cas.audit.spi.DelegatingAuditTrailManager) ResponseBody(org.springframework.web.bind.annotation.ResponseBody) ControllerUtils(org.apereo.cas.web.report.util.ControllerUtils) MemoryMappedFileAppender(org.apache.logging.log4j.core.appender.MemoryMappedFileAppender) ModelAndView(org.springframework.web.servlet.ModelAndView) FileAppender(org.apache.logging.log4j.core.appender.FileAppender) Environment(org.springframework.core.env.Environment) ToStringBuilder(org.apache.commons.lang3.builder.ToStringBuilder) PostConstruct(javax.annotation.PostConstruct) RandomAccessFileAppender(org.apache.logging.log4j.core.appender.RandomAccessFileAppender) ILoggerFactory(org.slf4j.ILoggerFactory) RollingFileAppender(org.apache.logging.log4j.core.appender.RollingFileAppender) Assert(org.springframework.util.Assert) RollingRandomAccessFileAppender(org.apache.logging.log4j.core.appender.RollingRandomAccessFileAppender) MemoryMappedFileAppender(org.apache.logging.log4j.core.appender.MemoryMappedFileAppender) FileAppender(org.apache.logging.log4j.core.appender.FileAppender) RandomAccessFileAppender(org.apache.logging.log4j.core.appender.RandomAccessFileAppender) RollingFileAppender(org.apache.logging.log4j.core.appender.RollingFileAppender) RollingRandomAccessFileAppender(org.apache.logging.log4j.core.appender.RollingRandomAccessFileAppender) RandomAccessFileAppender(org.apache.logging.log4j.core.appender.RandomAccessFileAppender) HashMap(java.util.HashMap) RollingFileAppender(org.apache.logging.log4j.core.appender.RollingFileAppender) MemoryMappedFileAppender(org.apache.logging.log4j.core.appender.MemoryMappedFileAppender) ToStringBuilder(org.apache.commons.lang3.builder.ToStringBuilder) HashMap(java.util.HashMap) Map(java.util.Map) RollingRandomAccessFileAppender(org.apache.logging.log4j.core.appender.RollingRandomAccessFileAppender) HashSet(java.util.HashSet) GetMapping(org.springframework.web.bind.annotation.GetMapping) ResponseBody(org.springframework.web.bind.annotation.ResponseBody)

Example 57 with Configuration

use of org.apache.logging.log4j.core.config.Configuration in project cryptomator by cryptomator.

the class DebugMode method enable.

private void enable() {
    LoggerContext context = (LoggerContext) LogManager.getContext(false);
    Configuration config = context.getConfiguration();
    LOGGER_UPGRADES.forEach(loggerUpgrade -> loggerUpgrade.execute(config));
    context.updateLoggers();
}
Also used : Configuration(org.apache.logging.log4j.core.config.Configuration) LoggerContext(org.apache.logging.log4j.core.LoggerContext)

Example 58 with Configuration

use of org.apache.logging.log4j.core.config.Configuration in project elasticsearch by elastic.

the class Loggers method findAppender.

public static Appender findAppender(final Logger logger, final Class<? extends Appender> clazz) {
    final LoggerContext ctx = (LoggerContext) LogManager.getContext(false);
    final Configuration config = ctx.getConfiguration();
    final LoggerConfig loggerConfig = config.getLoggerConfig(logger.getName());
    for (final Map.Entry<String, Appender> entry : loggerConfig.getAppenders().entrySet()) {
        if (entry.getValue().getClass().equals(clazz)) {
            return entry.getValue();
        }
    }
    return null;
}
Also used : Appender(org.apache.logging.log4j.core.Appender) Configuration(org.apache.logging.log4j.core.config.Configuration) Configuration.getConfiguration(javax.security.auth.login.Configuration.getConfiguration) LoggerContext(org.apache.logging.log4j.core.LoggerContext) Map(java.util.Map) LoggerConfig(org.apache.logging.log4j.core.config.LoggerConfig)

Example 59 with Configuration

use of org.apache.logging.log4j.core.config.Configuration in project elasticsearch by elastic.

the class Loggers method removeAppender.

public static void removeAppender(final Logger logger, final Appender appender) {
    final LoggerContext ctx = (LoggerContext) LogManager.getContext(false);
    final Configuration config = ctx.getConfiguration();
    LoggerConfig loggerConfig = config.getLoggerConfig(logger.getName());
    if (!logger.getName().equals(loggerConfig.getName())) {
        loggerConfig = new LoggerConfig(logger.getName(), logger.getLevel(), true);
        config.addLogger(logger.getName(), loggerConfig);
    }
    loggerConfig.removeAppender(appender.getName());
    ctx.updateLoggers();
}
Also used : Configuration(org.apache.logging.log4j.core.config.Configuration) Configuration.getConfiguration(javax.security.auth.login.Configuration.getConfiguration) LoggerContext(org.apache.logging.log4j.core.LoggerContext) LoggerConfig(org.apache.logging.log4j.core.config.LoggerConfig)

Example 60 with Configuration

use of org.apache.logging.log4j.core.config.Configuration in project elasticsearch by elastic.

the class Loggers method setLevel.

public static void setLevel(Logger logger, Level level) {
    if (!LogManager.ROOT_LOGGER_NAME.equals(logger.getName())) {
        Configurator.setLevel(logger.getName(), level);
    } else {
        final LoggerContext ctx = LoggerContext.getContext(false);
        final Configuration config = ctx.getConfiguration();
        final LoggerConfig loggerConfig = config.getLoggerConfig(logger.getName());
        loggerConfig.setLevel(level);
        ctx.updateLoggers();
    }
    // we have to descend the hierarchy
    final LoggerContext ctx = LoggerContext.getContext(false);
    for (final LoggerConfig loggerConfig : ctx.getConfiguration().getLoggers().values()) {
        if (LogManager.ROOT_LOGGER_NAME.equals(logger.getName()) || loggerConfig.getName().startsWith(logger.getName() + ".")) {
            Configurator.setLevel(loggerConfig.getName(), level);
        }
    }
}
Also used : Configuration(org.apache.logging.log4j.core.config.Configuration) Configuration.getConfiguration(javax.security.auth.login.Configuration.getConfiguration) LoggerContext(org.apache.logging.log4j.core.LoggerContext) LoggerConfig(org.apache.logging.log4j.core.config.LoggerConfig)

Aggregations

Configuration (org.apache.logging.log4j.core.config.Configuration)105 LoggerContext (org.apache.logging.log4j.core.LoggerContext)54 Test (org.junit.Test)43 LoggerConfig (org.apache.logging.log4j.core.config.LoggerConfig)39 Appender (org.apache.logging.log4j.core.Appender)21 DefaultConfiguration (org.apache.logging.log4j.core.config.DefaultConfiguration)15 File (java.io.File)12 FileAppender (org.apache.logging.log4j.core.appender.FileAppender)9 RollingFileAppender (org.apache.logging.log4j.core.appender.RollingFileAppender)9 Path (java.nio.file.Path)7 Level (org.apache.logging.log4j.Level)7 NullConfiguration (org.apache.logging.log4j.core.config.NullConfiguration)7 ArrayList (java.util.ArrayList)6 Logger (org.apache.logging.log4j.Logger)6 Filter (org.apache.logging.log4j.core.Filter)6 AbstractLogger (org.apache.logging.log4j.spi.AbstractLogger)6 PatternLayout (org.apache.logging.log4j.core.layout.PatternLayout)5 ListAppender (org.apache.logging.log4j.test.appender.ListAppender)5 ConsoleAppender (org.apache.logging.log4j.core.appender.ConsoleAppender)4 AppenderRef (org.apache.logging.log4j.core.config.AppenderRef)4