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;
18  
19  import org.apache.logging.log4j.message.StructuredDataMessage;
20  import org.apache.logging.log4j.spi.AbstractLogger;
21  import org.apache.logging.log4j.spi.AbstractLoggerWrapper;
22  
23  /**
24   *  Logs "Events" that are represented as StructuredDataMessages.
25   */
26  public final class EventLogger {
27  
28      /**
29       * Define the Event Marker.
30       */
31      public static final Marker EVENT_MARKER = MarkerManager.getMarker("EVENT");
32  
33      private static final String FQCN = EventLogger.class.getName();
34  
35      private static AbstractLoggerWrapper logger;
36  
37      static {
38          Logger l = LogManager.getLogger("EventLogger");
39          if (!(l instanceof AbstractLogger)) {
40              throw new LoggingException("Logger returned must be based on AbstractLogger");
41          }
42          logger = new AbstractLoggerWrapper((AbstractLogger) l, "EventLogger");
43      }
44  
45  
46      private EventLogger() {
47      }
48  
49      /**
50       * Log events with a level of ALL.
51       * @param msg The event StructuredDataMessage.
52       */
53      public static void logEvent(StructuredDataMessage msg) {
54          logger.log(EVENT_MARKER, FQCN, Level.OFF, msg, null);
55      }
56  
57      /**
58       * Log events and specify the logging level.
59       * @param msg The event StructuredDataMessage.
60       * @param level The logging Level.
61       */
62      public static void logEvent(StructuredDataMessage msg, Level level) {
63          logger.log(EVENT_MARKER, FQCN, level, msg, null);
64      }
65  }