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.logging.log4j.core.jackson;
18  
19  import org.apache.logging.log4j.Level;
20  import org.apache.logging.log4j.Marker;
21  import org.apache.logging.log4j.ThreadContext.ContextStack;
22  import org.apache.logging.log4j.core.LogEvent;
23  import org.apache.logging.log4j.core.impl.ExtendedStackTraceElement;
24  import org.apache.logging.log4j.core.impl.ThrowableProxy;
25  
26  import com.fasterxml.jackson.databind.Module.SetupContext;
27  import com.fasterxml.jackson.databind.module.SimpleModule;
28  
29  /**
30   * Initialization utils.
31   * <p>
32   * <em>Consider this class private.</em>
33   * </p>
34   */
35  class Initializers {
36  
37      /**
38       * Used to set up {@link SetupContext} from different {@link SimpleModule}s.
39       */
40      static class SetupContextInitializer {
41  
42          void setupModule(final SetupContext context) {
43              // JRE classes: we cannot edit those with Jackson annotations
44              context.setMixInAnnotations(StackTraceElement.class, StackTraceElementMixIn.class);
45              // Log4j API classes: we do not want to edit those with Jackson annotations because the API module should not depend on Jackson.
46              context.setMixInAnnotations(Marker.class, MarkerMixIn.class);
47              context.setMixInAnnotations(Level.class, LevelMixIn.class);
48              context.setMixInAnnotations(LogEvent.class, LogEventMixIn.class);
49              // Log4j Core classes: we do not want to bring in Jackson at runtime if we do not have to.
50              context.setMixInAnnotations(ExtendedStackTraceElement.class, ExtendedStackTraceElementMixIn.class);
51              context.setMixInAnnotations(ThrowableProxy.class, ThrowableProxyMixIn.class);            
52          }
53      }
54  
55      /**
56       * Used to set up {@link SimpleModule} from different {@link SimpleModule} subclasses.
57       */
58      static class SimpleModuleInitializer {
59          void initialize(final SimpleModule simpleModule) {
60              // Workaround because mix-ins do not work for classes that already have a built-in deserializer.
61              // See Jackson issue 429.
62              simpleModule.addDeserializer(StackTraceElement.class, new Log4jStackTraceElementDeserializer());
63              simpleModule.addDeserializer(ContextStack.class, new MutableThreadContextStackDeserializer());
64          }
65      }
66  
67  }