1 package com.atlassian.core.logging;
2
3 import org.apache.log4j.spi.LoggingEvent;
4
5 import java.util.List;
6 import java.util.LinkedList;
7
8
9
10
11 public class ThreadLocalErrorCollection
12 {
13 public static final int DEFAULT_LIMIT =100;
14 private static ThreadLocal threadLocalCollection = new ThreadLocal(){
15 protected Object initialValue(){
16 return new LinkedList();
17 }
18 };
19
20 private static ThreadLocal threadLocalEnabled = new ThreadLocal(){
21 protected Object initialValue(){
22 return Boolean.FALSE;
23 }
24 };
25
26 private static int limit = DEFAULT_LIMIT;
27
28 public static void add(long timeInMillis, LoggingEvent e)
29 {
30 if (!isEnabled())
31 return;
32
33 List loggingEvents = getList();
34
35 loggingEvents.add(new DatedLoggingEvent(timeInMillis, e));
36
37 while (loggingEvents.size() > limit)
38 loggingEvents.remove(0);
39
40 }
41
42 public static void clear()
43 {
44 getList().clear();
45 }
46
47 public static List getList()
48 {
49 List list = (List) threadLocalCollection.get();
50 return list;
51 }
52
53 public static boolean isEmpty()
54 {
55 return getList().isEmpty();
56 }
57
58 public static int getLimit()
59 {
60 return limit;
61 }
62
63 public static void setLimit(int limit)
64 {
65 ThreadLocalErrorCollection.limit = limit;
66 }
67
68 public static boolean isEnabled()
69 {
70 Boolean enabledState = (Boolean) threadLocalEnabled.get();
71 return Boolean.TRUE.equals(enabledState);
72 }
73
74 public static void enable()
75 {
76 threadLocalEnabled.set(Boolean.TRUE);
77 }
78
79 public static void disable()
80 {
81 threadLocalEnabled.set(Boolean.FALSE);
82 }
83 }