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 org.apache.logging.log4j.Level;
20  
21  /**
22   * Default router: enqueue the event for asynchronous logging in the background thread, unless the current thread is the
23   * background thread and the queue is full (enqueueing would cause a deadlock). In that case send the event directly to
24   * the appender (in the current thread).
25   */
26  public class DefaultAsyncQueueFullPolicy implements AsyncQueueFullPolicy {
27      @Override
28      public EventRoute getRoute(final long backgroundThreadId, final Level level) {
29  
30          // LOG4J2-471: prevent deadlock when RingBuffer is full and object
31          // being logged calls Logger.log() from its toString() method
32          if (Thread.currentThread().getId() == backgroundThreadId) {
33              return EventRoute.SYNCHRONOUS;
34          }
35          return EventRoute.ENQUEUE;
36      }
37  }