001package org.slf4j.event;
002
003import java.util.ArrayList;
004import java.util.Arrays;
005import java.util.List;
006
007import org.slf4j.Logger;
008import org.slf4j.Marker;
009
010/**
011 * A default implementation of {@link LoggingEvent}.
012 * 
013 * @author Ceki Gülcü
014 *
015 * @since 2.0.0
016 */
017public class DefaultLoggingEvent implements LoggingEvent {
018
019    Logger logger;
020    Level level;
021
022    String message;
023    List<Marker> markers;
024    List<Object> arguments;
025    List<KeyValuePair> keyValuePairs;
026
027    Throwable throwable;
028    String threadName;
029    long timeStamp;
030    
031    String callerBoundary;
032
033    public DefaultLoggingEvent(Level level, Logger logger) {
034        this.logger = logger;
035        this.level = level;
036    }
037
038    public void addMarker(Marker marker) {
039        if (markers == null) {
040            markers = new ArrayList<>(2);
041        }
042        markers.add(marker);
043    }
044
045    @Override
046    public List<Marker> getMarkers() {
047        return markers;
048    }
049
050    public void addArgument(Object p) {
051        getNonNullArguments().add(p);
052    }
053
054    public void addArguments(Object... args) {
055        getNonNullArguments().addAll(Arrays.asList(args));
056    }
057
058    private List<Object> getNonNullArguments() {
059        if (arguments == null) {
060            arguments = new ArrayList<>(3);
061        }
062        return arguments;
063    }
064
065    @Override
066    public List<Object> getArguments() {
067        return arguments;
068    }
069
070    @Override
071    public Object[] getArgumentArray() {
072        if (arguments == null)
073            return null;
074        return arguments.toArray();
075    }
076
077    public void addKeyValue(String key, Object value) {
078        getNonnullKeyValuePairs().add(new KeyValuePair(key, value));
079    }
080
081    private List<KeyValuePair> getNonnullKeyValuePairs() {
082        if (keyValuePairs == null) {
083            keyValuePairs = new ArrayList<>(4);
084        }
085        return keyValuePairs;
086    }
087
088    @Override
089    public List<KeyValuePair> getKeyValuePairs() {
090        return keyValuePairs;
091    }
092
093    public void setThrowable(Throwable cause) {
094        this.throwable = cause;
095    }
096
097    @Override
098    public Level getLevel() {
099        return level;
100    }
101
102    @Override
103    public String getLoggerName() {
104        return logger.getName();
105    }
106
107    @Override
108    public String getMessage() {
109        return message;
110    }
111
112    public void setMessage(String message) {
113        this.message = message;
114    }
115
116    @Override
117    public Throwable getThrowable() {
118        return throwable;
119    }
120
121    public String getThreadName() {
122        return threadName;
123    }
124
125    public long getTimeStamp() {
126        return timeStamp;
127    }
128
129    public void setTimeStamp(long timeStamp) {
130        this.timeStamp = timeStamp;
131    }
132
133    public void setCallerBoundary(String fqcn) {
134        this.callerBoundary = fqcn;
135    }
136    
137    public String getCallerBoundary() {
138        return callerBoundary;
139    }
140}