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.async;
18  
19  import java.net.URI;
20  
21  import org.apache.logging.log4j.core.Logger;
22  import org.apache.logging.log4j.core.LoggerContext;
23  import org.apache.logging.log4j.core.config.Configuration;
24  import org.apache.logging.log4j.core.config.DefaultConfiguration;
25  import org.apache.logging.log4j.core.jmx.RingBufferAdmin;
26  import org.apache.logging.log4j.message.MessageFactory;
27  import org.apache.logging.log4j.status.StatusLogger;
28  
29  /**
30   * {@code LoggerContext} that creates {@code AsyncLogger} objects.
31   */
32  public class AsyncLoggerContext extends LoggerContext {
33  
34      private static final long serialVersionUID = 1L;
35  
36      private final AsyncLoggerDisruptor loggerDisruptor;
37  
38      public AsyncLoggerContext(final String name) {
39          super(name);
40          loggerDisruptor = new AsyncLoggerDisruptor(name);
41      }
42  
43      public AsyncLoggerContext(final String name, final Object externalContext) {
44          super(name, externalContext);
45          loggerDisruptor = new AsyncLoggerDisruptor(name);
46      }
47  
48      public AsyncLoggerContext(final String name, final Object externalContext, final URI configLocn) {
49          super(name, externalContext, configLocn);
50          loggerDisruptor = new AsyncLoggerDisruptor(name);
51      }
52  
53      public AsyncLoggerContext(final String name, final Object externalContext, final String configLocn) {
54          super(name, externalContext, configLocn);
55          loggerDisruptor = new AsyncLoggerDisruptor(name);
56      }
57  
58      @Override
59      protected Logger newInstance(final LoggerContext ctx, final String name, final MessageFactory messageFactory) {
60          return new AsyncLogger(ctx, name, messageFactory, loggerDisruptor);
61      }
62      
63      @Override
64      public void setName(final String name) {
65          super.setName("AsyncContext[" + name + "]");
66          loggerDisruptor.setContextName(name);
67      }
68  
69      /*
70       * (non-Javadoc)
71       * 
72       * @see org.apache.logging.log4j.core.LoggerContext#start()
73       */
74      @Override
75      public void start() {
76          loggerDisruptor.start();
77          super.start();
78      }
79  
80      /*
81       * (non-Javadoc)
82       * 
83       * @see org.apache.logging.log4j.core.LoggerContext#start(org.apache.logging.log4j.core.config.Configuration)
84       */
85      @Override
86      public void start(Configuration config) {
87          maybeStartHelper(config);
88          super.start(config);
89      }
90  
91      private void maybeStartHelper(Configuration config) {
92          // If no log4j configuration was found, there are no loggers
93          // and there is no point in starting the disruptor (which takes up
94          // significant memory and starts a thread).
95          if (config instanceof DefaultConfiguration) {
96              StatusLogger.getLogger().debug("[{}] Not starting Disruptor for DefaultConfiguration.", getName());
97          } else {
98              loggerDisruptor.start();
99          }
100     }
101 
102     @Override
103     public void stop() {
104         loggerDisruptor.stop(); // first stop Disruptor
105         super.stop();
106     }
107 
108     /**
109      * Creates and returns a new {@code RingBufferAdmin} that instruments the ringbuffer of the {@code AsyncLogger}
110      * objects in this {@code LoggerContext}.
111      *
112      * @return a new {@code RingBufferAdmin} that instruments the ringbuffer
113      */
114     public RingBufferAdmin createRingBufferAdmin() {
115         return loggerDisruptor.createRingBufferAdmin(getName());
116     }
117 
118     /**
119      * Signals this context whether it is allowed to use ThreadLocal objects for efficiency.
120      * @param useThreadLocals whether this context is allowed to use ThreadLocal objects
121      */
122     public void setUseThreadLocals(final boolean useThreadLocals) {
123         loggerDisruptor.setUseThreadLocals(useThreadLocals);
124     }
125 }