001package com.identityworksllc.iiq.common.logging;
002
003import java.io.Serializable;
004import java.util.Arrays;
005import java.util.Date;
006import java.util.Objects;
007import java.util.StringJoiner;
008
009/**
010 * An abstract log listener that will receive log events instead of queueing them
011 */
012@FunctionalInterface
013public interface LogListener {
014        
015        /**
016         * A log message emitted if a listener is assigned to the log capture engine.
017         * 
018         * TODO make this more Beanshell friendly (map?)
019         */
020        @SuppressWarnings("javadoc")
021        final class LogMessage implements Serializable {
022                                
023                /**
024                 * 
025                 */
026                private static final long serialVersionUID = 1L;
027                
028                private final Date date;
029                private final String level;
030                private final String message;
031                private final String source;
032                private final String[] throwableLines;
033                
034                public LogMessage(Date date, String level, String source, String message, String[] throwableLines) {
035                        if (date == null) {
036                                throw new NullPointerException("date");
037                        }
038                        if (message == null) {
039                                throw new NullPointerException("message");
040                        }
041                        
042                        this.date = date;
043                        this.message = message;
044
045                        if (level == null) {
046                                this.level = "";
047                        } else {
048                                this.level = level;
049                        }
050                        
051                        if (source == null) {
052                                this.source = "";
053                        } else {
054                                this.source = source;
055                        }
056
057                        if (throwableLines != null) {
058                                this.throwableLines = throwableLines;
059                        } else {
060                                this.throwableLines = new String[0];
061                        }
062                }
063
064                @Override
065                public boolean equals(Object o) {
066                        if (this == o) return true;
067                        if (!(o instanceof LogMessage)) return false;
068                        LogMessage that = (LogMessage) o;
069                        return Objects.equals(date, that.date) &&
070                                        Objects.equals(level, that.level) &&
071                                        Objects.equals(source, that.source) &&
072                                        Objects.equals(message, that.message) &&
073                                        Arrays.equals(throwableLines, that.throwableLines);
074                }
075
076                /**
077                 * Retrieves the date of
078                 * @return
079                 */
080                public final Date getDate() {
081                        return date;
082                }
083
084                public final String getLevel() {
085                        return level;
086                }
087
088                public final String getMessage() {
089                        return message;
090                }
091
092                public final String getSource() {
093                        return source;
094                }
095
096                public final String[] getThrowableLines() {
097                        return this.throwableLines;
098                }
099
100                public final boolean hasThrowable() {
101                        return this.throwableLines.length > 0;
102                }
103
104                @Override
105                public int hashCode() {
106                        return Objects.hash(date, level, source, message, throwableLines);
107                }
108
109                @Override
110                public String toString() {
111                        return new StringJoiner(", ", LogMessage.class.getSimpleName() + "[", "]")
112                                        .add("date=" + date)
113                                        .add("level='" + level + "'")
114                                        .add("source='" + source + "'")
115                                        .add("message='" + message + "'")
116                                        .add("error?='" + hasThrowable() + "'")
117                                        .toString();
118                }
119        }
120        
121        /**
122         * A callback invoked on this listener when a log message is received in the listener's thread.
123         * @param message The message to log. All fields are guaranteed to not be null.
124         */
125        void logMessageReceived(LogMessage message);
126}