Search in sources :

Example 31 with Appender

use of org.apache.log4j.Appender in project Lucee by lucee.

the class LoggerAndSourceData method close.

public void close() {
    if (_log != null) {
        Appender a = _appender;
        _log = null;
        layout = null;
        if (a != null)
            a.close();
        _appender = null;
    }
}
Also used : Appender(org.apache.log4j.Appender)

Example 32 with Appender

use of org.apache.log4j.Appender in project Lucee by lucee.

the class Log4jUtil method getAppender.

public static final Appender getAppender(Config config, Layout layout, String name, ClassDefinition cd, Map<String, String> appenderArgs) {
    if (appenderArgs == null)
        appenderArgs = new HashMap<String, String>();
    // Appender
    Appender appender = null;
    if (cd != null && cd.hasClass()) {
        // Console Appender
        if (ConsoleAppender.class.getName().equalsIgnoreCase(cd.getClassName())) {
            // stream-type
            boolean doError = false;
            String st = Caster.toString(appenderArgs.get("streamtype"), null);
            if (!StringUtil.isEmpty(st, true)) {
                st = st.trim().toLowerCase();
                if (st.equals("err") || st.equals("error"))
                    doError = true;
            }
            appenderArgs.put("streamtype", doError ? "error" : "output");
            // get print writer
            PrintWriter pw;
            if (doError) {
                if (config.getErrWriter() == null)
                    pw = new PrintWriter(System.err);
                else
                    pw = config.getErrWriter();
            } else {
                if (config.getOutWriter() == null)
                    pw = new PrintWriter(System.out);
                else
                    pw = config.getOutWriter();
            }
            appender = new ConsoleAppender(pw, layout);
        } else if (DatasourceAppender.class.getName().equalsIgnoreCase(cd.getClassName())) {
            // datasource
            String dsn = Caster.toString(appenderArgs.get("datasource"), null);
            if (StringUtil.isEmpty(dsn, true))
                dsn = Caster.toString(appenderArgs.get("datasourceName"), null);
            if (!StringUtil.isEmpty(dsn, true))
                dsn = dsn.trim();
            appenderArgs.put("datasource", dsn);
            // username
            String user = Caster.toString(appenderArgs.get("username"), null);
            if (StringUtil.isEmpty(user, true))
                user = Caster.toString(appenderArgs.get("user"), null);
            if (!StringUtil.isEmpty(user, true))
                user = user.trim();
            else
                user = null;
            appenderArgs.put("username", user);
            // password
            String pass = Caster.toString(appenderArgs.get("password"), null);
            if (StringUtil.isEmpty(pass, true))
                pass = Caster.toString(appenderArgs.get("pass"), null);
            if (!StringUtil.isEmpty(pass, true))
                pass = pass.trim();
            else
                pass = null;
            appenderArgs.put("password", pass);
            try {
                appender = new DatasourceAppender(config, layout, dsn, user, pass);
            } catch (PageException e) {
                SystemOut.printDate(e);
                appender = null;
            }
        } else if (RollingResourceAppender.class.getName().equalsIgnoreCase(cd.getClassName())) {
            // path
            Resource res = null;
            String path = Caster.toString(appenderArgs.get("path"), null);
            if (!StringUtil.isEmpty(path, true)) {
                path = path.trim();
                path = ConfigWebUtil.translateOldPath(path);
                res = ConfigWebUtil.getFile(config, config.getConfigDir(), path, ResourceUtil.TYPE_FILE);
                if (res.isDirectory()) {
                    res = res.getRealResource(name + ".log");
                }
            }
            if (res == null) {
                res = ConfigWebUtil.getFile(config, config.getConfigDir(), "logs/" + name + ".log", ResourceUtil.TYPE_FILE);
            }
            // charset
            Charset charset = CharsetUtil.toCharset(Caster.toString(appenderArgs.get("charset"), null), null);
            if (charset == null) {
                charset = config.getResourceCharset();
                appenderArgs.put("charset", charset.name());
            }
            // maxfiles
            int maxfiles = Caster.toIntValue(appenderArgs.get("maxfiles"), 10);
            appenderArgs.put("maxfiles", Caster.toString(maxfiles));
            // maxfileSize
            long maxfilesize = Caster.toLongValue(appenderArgs.get("maxfilesize"), 1024 * 1024 * 10);
            appenderArgs.put("maxfilesize", Caster.toString(maxfilesize));
            // timeout
            // timeout in seconds
            int timeout = Caster.toIntValue(appenderArgs.get("timeout"), 60);
            appenderArgs.put("timeout", Caster.toString(timeout));
            try {
                appender = new RollingResourceAppender(layout, res, charset, true, maxfilesize, maxfiles, timeout, null);
            } catch (IOException e) {
                SystemOut.printDate(e);
            }
        } else // class defintion
        {
            Object obj = ClassUtil.loadInstance(cd.getClazz(null), null, null);
            if (obj instanceof Appender) {
                appender = (Appender) obj;
                AppenderSkeleton as = obj instanceof AppenderSkeleton ? (AppenderSkeleton) obj : null;
                Iterator<Entry<String, String>> it = appenderArgs.entrySet().iterator();
                Entry<String, String> e;
                String n;
                while (it.hasNext()) {
                    e = it.next();
                    n = e.getKey();
                    if (as != null) {
                        if ("threshold".equalsIgnoreCase(n)) {
                            Level level = Level.toLevel(e.getValue(), null);
                            if (level != null) {
                                as.setThreshold(level);
                                continue;
                            }
                        }
                    }
                    try {
                        Reflector.callSetter(obj, e.getKey(), e.getValue());
                    } catch (PageException e1) {
                        // TODO log
                        SystemOut.printDate(e1);
                    }
                }
            }
        }
    }
    if (appender instanceof AppenderSkeleton) {
        ((AppenderSkeleton) appender).activateOptions();
    } else if (appender == null) {
        PrintWriter pw;
        if (config.getOutWriter() == null)
            pw = new PrintWriter(System.out);
        else
            pw = config.getOutWriter();
        appender = new ConsoleAppender(pw, layout);
    }
    return appender;
}
Also used : TaskAppender(lucee.commons.io.log.log4j.appender.TaskAppender) Appender(org.apache.log4j.Appender) ConsoleAppender(lucee.commons.io.log.log4j.appender.ConsoleAppender) DatasourceAppender(lucee.commons.io.log.log4j.appender.DatasourceAppender) RollingResourceAppender(lucee.commons.io.log.log4j.appender.RollingResourceAppender) ConsoleAppender(lucee.commons.io.log.log4j.appender.ConsoleAppender) PageException(lucee.runtime.exp.PageException) AppenderSkeleton(org.apache.log4j.AppenderSkeleton) HashMap(java.util.HashMap) Resource(lucee.commons.io.res.Resource) Charset(java.nio.charset.Charset) IOException(java.io.IOException) DatasourceAppender(lucee.commons.io.log.log4j.appender.DatasourceAppender) RollingResourceAppender(lucee.commons.io.log.log4j.appender.RollingResourceAppender) Entry(java.util.Map.Entry) Level(org.apache.log4j.Level) PrintWriter(java.io.PrintWriter)

