001/* 002 * Licensed to the Apache Software Foundation (ASF) under one or more 003 * contributor license agreements. See the NOTICE file distributed with 004 * this work for additional information regarding copyright ownership. 005 * The ASF licenses this file to You under the Apache license, Version 2.0 006 * (the "License"); you may not use this file except in compliance with 007 * the License. You may obtain a copy of the License at 008 * 009 * http://www.apache.org/licenses/LICENSE-2.0 010 * 011 * Unless required by applicable law or agreed to in writing, software 012 * distributed under the License is distributed on an "AS IS" BASIS, 013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 014 * See the license for the specific language governing permissions and 015 * limitations under the license. 016 */ 017package org.apache.logging.log4j; 018 019import org.apache.logging.log4j.message.StructuredDataMessage; 020import org.apache.logging.log4j.spi.AbstractLogger; 021import org.apache.logging.log4j.spi.AbstractLoggerWrapper; 022 023/** 024 * Logs "Events" that are represented as StructuredDataMessages. 025 */ 026public final class EventLogger { 027 028 private static final String NAME = "EventLogger"; 029 030 /** 031 * Define the Event Marker. 032 */ 033 public static final Marker EVENT_MARKER = MarkerManager.getMarker("EVENT"); 034 035 private static final String FQCN = EventLogger.class.getName(); 036 037 private static AbstractLoggerWrapper loggerWrapper; 038 039 static { 040 final Logger eventLogger = LogManager.getLogger(NAME); 041 if (!(eventLogger instanceof AbstractLogger)) { 042 throw new LoggingException("Logger returned must be based on AbstractLogger"); 043 } 044 loggerWrapper = new AbstractLoggerWrapper((AbstractLogger) eventLogger, NAME, null); 045 } 046 047 048 private EventLogger() { 049 } 050 051 /** 052 * Log events with a level of ALL. 053 * @param msg The event StructuredDataMessage. 054 */ 055 public static void logEvent(final StructuredDataMessage msg) { 056 loggerWrapper.log(EVENT_MARKER, FQCN, Level.OFF, msg, null); 057 } 058 059 /** 060 * Log events and specify the logging level. 061 * @param msg The event StructuredDataMessage. 062 * @param level The logging Level. 063 */ 064 public static void logEvent(final StructuredDataMessage msg, final Level level) { 065 loggerWrapper.log(EVENT_MARKER, FQCN, level, msg, null); 066 } 067}