Search in sources :

Example 26 with FileAppender

use of org.apache.log4j.FileAppender in project cpsolver by UniTime.

the class Test method setupLogging.

/**
     * Setup log4j logging
     * 
     * @param logFile
     *            log file
     * @param debug
     *            true if debug messages should be logged (use -Ddebug=true to
     *            enable debug message)
     */
public static void setupLogging(File logFile, boolean debug) {
    Logger root = Logger.getRootLogger();
    ConsoleAppender console = new ConsoleAppender(new PatternLayout("[%t] %m%n"));
    console.setThreshold(Level.INFO);
    root.addAppender(console);
    if (logFile != null) {
        try {
            FileAppender file = new FileAppender(new PatternLayout("%d{dd-MMM-yy HH:mm:ss.SSS} [%t] %-5p %c{2}> %m%n"), logFile.getPath(), false);
            file.setThreshold(Level.DEBUG);
            root.addAppender(file);
        } catch (IOException e) {
            sLog.fatal("Unable to configure logging, reason: " + e.getMessage(), e);
        }
    }
    if (!debug)
        root.setLevel(Level.INFO);
}
Also used : ConsoleAppender(org.apache.log4j.ConsoleAppender) FileAppender(org.apache.log4j.FileAppender) PatternLayout(org.apache.log4j.PatternLayout) IOException(java.io.IOException) Logger(org.apache.log4j.Logger)

Example 27 with FileAppender

use of org.apache.log4j.FileAppender 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 28 with FileAppender

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

the class TaskLogger method createFileAppender.

public File createFileAppender(File pathToFolder) throws IOException {
    if (taskLogAppender.getAppender(FILE_APPENDER_NAME) != null) {
        throw new IllegalStateException("Only one file appender can be created");
    }
    File logFile = new File(pathToFolder, new TaskLoggerRelativePathGenerator(taskId).getRelativePath());
    logFile.getParentFile().mkdirs();
    FileUtils.touch(logFile);
    logFile.setWritable(true, false);
    FileAppender fap = new FileAppender(Log4JTaskLogs.getTaskLogLayout(), logFile.getAbsolutePath(), false);
    fap.setName(FILE_APPENDER_NAME);
    taskLogAppender.addAppender(fap);
    return logFile;
}
Also used : FileAppender(org.apache.log4j.FileAppender) File(java.io.File) TaskLoggerRelativePathGenerator(org.ow2.proactive.scheduler.common.util.TaskLoggerRelativePathGenerator)

Example 29 with FileAppender

use of org.apache.log4j.FileAppender in project uavstack by uavorg.

the class Log4jHookProxy method figureoutLogConfiguration.

@SuppressWarnings("unchecked")
private void figureoutLogConfiguration(Logger Logger, LinkedList<LogProfileInfo> list, String appid) {
    Enumeration<Appender> appenders = Logger.getAllAppenders();
    while (appenders != null && appenders.hasMoreElements()) {
        LogProfileInfo logProfileInfo = new LogProfileInfo();
        if (appid != null) {
            logProfileInfo.setAppId(appid);
        }
        logProfileInfo.setLogType(LogProfileInfo.LogType.Log4j);
        Map<String, String> attributes = new HashMap<String, String>();
        attributes.put(LogProfileInfo.ENGINE, "log4j");
        Appender appender = appenders.nextElement();
        if (!(appender instanceof FileAppender)) {
            continue;
        }
        FileAppender fileAppender = (FileAppender) appender;
        Layout layout = fileAppender.getLayout();
        if (null != layout) {
            if (layout instanceof PatternLayout) {
                PatternLayout patternLayout = (PatternLayout) fileAppender.getLayout();
                attributes.put(LogProfileInfo.PATTERN, patternLayout.getConversionPattern());
            } else if (layout instanceof HTMLLayout) {
                attributes.put(LogProfileInfo.PATTERN, "HTMLLayout");
            } else if (layout instanceof SimpleLayout) {
                attributes.put(LogProfileInfo.PATTERN, "SimpleLayout");
            } else if (layout instanceof TTCCLayout) {
                attributes.put(LogProfileInfo.PATTERN, "TTCCLayout");
            }
        }
        boolean useBuffIO = fileAppender.getBufferedIO();
        if (useBuffIO == true) {
            attributes.put(LogProfileInfo.BUFFER_SIZE, String.valueOf(fileAppender.getBufferSize()));
            attributes.put(LogProfileInfo.BUFFRT_IO, String.valueOf(useBuffIO));
        }
        logProfileInfo.setFilePath(fileAppender.getFile());
        logProfileInfo.setAttributes(attributes);
        list.add(logProfileInfo);
    }
}
Also used : Appender(org.apache.log4j.Appender) FileAppender(org.apache.log4j.FileAppender) FileAppender(org.apache.log4j.FileAppender) HashMap(java.util.HashMap) HTMLLayout(org.apache.log4j.HTMLLayout) PatternLayout(org.apache.log4j.PatternLayout) LogProfileInfo(com.creditease.uav.profiling.handlers.log.LogProfileInfo) SimpleLayout(org.apache.log4j.SimpleLayout) TTCCLayout(org.apache.log4j.TTCCLayout) HTMLLayout(org.apache.log4j.HTMLLayout) SimpleLayout(org.apache.log4j.SimpleLayout) Layout(org.apache.log4j.Layout) PatternLayout(org.apache.log4j.PatternLayout) TTCCLayout(org.apache.log4j.TTCCLayout)

Example 30 with FileAppender

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

the class GridTestLog4jLogger method setNodeId.

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

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