Search in sources :

Example 1 with RandomAccessFileAppender

use of org.apache.logging.log4j.core.appender.RandomAccessFileAppender in project hive by apache.

the class LlapWrappedAppender method stop.

@Override
public void stop() {
    if (!(this.isStopping() || this.isStopped())) {
        super.stop();
        if (appenderControl.get() != null) {
            appenderControl.get().stop();
            realAppender.get().stop();
        }
        if (LOGGER.isDebugEnabled()) {
            LOGGER.debug("Stop invoked for " + ((RandomAccessFileAppender) realAppender.get()).getFileName());
        }
        if (realAppender.get() == null) {
            LOGGER.info("RealAppender is null. Ignoring stop");
            return;
        }
        RandomAccessFileAppender raf = (RandomAccessFileAppender) realAppender.get();
        Path renamedPath = null;
        if (renameFileOnClose) {
            try {
                // Look for a file to which we can move the existing file. With external services,
                // it's possible for the service to be marked complete after each fragment.
                int counter = 0;
                while (true) {
                    renamedPath = getRenamedPath(raf.getFileName(), counter);
                    if (!Files.exists(renamedPath)) {
                        if (LOGGER.isTraceEnabled()) {
                            LOGGER.trace("Renaming file: " + raf.getFileName() + " to " + renamedPath);
                        }
                        Files.move(Paths.get(raf.getFileName()), renamedPath);
                        break;
                    }
                    counter++;
                }
            } catch (IOException e) {
                // Bail on an exception - out of the loop.
                LOGGER.warn("Failed to rename file: " + raf.getFileName() + " to " + renamedPath, e);
            }
        }
    }
}
Also used : Path(java.nio.file.Path) RandomAccessFileAppender(org.apache.logging.log4j.core.appender.RandomAccessFileAppender) IOException(java.io.IOException)

Example 2 with RandomAccessFileAppender

use of org.apache.logging.log4j.core.appender.RandomAccessFileAppender in project cas by apereo.

the class LoggingOutputSocketMessagingController method registerLogFileTailThreads.

private void registerLogFileTailThreads() throws IOException {
    final Collection<String> outputFileNames = new HashSet<>();
    final Collection<Appender> loggerAppenders = this.loggerContext.getConfiguration().getAppenders().values();
    loggerAppenders.forEach(appender -> {
        if (appender instanceof FileAppender) {
            outputFileNames.add(((FileAppender) appender).getFileName());
        } else if (appender instanceof RandomAccessFileAppender) {
            outputFileNames.add(((RandomAccessFileAppender) appender).getFileName());
        } else if (appender instanceof RollingFileAppender) {
            outputFileNames.add(((RollingFileAppender) appender).getFileName());
        } else if (appender instanceof MemoryMappedFileAppender) {
            outputFileNames.add(((MemoryMappedFileAppender) appender).getFileName());
        } else if (appender instanceof RollingRandomAccessFileAppender) {
            outputFileNames.add(((RollingRandomAccessFileAppender) appender).getFileName());
        }
    });
    outputFileNames.forEach(s -> {
        final Tailer t = new Tailer(new File(s), new LogTailerListener(), 100, false, true);
        final Thread thread = new Thread(t);
        thread.setPriority(Thread.MIN_PRIORITY);
        thread.setName(s);
        thread.start();
    });
}
Also used : RollingRandomAccessFileAppender(org.apache.logging.log4j.core.appender.RollingRandomAccessFileAppender) Appender(org.apache.logging.log4j.core.Appender) 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) 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) Tailer(org.apache.commons.io.input.Tailer) RollingRandomAccessFileAppender(org.apache.logging.log4j.core.appender.RollingRandomAccessFileAppender) RandomAccessFileAppender(org.apache.logging.log4j.core.appender.RandomAccessFileAppender) RollingFileAppender(org.apache.logging.log4j.core.appender.RollingFileAppender) MemoryMappedFileAppender(org.apache.logging.log4j.core.appender.MemoryMappedFileAppender) File(java.io.File) RollingRandomAccessFileAppender(org.apache.logging.log4j.core.appender.RollingRandomAccessFileAppender) HashSet(java.util.HashSet)

