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