private void parseErrorHandler( final ErrorHandler eh, final String errorHandlerPrefix, final Properties props, final LoggerRepository hierarchy) { boolean rootRef = OptionConverter.toBoolean( OptionConverter.findAndSubst(errorHandlerPrefix + ROOT_REF, props), false); if (rootRef) { eh.setLogger(hierarchy.getRootLogger()); } String loggerName = OptionConverter.findAndSubst(errorHandlerPrefix + LOGGER_REF , props); if (loggerName != null) { Logger logger = (loggerFactory == null) ? hierarchy.getLogger(loggerName) : hierarchy.getLogger(loggerName, loggerFactory); eh.setLogger(logger); } String appenderName = OptionConverter.findAndSubst(errorHandlerPrefix + APPENDER_REF_TAG, props); if (appenderName != null) { Appender backup = parseAppender(props, appenderName); if (backup != null) { eh.setBackupAppender(backup); } } }
public void setErrorHandler(ErrorHandler eh) { if(eh == null) { // This is a programming error on the part of the enclosing appender. throw new IllegalArgumentException("Attempted to set null ErrorHandler."); } else { this.errorHandler = eh; } }
/** Set the {@link ErrorHandler} for this Appender. @since 0.9.0 */ public synchronized void setErrorHandler(ErrorHandler eh) { if(eh == null) { // We do not throw exception here since the cause is probably a // bad config file. LogLog.warn("You have tried to set a null error-handler."); } else { this.errorHandler = eh; } }
/** Set the {@link ErrorHandler} for this WriterAppender and also the underlying {@link QuietWriter} if any. */ public synchronized void setErrorHandler(ErrorHandler eh) { if(eh == null) { LogLog.warn("You have tried to set a null error-handler."); } else { this.errorHandler = eh; if(this.qw != null) { this.qw.setErrorHandler(eh); } } }
/** Convert <code>val</code> a String parameter to an object of a given type. */ protected Object convertArg(String val, Class type) { if(val == null) return null; String v = val.trim(); if (String.class.isAssignableFrom(type)) { return val; } else if (Integer.TYPE.isAssignableFrom(type)) { return new Integer(v); } else if (Long.TYPE.isAssignableFrom(type)) { return new Long(v); } else if (Boolean.TYPE.isAssignableFrom(type)) { if ("true".equalsIgnoreCase(v)) { return Boolean.TRUE; } else if ("false".equalsIgnoreCase(v)) { return Boolean.FALSE; } } else if (Priority.class.isAssignableFrom(type)) { return OptionConverter.toLevel(v, (Level) Level.DEBUG); } else if (ErrorHandler.class.isAssignableFrom(type)) { return OptionConverter.instantiateByClassName(v, ErrorHandler.class, null); } return null; }
/** * Constructor */ public FallbackAppender() { ErrorHandler fb = new FallbackErrorHandler(); this.setErrorHandler(fb); fb.setAppender(this); Appender rootAppen = Logger.getRootLogger().getAppender("RootAppender"); if (rootAppen != null) { fb.setBackupAppender(rootAppen); } Logger logger = Logger.getLogger(LoggingInvocationProcessor.class); fb.setLogger(logger); }
/** * Constructor */ public FailbackAppender() { ErrorHandler fb = new FallbackErrorHandler(); this.setErrorHandler(fb); fb.setAppender(this); Appender rootAppen = Logger.getRootLogger().getAppender("RootAppender"); if (rootAppen != null) { fb.setBackupAppender(rootAppen); } Logger logger = Logger.getLogger(LoggingInvocationProcessor.class); fb.setLogger(logger); }
/** * Set the {@link ErrorHandler} for this Appender. * * @since 0.9.0 */ @Override public synchronized void setErrorHandler(ErrorHandler eh) { if (eh != null) { this.errorHandler = eh; } }
@Override public ErrorHandler getErrorHandler() { // TODO Auto-generated method stub return null; }
@Override public void setErrorHandler(ErrorHandler arg0) { // TODO Auto-generated method stub }
@Override public void setErrorHandler(ErrorHandler errorHandler) { this.errorHandler = errorHandler; }
@Override public ErrorHandler getErrorHandler() { return errorHandler; }
@Override public ErrorHandler getErrorHandler() { return null; }
@Override public void setErrorHandler(ErrorHandler errorHandler) { super.setErrorHandler(errorHandler); }
@Override public ErrorHandler getErrorHandler() { return super.getErrorHandler(); }
public ErrorHandler getErrorHandler() { return null; }
public void setErrorHandler(ErrorHandler errorHandler) { }
@Override public ErrorHandler getErrorHandler() { return fallbackErrorHandler; }
public CountingQuietWriterEx(String key, String fileName, Writer writer, ErrorHandler eh) { super(writer, eh); this.key = key; this.fileName = fileName; }
public QuietWriter(Writer writer, ErrorHandler errorHandler) { super(writer); setErrorHandler(errorHandler); }
public SyslogQuietWriter(Writer writer, int syslogFacility, ErrorHandler eh) { super(writer, eh); this.syslogFacility = syslogFacility; }
public CountingQuietWriter(Writer writer, ErrorHandler eh) { super(writer, eh); }
/** Used internally to parse an {@link ErrorHandler} element. */ protected void parseErrorHandler(Element element, Appender appender) { ErrorHandler eh = (ErrorHandler) OptionConverter.instantiateByClassName( subst(element.getAttribute(CLASS_ATTR)), org.apache.log4j.spi.ErrorHandler.class, null); if(eh != null) { eh.setAppender(appender); PropertySetter propSetter = new PropertySetter(eh); NodeList children = element.getChildNodes(); final int length = children.getLength(); for (int loop = 0; loop < length; loop++) { Node currentNode = children.item(loop); if (currentNode.getNodeType() == Node.ELEMENT_NODE) { Element currentElement = (Element) currentNode; String tagName = currentElement.getTagName(); if(tagName.equals(PARAM_TAG)) { setParameter(currentElement, propSetter); } else if(tagName.equals(APPENDER_REF_TAG)) { eh.setBackupAppender(findAppenderByReference(currentElement)); } else if(tagName.equals(LOGGER_REF)) { String loggerName = currentElement.getAttribute(REF_ATTR); Logger logger = (catFactory == null) ? repository.getLogger(loggerName) : repository.getLogger(loggerName, catFactory); eh.setLogger(logger); } else if(tagName.equals(ROOT_REF)) { Logger root = repository.getRootLogger(); eh.setLogger(root); } else { quietParseUnrecognizedElement(eh, currentElement, props); } } } propSetter.activate(); appender.setErrorHandler(eh); } }
public HttpThread(HttpClient httpClient, ErrorHandler errorHandler) { this.httpClient = httpClient; this.errorHandler = errorHandler; }
@Test public void testLogging() { Log4jFactory factory = new Log4jFactory(); org.apache.log4j.Logger loggerImpl= org.apache.log4j.Logger.getLogger("testlogger"); final StringBuffer sb = new StringBuffer(); loggerImpl.addAppender(new Appender() { @Override public void setName(String name) { } @Override public void setLayout(Layout layout) { } @Override public void setErrorHandler(ErrorHandler errorHandler) { } @Override public boolean requiresLayout() { return false; } @Override public String getName() { return null; } @Override public Layout getLayout() { return null; } @Override public Filter getFilter() { return null; } @Override public ErrorHandler getErrorHandler() { return null; } @Override public void doAppend(LoggingEvent event) { sb.append(event.getLevel()+" "+event.getMessage()+"\n"); } @Override public void close() { } @Override public void clearFilters() { } @Override public void addFilter(Filter newFilter) { } }); Logger logger = factory.getLogger("testlogger"); logger.trace("trace message"); logger.debug("debug message"); logger.info("info message"); logger.warn("warn message"); logger.error("error message"); logger.error("error exception", new RuntimeException("Testing error logger, please ignore this exception")); String expected = "INFO info message\n" + "WARN warn message\n" + "ERROR error message\n" + "ERROR error exception\n"; Assert.assertEquals(expected, sb.toString()); }
Appender parseAppender(Properties props, String appenderName) { Appender appender = registryGet(appenderName); if((appender != null)) { LogLog.debug("Appender \"" + appenderName + "\" was already parsed."); return appender; } // Appender was not previously initialized. String prefix = APPENDER_PREFIX + appenderName; String layoutPrefix = prefix + ".layout"; appender = (Appender) OptionConverter.instantiateByKey(props, prefix, org.apache.log4j.Appender.class, null); if(appender == null) { LogLog.error( "Could not instantiate appender named \"" + appenderName+"\"."); return null; } appender.setName(appenderName); if(appender instanceof OptionHandler) { if(appender.requiresLayout()) { Layout layout = (Layout) OptionConverter.instantiateByKey(props, layoutPrefix, Layout.class, null); if(layout != null) { appender.setLayout(layout); LogLog.debug("Parsing layout options for \"" + appenderName +"\"."); //configureOptionHandler(layout, layoutPrefix + ".", props); PropertySetter.setProperties(layout, props, layoutPrefix + "."); LogLog.debug("End of parsing for \"" + appenderName +"\"."); } } final String errorHandlerPrefix = prefix + ".errorhandler"; String errorHandlerClass = OptionConverter.findAndSubst(errorHandlerPrefix, props); if (errorHandlerClass != null) { ErrorHandler eh = (ErrorHandler) OptionConverter.instantiateByKey(props, errorHandlerPrefix, ErrorHandler.class, null); if (eh != null) { appender.setErrorHandler(eh); LogLog.debug("Parsing errorhandler options for \"" + appenderName +"\"."); parseErrorHandler(eh, errorHandlerPrefix, props, repository); final Properties edited = new Properties(); final String[] keys = new String[] { errorHandlerPrefix + "." + ROOT_REF, errorHandlerPrefix + "." + LOGGER_REF, errorHandlerPrefix + "." + APPENDER_REF_TAG }; for(Iterator iter = props.entrySet().iterator();iter.hasNext();) { Map.Entry entry = (Map.Entry) iter.next(); int i = 0; for(; i < keys.length; i++) { if(keys[i].equals(entry.getKey())) break; } if (i == keys.length) { edited.put(entry.getKey(), entry.getValue()); } } PropertySetter.setProperties(eh, edited, errorHandlerPrefix + "."); LogLog.debug("End of errorhandler parsing for \"" + appenderName +"\"."); } } //configureOptionHandler((OptionHandler) appender, prefix + ".", props); PropertySetter.setProperties(appender, props, prefix + "."); LogLog.debug("Parsed \"" + appenderName +"\" options."); } parseAppenderFilters(props, appenderName, appender); registryPut(appender); return appender; }
@Override public ErrorHandler getErrorHandler() { return appender.getErrorHandler(); }