Example 3 with RandomAccessFileAppender

use of org.apache.logging.log4j.core.appender.RandomAccessFileAppender in project hive by apache.

the class LlapWrappedAppender method setupAppenderIfRequired.

private void setupAppenderIfRequired(LogEvent event) {
    if (appenderControl.get() == null) {
        if (node.getType().getElementName().equalsIgnoreCase("appender")) {
            for (final Node cnode : node.getChildren()) {
                final Node appNode = new Node(cnode);
                config.createConfiguration(appNode, event);
                if (appNode.getObject() instanceof Appender) {
                    final Appender app = appNode.getObject();
                    app.start();
                    if (!(app instanceof RandomAccessFileAppender)) {
                        String message = "Cannot handle appenders other than " + RandomAccessFileAppender.class.getName() + ". Found: " + app.getClass().getName();
                        LOGGER.error(message);
                        throw new IllegalStateException(message);
                    }
                    realAppender.set(app);
                    appenderControl.set(new AppenderControl(app, null, null));
                    if (LOGGER.isDebugEnabled()) {
                        RandomAccessFileAppender raf = (RandomAccessFileAppender) app;
                        LOGGER.debug("Setup new appender to write to file: " + raf.getFileName() + ", appenderName=" + raf.getName() + ", appenderManagerName=" + raf.getManager().getName());
                    }
                    break;
                }
            }
            if (appenderControl.get() == null) {
                // Fail if mis-configured.
                throw new RuntimeException(LlapWrappedAppender.class.getSimpleName() + "name=" + getName() + " unable to setup actual appender." + "Could not find child appender");
            }
        } else {
            // Fail if mis-configured.
            throw new RuntimeException(LlapWrappedAppender.class.getSimpleName() + "name=" + getName() + " unable to setup actual appender." + "Could not find child appender");
        }
    }
}
Also used : AbstractAppender(org.apache.logging.log4j.core.appender.AbstractAppender) Appender(org.apache.logging.log4j.core.Appender) RandomAccessFileAppender(org.apache.logging.log4j.core.appender.RandomAccessFileAppender) RandomAccessFileAppender(org.apache.logging.log4j.core.appender.RandomAccessFileAppender) AppenderControl(org.apache.logging.log4j.core.config.AppenderControl) PluginNode(org.apache.logging.log4j.core.config.plugins.PluginNode) Node(org.apache.logging.log4j.core.config.Node)

Example 4 with RandomAccessFileAppender

use of org.apache.logging.log4j.core.appender.RandomAccessFileAppender 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)

Aggregations

RandomAccessFileAppender (org.apache.logging.log4j.core.appender.RandomAccessFileAppender)4 IOException (java.io.IOException)2 HashSet (java.util.HashSet)2 Appender (org.apache.logging.log4j.core.Appender)2 FileAppender (org.apache.logging.log4j.core.appender.FileAppender)2 MemoryMappedFileAppender (org.apache.logging.log4j.core.appender.MemoryMappedFileAppender)2 RollingFileAppender (org.apache.logging.log4j.core.appender.RollingFileAppender)2 RollingRandomAccessFileAppender (org.apache.logging.log4j.core.appender.RollingRandomAccessFileAppender)2 File (java.io.File)1 Path (java.nio.file.Path)1 Collection (java.util.Collection)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 Set (java.util.Set)1 PostConstruct (javax.annotation.PostConstruct)1 HttpServletRequest (javax.servlet.http.HttpServletRequest)1 HttpServletResponse (javax.servlet.http.HttpServletResponse)1 Tailer (org.apache.commons.io.input.Tailer)1 StringUtils (org.apache.commons.lang3.StringUtils)1 ToStringBuilder (org.apache.commons.lang3.builder.ToStringBuilder)1