Example 33 with Appender

use of org.apache.log4j.Appender in project cxf by apache.

the class Log4jLogger method getHandlers.

public synchronized Handler[] getHandlers() {
    List<Handler> ret = new ArrayList<>();
    Enumeration<?> en = log.getAllAppenders();
    while (en.hasMoreElements()) {
        Appender ap = (Appender) en.nextElement();
        if (ap instanceof HandlerWrapper) {
            ret.add(((HandlerWrapper) ap).getHandler());
        }
    }
    return ret.toArray(new Handler[ret.size()]);
}
Also used : Appender(org.apache.log4j.Appender) ArrayList(java.util.ArrayList) Handler(java.util.logging.Handler)

Example 34 with Appender

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

the class NodeLogger method removeWriter.

/**
 * Removes the previously added {@link java.io.Writer} from the logger.
 *
 * @param writer The Writer to remove.
 */
public static void removeWriter(final Writer writer) {
    synchronized (WRITER) {
        Appender o = WRITER.get(writer);
        if (o != null) {
            if (o != LOG_FILE_APPENDER) {
                Logger.getRootLogger().removeAppender(o);
                WRITER.remove(writer);
            }
        } else {
            getLogger(NodeLogger.class).warn("Could not delete writer: " + writer);
        }
    }
}
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)

Example 35 with Appender

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

the class NodeLogger method removeWorkflowDirAppender.

/**
 * Removes any extra workflow directory appender if it exists.
 * @param workflowDir the directory of the workflow that should no longer be logged
 */
private void removeWorkflowDirAppender(final File workflowDir) {
    if (workflowDir == null) {
        // if the workflowDir is null we do not need to remove the extra log appender
        return;
    }
    final String workflowDirPath = workflowDir.getPath();
    if (workflowDirPath != null) {
        synchronized (WF_APPENDER) {
            final Appender appender = WF_APPENDER.remove(workflowDirPath);
            if (appender != null) {
                appender.close();
                // Remove the appender from all open node loggers
                @SuppressWarnings("unchecked") final Enumeration<Logger> allLoggers = Logger.getRootLogger().getLoggerRepository().getCurrentLoggers();
                while (allLoggers.hasMoreElements()) {
                    allLoggers.nextElement().removeAppender(appender);
                }
            }
        }
    }
}
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) Logger(org.apache.log4j.Logger)

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