Search in sources :

Example 41 with FileAppender

use of org.apache.log4j.FileAppender in project apex-core by apache.

the class LoggerUtil method getLogFileInformation.

public static LogFileInformation getLogFileInformation(Logger logger) {
    if (logger == null) {
        logger = LogManager.getRootLogger();
    }
    FileAppender fileAppender = getFileAppender(logger);
    if (fileAppender != null) {
        File logFile = new File(fileAppender.getFile());
        LogFileInformation logFileInfo = new LogFileInformation(fileAppender.getFile(), logFile.length());
        return logFileInfo;
    }
    return null;
}
Also used : FileAppender(org.apache.log4j.FileAppender) LogFileInformation(org.apache.apex.log.LogFileInformation) File(java.io.File)

Example 42 with FileAppender

use of org.apache.log4j.FileAppender in project alluxio by Alluxio.

the class BaseIntegrationTest method logHandler.

private TestWatcher logHandler() {
    return new TestWatcher() {

        private String mLogPath;

        private Appender mAppender;

        @Override
        protected void starting(Description description) {
            try {
                mLogPath = logPath(description);
                // In case the file already exists, truncate it.
                new FileWriter(mLogPath).close();
                mAppender = new FileAppender(new PatternLayout("%d{ISO8601} [%t] %-5p %c{2} (%F:%M) - %m%n"), mLogPath);
                LogManager.getRootLogger().addAppender(mAppender);
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }

        @Override
        protected void succeeded(Description description) {
            try {
                Files.delete(Paths.get(mLogPath));
            } catch (Throwable t) {
                LOG.error("Failed to delete success log file {}", mLogPath);
            }
        }

        @Override
        protected void skipped(AssumptionViolatedException e, Description description) {
            succeeded(description);
        }

        @Override
        protected void finished(Description description) {
            LogManager.getRootLogger().removeAppender(mAppender);
        }

        private String logPath(Description description) {
            String basename = String.format("tests-%s-%s.log", description.getClassName(), description.getMethodName());
            return PathUtils.concatPath(Constants.TEST_LOG_DIR, basename);
        }
    };
}
Also used : Appender(org.apache.log4j.Appender) FileAppender(org.apache.log4j.FileAppender) FileAppender(org.apache.log4j.FileAppender) Description(org.junit.runner.Description) TestWatcher(org.junit.rules.TestWatcher) AssumptionViolatedException(org.junit.AssumptionViolatedException) FileWriter(java.io.FileWriter) PatternLayout(org.apache.log4j.PatternLayout) IOException(java.io.IOException)

Example 43 with FileAppender

use of org.apache.log4j.FileAppender in project gerrit by GerritCodeReview.

the class SystemLog method createAppender.

public static Appender createAppender(Path logdir, String name, Layout layout, boolean rotate) {
    final FileAppender dst = rotate ? new DailyRollingFileAppender() : new FileAppender();
    dst.setName(name);
    dst.setLayout(layout);
    dst.setEncoding(UTF_8.name());
    dst.setFile(resolve(logdir).resolve(name).toString());
    dst.setImmediateFlush(true);
    dst.setAppend(true);
    dst.setErrorHandler(new DieErrorHandler());
    dst.activateOptions();
    dst.setErrorHandler(new OnlyOnceErrorHandler());
    return dst;
}
Also used : DailyRollingFileAppender(org.apache.log4j.DailyRollingFileAppender) FileAppender(org.apache.log4j.FileAppender) DailyRollingFileAppender(org.apache.log4j.DailyRollingFileAppender) OnlyOnceErrorHandler(org.apache.log4j.helpers.OnlyOnceErrorHandler)

Example 44 with FileAppender

use of org.apache.log4j.FileAppender in project databus by linkedin.

the class TestUtil method setupLogging.

public static void setupLogging(boolean logToConsole, String fileLogPath, Level logLevel) {
    PatternLayout defaultLayout = new PatternLayout("%d{ISO8601} [%t] (%p) {%c{1}} %m%n");
    Logger.getRootLogger().removeAllAppenders();
    if (logToConsole) {
        ConsoleAppender defaultAppender = new ConsoleAppender(defaultLayout);
        Logger.getRootLogger().addAppender(defaultAppender);
    }
    if (null != fileLogPath && 0 < fileLogPath.length()) {
        File logFile = new File(fileLogPath);
        if (null != logFile.getParentFile() && !logFile.getParentFile().exists()) {
            if (!logFile.getParentFile().mkdirs()) {
                Logger.getRootLogger().error("unable to create parent directory for log file: " + logFile);
            }
        }
        FileAppender fileAppender = null;
        try {
            fileAppender = new FileAppender(defaultLayout, fileLogPath);
        } catch (IOException io) {
            Logger.getRootLogger().error(io);
        }
        if (null != fileAppender)
            Logger.getRootLogger().addAppender(fileAppender);
    }
    Logger.getRootLogger().setLevel(logLevel);
}
Also used : ConsoleAppender(org.apache.log4j.ConsoleAppender) FileAppender(org.apache.log4j.FileAppender) PatternLayout(org.apache.log4j.PatternLayout) IOException(java.io.IOException) File(java.io.File)

Example 45 with FileAppender

use of org.apache.log4j.FileAppender in project ignite by apache.

the class GridTestLog4jLogger method setApplicationAndNode.

/**
 * {@inheritDoc}
 */
@Override
public void setApplicationAndNode(@Nullable String application, UUID nodeId) {
    A.notNull(nodeId, "nodeId");
    this.nodeId = nodeId;
    for (FileAppender a : fileAppenders) {
        if (a instanceof LoggerNodeIdAndApplicationAware) {
            ((LoggerNodeIdAndApplicationAware) a).setApplicationAndNode(application, nodeId);
            a.activateOptions();
        }
    }
}
Also used : FileAppender(org.apache.log4j.FileAppender) LoggerNodeIdAndApplicationAware(org.apache.ignite.logger.LoggerNodeIdAndApplicationAware)

Aggregations

FileAppender (org.apache.log4j.FileAppender)47 Appender (org.apache.log4j.Appender)18 File (java.io.File)15 IOException (java.io.IOException)14 PatternLayout (org.apache.log4j.PatternLayout)14 Logger (org.apache.log4j.Logger)12 ConsoleAppender (org.apache.log4j.ConsoleAppender)8 Layout (org.apache.log4j.Layout)5 Enumeration (java.util.Enumeration)4 Properties (java.util.Properties)4 FileNotFoundException (java.io.FileNotFoundException)3 DailyRollingFileAppender (org.apache.log4j.DailyRollingFileAppender)3 RollingFileAppender (org.apache.log4j.RollingFileAppender)3 SimpleLayout (org.apache.log4j.SimpleLayout)3 InputStream (java.io.InputStream)2 HTMLLayout (org.apache.log4j.HTMLLayout)2 LoggingEvent (org.apache.log4j.spi.LoggingEvent)2 LoggerChannel (com.biglybt.pif.logging.LoggerChannel)1 LoggerChannelListener (com.biglybt.pif.logging.LoggerChannelListener)1 LogProfileInfo (com.creditease.uav.profiling.handlers.log.LogProfileInfo)1