View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements. See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache license, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License. You may obtain a copy of the License at
8    *
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the license for the specific language governing permissions and
15   * limitations under the license.
16   */
17  package org.apache.log4j.bridge;
18  
19  import org.apache.log4j.NDC;
20  import org.apache.log4j.helpers.OptionConverter;
21  import org.apache.log4j.spi.LocationInfo;
22  import org.apache.log4j.spi.LoggingEvent;
23  import org.apache.logging.log4j.Level;
24  import org.apache.logging.log4j.Marker;
25  import org.apache.logging.log4j.ThreadContext;
26  import org.apache.logging.log4j.core.LogEvent;
27  import org.apache.logging.log4j.core.impl.ThrowableProxy;
28  import org.apache.logging.log4j.core.time.Instant;
29  import org.apache.logging.log4j.core.time.MutableInstant;
30  import org.apache.logging.log4j.message.Message;
31  import org.apache.logging.log4j.message.SimpleMessage;
32  import org.apache.logging.log4j.spi.MutableThreadContextStack;
33  import org.apache.logging.log4j.util.BiConsumer;
34  import org.apache.logging.log4j.util.ReadOnlyStringMap;
35  import org.apache.logging.log4j.util.TriConsumer;
36  
37  import java.util.HashMap;
38  import java.util.Map;
39  
40  /**
41   * Exposes a Log4j 1 logging event as a Log4j 2 LogEvent.
42   */
43  public class LogEventWrapper implements LogEvent {
44  
45      private final LoggingEvent event;
46      private final ContextDataMap contextData;
47      private final MutableThreadContextStack contextStack;
48      private volatile Thread thread;
49  
50      public LogEventWrapper(LoggingEvent event) {
51          this.event = event;
52          this.contextData = new ContextDataMap(event.getProperties());
53          this.contextStack = new MutableThreadContextStack(NDC.cloneStack());
54      }
55  
56      @Override
57      public LogEvent toImmutable() {
58          return null;
59      }
60  
61      @Override
62      public Map<String, String> getContextMap() {
63          return contextData;
64      }
65  
66      @Override
67      public ReadOnlyStringMap getContextData() {
68          return contextData;
69      }
70  
71      @Override
72      public ThreadContext.ContextStack getContextStack() {
73          return contextStack;
74      }
75  
76      @Override
77      public String getLoggerFqcn() {
78          return null;
79      }
80  
81      @Override
82      public Level getLevel() {
83          return OptionConverter.convertLevel(event.getLevel());
84      }
85  
86      @Override
87      public String getLoggerName() {
88          return event.getLoggerName();
89      }
90  
91      @Override
92      public Marker getMarker() {
93          return null;
94      }
95  
96      @Override
97      public Message getMessage() {
98          return new SimpleMessage(event.getRenderedMessage());
99      }
100 
101     @Override
102     public long getTimeMillis() {
103         return event.getTimeStamp();
104     }
105 
106     @Override
107     public Instant getInstant() {
108         MutableInstant mutable = new MutableInstant();
109         mutable.initFromEpochMilli(event.getTimeStamp(), 0);
110         return mutable;
111     }
112 
113     @Override
114     public StackTraceElement getSource() {
115         LocationInfo info = event.getLocationInformation();
116         return new StackTraceElement(info.getClassName(), info.getMethodName(), info.getFileName(),
117                 Integer.parseInt(info.getLineNumber()));
118     }
119 
120     @Override
121     public String getThreadName() {
122         return event.getThreadName();
123     }
124 
125     @Override
126     public long getThreadId() {
127         Thread thread = getThread();
128         return thread != null ? thread.getId() : 0;
129     }
130 
131     @Override
132     public int getThreadPriority() {
133             Thread thread = getThread();
134             return thread != null ? thread.getPriority() : 0;
135     }
136 
137     private Thread getThread() {
138         if (thread == null) {
139             for (Thread thread : Thread.getAllStackTraces().keySet()) {
140                 if (thread.getName().equals(event.getThreadName())) {
141                     this.thread = thread;
142                     return thread;
143                 }
144             }
145         }
146         return null;
147     }
148 
149     @Override
150     public Throwable getThrown() {
151         if (event.getThrowableInformation() != null) {
152             return event.getThrowableInformation().getThrowable();
153         }
154         return null;
155     }
156 
157     @Override
158     public ThrowableProxy getThrownProxy() {
159         return null;
160     }
161 
162     @Override
163     public boolean isEndOfBatch() {
164         return false;
165     }
166 
167     @Override
168     public boolean isIncludeLocation() {
169         return false;
170     }
171 
172     @Override
173     public void setEndOfBatch(boolean endOfBatch) {
174 
175     }
176 
177     @Override
178     public void setIncludeLocation(boolean locationRequired) {
179 
180     }
181 
182     @Override
183     public long getNanoTime() {
184         return 0;
185     }
186 
187 
188     private static class ContextDataMap extends HashMap<String, String> implements ReadOnlyStringMap {
189 
190         ContextDataMap(Map<String, String> map) {
191             if (map != null) {
192                 super.putAll(map);
193             }
194         }
195 
196         @Override
197         public Map<String, String> toMap() {
198             return this;
199         }
200 
201         @Override
202         public boolean containsKey(String key) {
203             return super.containsKey(key);
204         }
205 
206         @Override
207         public <V> void forEach(BiConsumer<String, ? super V> action) {
208             super.forEach((k,v) -> action.accept(k, (V) v));
209         }
210 
211         @Override
212         public <V, S> void forEach(TriConsumer<String, ? super V, S> action, S state) {
213             super.forEach((k,v) -> action.accept(k, (V) v, state));
214         }
215 
216         @Override
217         public <V> V getValue(String key) {
218             return (V) super.get(key);
219         }
220     }
221 }