Search in sources :

Example 1 with RollingResourceAppender

use of lucee.commons.io.log.log4j.appender.RollingResourceAppender 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)

Aggregations

IOException (java.io.IOException)1 PrintWriter (java.io.PrintWriter)1 Charset (java.nio.charset.Charset)1 HashMap (java.util.HashMap)1 Entry (java.util.Map.Entry)1 ConsoleAppender (lucee.commons.io.log.log4j.appender.ConsoleAppender)1 DatasourceAppender (lucee.commons.io.log.log4j.appender.DatasourceAppender)1 RollingResourceAppender (lucee.commons.io.log.log4j.appender.RollingResourceAppender)1 TaskAppender (lucee.commons.io.log.log4j.appender.TaskAppender)1 Resource (lucee.commons.io.res.Resource)1 PageException (lucee.runtime.exp.PageException)1 Appender (org.apache.log4j.Appender)1 AppenderSkeleton (org.apache.log4j.AppenderSkeleton)1 Level (org.apache.log4j.Level)1