Code example for Handler

Methods: setEncodingsetFiltersetFormattersetLevel

0
        final String formatter = manager.getProperty(className + ".formatter");
        setFormatter(makeFormatter(formatter));
         
        final String encoding = manager.getProperty(className + ".encoding");
        try { 
            this.stdOutHandler.setEncoding(encoding);
            this.stdErrHandler.setEncoding(encoding);
        } catch (final UnsupportedEncodingException e) {
            Log.logException(e);
        } 
         
        final String ignoreCtrlChrStr = manager.getProperty(className + ".ignoreCtrlChr");
        this.ignoreCtrlChr = (ignoreCtrlChrStr==null) ? false : "true".equalsIgnoreCase(ignoreCtrlChrStr);
         
    }     
     
    private Level parseLevel(final String levelName) {
        try { 
            return (levelName == null) ? Level.INFO : Level.parse(levelName);
        } catch (final Exception e) {
            return Level.ALL;
        } 
    } 
     
    private Filter makeFilter(final String name) {
        if (name == null) return null;
         
        Filter f = null;
        try { 
            final Class<?> c = Class.forName(name);
            f = (Filter)c.newInstance();
        } catch (final Exception e) {
            if (name != null) {
                System.err.println("Unable to load filter: " + name);
            } 
        } 
        return f;
    }     
     
    private Formatter makeFormatter(final String name) {
        if (name == null) return null;
         
        Formatter f = null;
        try { 
            final Class<?> c = Class.forName(name);
            f = (Formatter)c.newInstance();
        } catch (final Exception e) {
            f = new SimpleFormatter();
        } 
        return f;
    }     
     
     
    public final void publish(final LogRecord record) {
        if (!isLoggable(record)) return;
         
        if (this.ignoreCtrlChr) {
            String msg = record.getMessage();
            if (msg != null) {
                msg = msg.replaceAll("[\u0000-\u0008\u000B\u000C\u000E-\u001F]"," ");
            } 
            record.setMessage(msg);
        } 
         
        if (record.getLevel().intValue() >= splitLevel.intValue()) {
            this.stdErrHandler.publish(record);
        } else { 
            this.stdOutHandler.publish(record);
        } 
        flush(); 
    } 
 
    public void flush() { 
        this.stdOutHandler.flush();
        this.stdErrHandler.flush();
    } 
 
    public void close() throws SecurityException {
        this.stdOutHandler.close();  
        this.stdErrHandler.close();
    } 
     
    @Override 
    public synchronized void setLevel(final Level newLevel) throws SecurityException {
        super.setLevel(newLevel);
    } 
     
    public void setLevels(final Level stdOutLevel, final Level splitLevel, final Level stdErrLevel) throws SecurityException {
        this.stdOutHandler.setLevel(stdOutLevel);
        this.splitLevel = splitLevel;
        this.stdErrHandler.setLevel(stdErrLevel);
    } 
     
    @Override 
    public void setFormatter(final Formatter newFormatter) throws SecurityException {
        super.setFormatter(newFormatter);
        if (newFormatter == null) return;
        try { 
            this.stdOutHandler.setFormatter(newFormatter.getClass().newInstance());
            this.stdErrHandler.setFormatter(newFormatter.getClass().newInstance());
        } catch (final Exception e) {
            throw new SecurityException(e.getMessage());
        } 
    } 
 
    @Override 
    public final void setFilter(final Filter newFilter) throws SecurityException {
        super.setFilter(newFilter);
        if (newFilter == null) return;
        try { 
            this.stdOutHandler.setFilter(newFilter.getClass().newInstance());
            this.stdErrHandler.setFilter(newFilter.getClass().newInstance());
        } catch (final Exception e) {
            throw new SecurityException(e.getMessage());
        } 
    }