1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.logging.log4j.core.jmx;
18
19 import java.util.List;
20 import java.util.concurrent.Executor;
21 import java.util.concurrent.atomic.AtomicLong;
22
23 import javax.management.MBeanNotificationInfo;
24 import javax.management.Notification;
25 import javax.management.NotificationBroadcasterSupport;
26 import javax.management.ObjectName;
27
28 import org.apache.logging.log4j.Level;
29 import org.apache.logging.log4j.status.StatusData;
30 import org.apache.logging.log4j.status.StatusListener;
31 import org.apache.logging.log4j.status.StatusLogger;
32
33
34
35
36 public class StatusLoggerAdmin extends NotificationBroadcasterSupport implements
37 StatusListener, StatusLoggerAdminMBean {
38
39 private final AtomicLong sequenceNo = new AtomicLong();
40 private final ObjectName objectName;
41
42
43
44
45
46
47
48 public StatusLoggerAdmin(Executor executor) {
49 super(executor, createNotificationInfo());
50 try {
51 objectName = new ObjectName(NAME);
52 } catch (Exception e) {
53 throw new IllegalStateException(e);
54 }
55 StatusLogger.getLogger().registerListener(this);
56 }
57
58 private static MBeanNotificationInfo createNotificationInfo() {
59 String[] notifTypes = new String[] { NOTIF_TYPE_DATA,
60 NOTIF_TYPE_MESSAGE };
61 String name = Notification.class.getName();
62 String description = "StatusLogger has logged an event";
63 return new MBeanNotificationInfo(notifTypes, name, description);
64 }
65
66 @Override
67 public String[] getStatusDataHistory() {
68 List<StatusData> data = getStatusData();
69 String[] result = new String[data.size()];
70 for (int i = 0; i < result.length; i++) {
71 result[i] = data.get(i).getFormattedStatus();
72 }
73 return result;
74 }
75
76 @Override
77 public List<StatusData> getStatusData() {
78 return StatusLogger.getLogger().getStatusData();
79 }
80
81 @Override
82 public String getLevel() {
83 return StatusLogger.getLogger().getLevel().name();
84 }
85
86 @Override
87 public void setLevel(String level) {
88 StatusLogger.getLogger().setLevel(Level.valueOf(level));
89 }
90
91
92
93
94
95
96
97
98 @Override
99 public void log(StatusData data) {
100 Notification notifMsg = new Notification(NOTIF_TYPE_MESSAGE,
101 getObjectName(), nextSeqNo(), now(), data.getFormattedStatus());
102 sendNotification(notifMsg);
103
104 Notification notifData = new Notification(NOTIF_TYPE_DATA,
105 getObjectName(), nextSeqNo(), now());
106 notifData.setUserData(data);
107 sendNotification(notifData);
108 }
109
110
111 public ObjectName getObjectName() {
112 return objectName;
113 }
114
115 private long nextSeqNo() {
116 return sequenceNo.getAndIncrement();
117 }
118
119 private long now() {
120 return System.currentTimeMillis();
121 }
122 }