Search in sources :

Example 66 with Appender

use of org.apache.log4j.Appender in project xtext-core by eclipse.

the class LoggingTester method captureLogging.

public static LoggingTester.LogCapture captureLogging(final Level level, final Class<?> source, final Runnable action) {
    final Logger logger = Logger.getLogger(source);
    final LoggingTester.QueueAppender appender = new LoggingTester.QueueAppender();
    final Level oldLevel = logger.getLevel();
    final ArrayList<Appender> allAppenders = LoggingTester.appenderHierarchy(logger);
    final LoggingTester.SourceFilter filter = new LoggingTester.SourceFilter(logger);
    try {
        final Consumer<Appender> _function = (Appender it) -> {
            it.addFilter(filter);
        };
        allAppenders.forEach(_function);
        logger.addAppender(appender);
        logger.setLevel(level);
        action.run();
        final List<LoggingTester.LogEntry> events = IterableExtensions.<LoggingTester.LogEntry>sortWith(IterableExtensions.<LoggingTester.LogEntry>toList(appender.events), LoggingTester.TEMPORAL_ORDER);
        return new LoggingTester.LogCapture(events);
    } finally {
        logger.removeAppender(appender);
        final Consumer<Appender> _function_1 = (Appender it) -> {
            LoggingTester.removeFilter(it, filter);
        };
        allAppenders.forEach(_function_1);
        logger.setLevel(oldLevel);
    }
}
Also used : Appender(org.apache.log4j.Appender) Logger(org.apache.log4j.Logger) Level(org.apache.log4j.Level)

Example 67 with Appender

use of org.apache.log4j.Appender in project knime-core by knime.

the class NodeLogger method addWorkflowDirAppender.

/**
 * Adds a new workflow directory logger for the given workflow directory if it doesn't exists yet.
 * @param workflowDir the directory of the workflow that should be logged to
 */
private void addWorkflowDirAppender(final File workflowDir) {
    if (workflowDir == null) {
        // if the workflowDir is null we do not need to append an extra log appender
        return;
    }
    // in this method we have to use the logger directly to prevent a deadlock!!!
    final Logger logger = m_logger;
    final String workflowDirPath = workflowDir.getPath();
    if (workflowDirPath == null) {
        return;
    }
    Appender wfAppender = WF_APPENDER.get(workflowDirPath);
    if (wfAppender != null) {
        logger.addAppender(wfAppender);
    } else {
        // we do the getAppender twice to prevent the synchronize block on subsequent calls!!!
        synchronized (WF_APPENDER) {
            // we need a synchronize block otherwise we might create a second appender that opens a file handle
            // which never get closed and thus the copying of a full log file to the zip file fails
            wfAppender = WF_APPENDER.get(workflowDirPath);
            if (wfAppender == null) {
                // use the KNIME specific LogfielAppender that moves larger log files into a separate zip file
                // and that implements equals and hash code to ensure that two LogfileAppender
                // with the same name are considered equal to prevent duplicate appender registration
                final FileAppender fileAppender = new LogfileAppender(workflowDir);
                fileAppender.setLayout(WF_DIR_LOG_FILE_LAYOUT);
                fileAppender.setName(workflowDirPath);
                final Filter mainFilter = LOG_FILE_APPENDER.getFilter();
                fileAppender.addFilter(new Filter() {

                    @Override
                    public int decide(final LoggingEvent event) {
                        final Object msg = event.getMessage();
                        if (msg instanceof KNIMELogMessage) {
                            final KNIMELogMessage kmsg = (KNIMELogMessage) msg;
                            // can be null
                            final File msgDir = kmsg.getWorkflowDir();
                            if ((LOG_GLOBAL_IN_WF_DIR && msgDir == null) || LOG_IN_WF_DIR && workflowDir.equals(msgDir)) {
                                // return only neutral to let the log level based filters decide if we log this event
                                if (mainFilter != null) {
                                    return mainFilter.decide(event);
                                }
                                return Filter.NEUTRAL;
                            }
                        }
                        return Filter.DENY;
                    }
                });
                // we have to call this function to activate the writer!!!
                fileAppender.activateOptions();
                logger.addAppender(fileAppender);
                WF_APPENDER.put(workflowDirPath, fileAppender);
                if (m_listener == null) {
                    m_listener = new MyWorkflowListener();
                    WorkflowManager.ROOT.addListener(m_listener);
                }
            }
        }
    }
}
Also used : LogfileAppender(org.knime.core.util.LogfileAppender) NullAppender(org.apache.log4j.varia.NullAppender) Appender(org.apache.log4j.Appender) FileAppender(org.apache.log4j.FileAppender) WriterAppender(org.apache.log4j.WriterAppender) FileAppender(org.apache.log4j.FileAppender) LoggingEvent(org.apache.log4j.spi.LoggingEvent) LevelRangeFilter(org.apache.log4j.varia.LevelRangeFilter) Filter(org.apache.log4j.spi.Filter) LogfileAppender(org.knime.core.util.LogfileAppender) Logger(org.apache.log4j.Logger) ReferencedFile(org.knime.core.internal.ReferencedFile) File(java.io.File)

