Posted By

dhsmith1001 on 03/19/15


Tagged

log4j


Versions (?)

Find all log4j appenders


 / Published in: Java
 

Find all the current log4j appenders by iterating through all loggers and checking the appender references.

  1. public ArrayList<Appender> findAllAppenders()
  2. {
  3. ArrayList<Appender> appenderList = new ArrayList<>();
  4.  
  5. Logger logger = org.apache.log4j.LogManager.getRootLogger();
  6. Enumeration appenders = logger.getAllAppenders();
  7. if (appenders != null) {
  8. while (appenders.hasMoreElements()) {
  9. Appender appender = (Appender) appenders.nextElement();
  10. if (appender != null) {
  11. appenderList.add(appender);
  12. }
  13. }
  14. }
  15.  
  16. Enumeration loggers = org.apache.log4j.LogManager.getCurrentLoggers();
  17. while (loggers.hasMoreElements()) {
  18. logger = (Logger) loggers.nextElement();
  19. appenders = logger.getAllAppenders();
  20. if (appenders != null) {
  21. while (appenders.hasMoreElements()) {
  22. Appender appender = (Appender) appenders.nextElement();
  23. if (appender != null) {
  24. appenderList.add(appender);
  25. }
  26. }
  27. }
  28. }
  29. return appenderList;
  30. }

Report this snippet  

Comments

RSS Icon Subscribe to comments
Posted By: dhsmith1001 on March 19, 2015

To find a specific appender, use either its name or implementing class:

if (appender instanceof ConsoleAppender) { System.out.println("Found ConsoleAppender with name " + appender.getName()); }

You need to login to post a comment.