1 package com.nilhcem.fakesmtp.gui.tab;
2
3 import ch.qos.logback.classic.spi.ILoggingEvent;
4 import ch.qos.logback.core.spi.AppenderAttachable;
5 import com.nilhcem.fakesmtp.core.Configuration;
6 import com.nilhcem.fakesmtp.gui.info.ClearAllButton;
7 import com.nilhcem.fakesmtp.log.SMTPLogsAppender;
8 import com.nilhcem.fakesmtp.log.SMTPLogsObservable;
9 import org.slf4j.Logger;
10 import org.slf4j.LoggerFactory;
11
12 import javax.swing.JScrollPane;
13 import javax.swing.JTextArea;
14 import java.text.SimpleDateFormat;
15 import java.util.Date;
16 import java.util.Observable;
17 import java.util.Observer;
18
19
20
21
22
23
24
25 public final class LogsPane implements Observer {
26
27 private final JScrollPane logsPane = new JScrollPane();
28 private final SimpleDateFormat dateFormat = new SimpleDateFormat("hh:mm:ss a");
29 private final JTextArea logsArea = new JTextArea();
30
31
32
33
34 public LogsPane() {
35 logsArea.setEditable(false);
36 logsPane.getViewport().add(logsArea, null);
37 addObserverToSmtpLogAppender();
38 }
39
40
41
42
43
44
45 public JScrollPane get() {
46 return logsPane;
47 }
48
49
50
51
52
53
54
55
56 private void addObserverToSmtpLogAppender() {
57 Logger smtpLogger = LoggerFactory.getLogger(org.subethamail.smtp.server.Session.class);
58 String appenderName = Configuration.INSTANCE.get("logback.appender.name");
59
60 @SuppressWarnings("unchecked")
61 SMTPLogsAppender<ILoggingEvent> appender = (SMTPLogsAppender<ILoggingEvent>)
62 ((AppenderAttachable<ILoggingEvent>) smtpLogger).getAppender(appenderName);
63 if (appender == null) {
64 LoggerFactory.getLogger(LogsPane.class).error("Can't find logger: {}", appenderName);
65 } else {
66 appender.getObservable().addObserver(this);
67 }
68 }
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83 @Override
84 public void update(Observable o, Object log) {
85 if (o instanceof SMTPLogsObservable) {
86
87 logsArea.append(String.format("%s - %s%n", dateFormat.format(new Date()), log));
88 logsArea.setCaretPosition(logsArea.getText().length());
89 } else if (o instanceof ClearAllButton) {
90
91 logsArea.setText("");
92 }
93 }
94 }