Example 68 with Appender

use of org.apache.log4j.Appender in project knime-core by knime.

the class NodeLogger method setAppenderLevelRange.

/**
 * Sets a level range filter on the given appender.
 *
 * @param appenderName the name of the appender
 * @param min the minimum logging level
 * @param max the maximum logging level
 * @throws NoSuchElementException if the given appender does not exist
 * @since 2.8
 */
public static void setAppenderLevelRange(final String appenderName, final LEVEL min, final LEVEL max) throws NoSuchElementException {
    Logger root = Logger.getRootLogger();
    Appender appender = root.getAppender(appenderName);
    if (appender == null) {
        throw new NoSuchElementException("Appender '" + appenderName + "' does not exist");
    }
    Filter filter = appender.getFilter();
    while ((filter != null) && !(filter instanceof LevelRangeFilter)) {
        filter = filter.getNext();
    }
    if (filter == null) {
        // add a new level range filter
        LevelRangeFilter levelFilter = new LevelRangeFilter();
        levelFilter.setLevelMin(transLEVEL(min));
        levelFilter.setLevelMax(transLEVEL(max));
        appender.addFilter(levelFilter);
    } else {
        // modify existing level range filter
        ((LevelRangeFilter) filter).setLevelMin(transLEVEL(min));
        ((LevelRangeFilter) filter).setLevelMax(transLEVEL(max));
    }
}
Also used : LogfileAppender(org.knime.core.util.LogfileAppender) NullAppender(org.apache.log4j.varia.NullAppender) Appender(org.apache.log4j.Appender) FileAppender(org.apache.log4j.FileAppender) WriterAppender(org.apache.log4j.WriterAppender) LevelRangeFilter(org.apache.log4j.varia.LevelRangeFilter) Filter(org.apache.log4j.spi.Filter) LevelRangeFilter(org.apache.log4j.varia.LevelRangeFilter) Logger(org.apache.log4j.Logger) NoSuchElementException(java.util.NoSuchElementException)

Example 69 with Appender

use of org.apache.log4j.Appender in project traccar by tananaev.

the class Log method setupLogger.

public static void setupLogger(Config config) throws IOException {
    Layout layout = new PatternLayout("%d{" + DATE_FORMAT + "} %5p: %m%n");
    Appender appender = new DailyRollingFileAppender(layout, config.getString("logger.file"), "'.'yyyyMMdd");
    LogManager.resetConfiguration();
    LogManager.getRootLogger().addAppender(new NullAppender());
    logger = Logger.getLogger(LOGGER_NAME);
    logger.addAppender(appender);
    logger.setLevel(Level.toLevel(config.getString("logger.level"), Level.ALL));
    // Workaround for "Bug 745866 - (EDG-45) Possible netty logging config problem"
    InternalLoggerFactory.setDefaultFactory(new InternalLoggerFactory() {

        @Override
        public InternalLogger newInstance(String string) {
            return new NettyInternalLogger();
        }
    });
    Log.logSystemInfo();
    Log.info("Version: " + getAppVersion());
}
Also used : NullAppender(org.apache.log4j.varia.NullAppender) DailyRollingFileAppender(org.apache.log4j.DailyRollingFileAppender) Appender(org.apache.log4j.Appender) Layout(org.apache.log4j.Layout) PatternLayout(org.apache.log4j.PatternLayout) NullAppender(org.apache.log4j.varia.NullAppender) PatternLayout(org.apache.log4j.PatternLayout) InternalLoggerFactory(org.jboss.netty.logging.InternalLoggerFactory) DailyRollingFileAppender(org.apache.log4j.DailyRollingFileAppender) InternalLogger(org.jboss.netty.logging.InternalLogger) AbstractInternalLogger(org.jboss.netty.logging.AbstractInternalLogger)

