Posted By

cruelfate on 01/21/11


Tagged

log4j synchronous appender


Versions (?)

A demonstration showing that Log4J RollingFileAppender is actually synchronous.


 / Published in: Java
 

The Log4J RollingFileAppender is actually synchronous.

  1. package org.example;
  2.  
  3. import java.io.IOException;
  4. import java.text.MessageFormat;
  5.  
  6. import org.apache.log4j.Layout;
  7. import org.apache.log4j.Level;
  8. import org.apache.log4j.Logger;
  9. import org.apache.log4j.PatternLayout;
  10. import org.apache.log4j.RollingFileAppender;
  11. import org.apache.log4j.spi.LoggingEvent;
  12.  
  13. public class L4JRFAisSynchronous {
  14.  
  15. static RFA appender;
  16. static {
  17. System.setProperty("log4j.defaultInitOverride", "true");
  18. // System.setProperty("log4j.debug", "true");
  19. Logger rootLogger = Logger.getRootLogger();
  20. if (!rootLogger.getAllAppenders().hasMoreElements()) {
  21. rootLogger.setLevel(Level.INFO);
  22. try {
  23. appender = new RFA(new PatternLayout("%d{ISO8601} [%t] %-5p %c %x - %m%n"), "log/L4J.log", true);
  24. appender.setMaxFileSize("10240KB");
  25. appender.setMaxBackupIndex(10);
  26. rootLogger.addAppender(appender);
  27. } catch (IOException e) {
  28. throw new RuntimeException("Appender configuration failed", e);
  29. }
  30. }
  31. }
  32. static final Logger log = Logger.getLogger(L4JRFAisSynchronous.class);
  33. static final long N = 20000;
  34.  
  35. public static void main(String[] args) {
  36. System.out.println("Appender: " + appender.getClass().getSuperclass().toString());
  37. System.out.println("# of iterations per trial = " + N);
  38. trial(0);
  39. trial(0);
  40. trial(0);
  41. trial(0);
  42. trial(0);
  43. trial(0);
  44. trial(1);
  45. trial(10);
  46. trial(100);
  47. trial(0);
  48. trial(1);
  49. trial(10);
  50. trial(0);
  51. trial(100);
  52. }
  53.  
  54. private static void trial(int delay) {
  55. appender.setDelay(delay);
  56. long start = System.nanoTime();
  57. for (int i = 0; i < N; i++) {
  58. log.info(delay + " " + i);
  59. }
  60. long end = System.nanoTime() - start;
  61. long total = end/N;
  62. System.out.println(MessageFormat.format("{0} nS. per append with delay = {1} nS. : appender time = {2} nS. per call.",
  63. total,
  64. delay,
  65. total - delay));
  66. }
  67.  
  68. public static class RFA extends RollingFileAppender {
  69.  
  70. int delay = 0;
  71.  
  72. public int getDelay() {
  73. return delay;
  74. }
  75.  
  76. public void setDelay(int time) {
  77. this.delay = time;
  78. }
  79.  
  80. public RFA() {
  81. super();
  82. }
  83.  
  84. public RFA(Layout layout, String filename, boolean append)
  85. throws IOException {
  86. super(layout, filename, append);
  87. }
  88.  
  89. public RFA(Layout layout, String filename) throws IOException {
  90. super(layout, filename);
  91. }
  92.  
  93. @Override
  94. protected void subAppend(LoggingEvent arg0) {
  95. try {
  96. Thread.sleep(0, delay);
  97. } catch (InterruptedException e) {
  98. // ignore
  99. }
  100. super.subAppend(arg0);
  101. }
  102. }
  103. }

Report this snippet  

Comments

RSS Icon Subscribe to comments
Posted By: abebe on January 22, 2011

ww

You need to login to post a comment.