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  import org.apache.logging.log4j.Marker;
21  import org.apache.logging.log4j.core.LogEvent;
22  import org.apache.logging.log4j.core.appender.AsyncAppender;
23  import org.apache.logging.log4j.message.Message;
24  
25  /**
26   * Enumeration over the different destinations where a log event can be sent.
27   *
28   * @see AsyncQueueFullPolicy
29   * @see AsyncQueueFullPolicyFactory
30   * @see DefaultAsyncQueueFullPolicy
31   * @see DiscardingAsyncQueueFullPolicy
32   * @since 2.6
33   */
34  public enum EventRoute {
35      /**
36       * Enqueues the event for asynchronous logging in the background thread.
37       */
38      ENQUEUE {
39          @Override
40          public void logMessage(final AsyncLogger asyncLogger, final String fqcn, final Level level,
41                  final Marker marker, final Message message, final Throwable thrown) {
42          }
43  
44          @Override
45          public void logMessage(final AsyncLoggerConfig asyncLoggerConfig, final LogEvent event) {
46              asyncLoggerConfig.logInBackgroundThread(event);
47          }
48  
49          @Override
50          public void logMessage(final AsyncAppender asyncAppender, final LogEvent logEvent) {
51              asyncAppender.logMessageInBackgroundThread(logEvent);
52          }
53      },
54      /**
55       * Logs the event synchronously: sends the event directly to the appender (in the current thread).
56       * WARNING: This may result in lines logged out of order as synchronous events may be persisted before
57       * earlier events, even from the same thread, which wait in the queue.
58       */
59      SYNCHRONOUS {
60          @Override
61          public void logMessage(final AsyncLogger asyncLogger, final String fqcn, final Level level,
62                  final Marker marker, final Message message, final Throwable thrown) {
63          }
64  
65          @Override
66          public void logMessage(final AsyncLoggerConfig asyncLoggerConfig, final LogEvent event) {
67              asyncLoggerConfig.logToAsyncLoggerConfigsOnCurrentThread(event);
68          }
69  
70          @Override
71          public void logMessage(final AsyncAppender asyncAppender, final LogEvent logEvent) {
72              asyncAppender.logMessageInCurrentThread(logEvent);
73          }
74      },
75      /**
76       * Discards the event (so it is not logged at all).
77       */
78      DISCARD {
79          @Override
80          public void logMessage(final AsyncLogger asyncLogger, final String fqcn, final Level level,
81                  final Marker marker, final Message message, final Throwable thrown) {
82              // do nothing: drop the event
83          }
84  
85          @Override
86          public void logMessage(final AsyncLoggerConfig asyncLoggerConfig, final LogEvent event) {
87              // do nothing: drop the event
88          }
89  
90          @Override
91          public void logMessage(final AsyncAppender asyncAppender, final LogEvent coreEvent) {
92              // do nothing: drop the event
93          }
94      };
95  
96      public abstract void logMessage(final AsyncLogger asyncLogger, final String fqcn, final Level level,
97              final Marker marker, final Message message, final Throwable thrown);
98  
99      public abstract void logMessage(final AsyncLoggerConfig asyncLoggerConfig, final LogEvent event);
100 
101     public abstract void logMessage(final AsyncAppender asyncAppender, final LogEvent coreEvent);
102 }