Example 70 with Appender

use of org.apache.log4j.Appender in project scheduling by ow2-proactive.

the class EnabledListenJobLogsSupport method listenJobLogs.

@Override
public synchronized void listenJobLogs(JobId jobId, AppenderProvider appenderProvider) throws UnknownJobException {
    jlogger.info(jobId, "listening logs");
    // create the appender to the remote listener
    Appender clientAppender = null;
    try {
        clientAppender = appenderProvider.getAppender();
    } catch (LogForwardingException e) {
        jlogger.error(jobId, "cannot create an appender", e);
        throw new InternalException("Cannot create an appender for job " + jobId, e);
    }
    boolean logIsAlreadyInitialized = jobsToBeLogged.contains(jobId);
    initJobLogging(jobId, clientAppender);
    JobResult result = dbManager.loadJobResult(jobId);
    if (result == null) {
        throw new UnknownJobException(jobId);
    }
    // for finished tasks, add logs events "manually"
    Collection<TaskResult> allRes = result.getAllResults().values();
    for (TaskResult tr : allRes) {
        this.flushTaskLogs(tr, clientAppender, jobId);
    }
    for (RunningTaskData taskData : liveJobs.getRunningTasks(jobId)) {
        jlogger.debug(jobId, "Handling log initialization for task " + taskData.getTask().getName());
        try {
            TaskLauncher taskLauncher = taskData.getLauncher();
            if (logIsAlreadyInitialized) {
                jlogger.debug(jobId, "Call getStoredLogs");
                taskLauncher.getStoredLogs(appenderProvider);
            } else {
                jlogger.debug(jobId, "Call activateLogs");
                taskLauncher.activateLogs(lfs.getAppenderProvider());
            }
        } catch (Exception e) {
            tlogger.error(taskData.getTask().getId(), "cannot create an appender provider", e);
        }
    }
    if (!result.getJobInfo().getStatus().isJobAlive()) {
        jlogger.info(jobId, "cleaning loggers for already finished job");
        cleanLoggers(jobId);
    }
}
Also used : Appender(org.apache.log4j.Appender) JobResult(org.ow2.proactive.scheduler.common.job.JobResult) UnknownJobException(org.ow2.proactive.scheduler.common.exception.UnknownJobException) TaskLauncher(org.ow2.proactive.scheduler.task.TaskLauncher) TaskResult(org.ow2.proactive.scheduler.common.task.TaskResult) LogForwardingException(org.ow2.proactive.scheduler.common.util.logforwarder.LogForwardingException) LogForwardingException(org.ow2.proactive.scheduler.common.util.logforwarder.LogForwardingException) UnknownJobException(org.ow2.proactive.scheduler.common.exception.UnknownJobException) InternalException(org.ow2.proactive.scheduler.common.exception.InternalException) InternalException(org.ow2.proactive.scheduler.common.exception.InternalException)

Aggregations

Appender (org.apache.log4j.Appender)73 FileAppender (org.apache.log4j.FileAppender)24 Logger (org.apache.log4j.Logger)20 File (java.io.File)12 ConsoleAppender (org.apache.log4j.ConsoleAppender)11 RollingFileAppender (org.apache.log4j.RollingFileAppender)11 WriterAppender (org.apache.log4j.WriterAppender)11 NullAppender (org.apache.log4j.varia.NullAppender)11 PatternLayout (org.apache.log4j.PatternLayout)9 Log4JLogger (org.apache.commons.logging.impl.Log4JLogger)8 Enumeration (java.util.Enumeration)7 Test (org.junit.Test)7 IOException (java.io.IOException)6 AsyncAppender (org.apache.log4j.AsyncAppender)6 Layout (org.apache.log4j.Layout)6 LogfileAppender (org.knime.core.util.LogfileAppender)6 DailyRollingFileAppender (org.apache.log4j.DailyRollingFileAppender)5 Filter (org.apache.log4j.spi.Filter)5 LevelRangeFilter (org.apache.log4j.varia.LevelRangeFilter)5 ActiveDbAppender (com.axway.ats.log.appenders.ActiveDbAppender)4