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.core.lookup; 018 019import org.apache.logging.log4j.core.LogEvent; 020import org.apache.logging.log4j.core.config.plugins.Plugin; 021 022/** 023 * Looks up values from the log event. 024 */ 025@Plugin(name = "event", category = StrLookup.CATEGORY) 026public class EventLookup extends AbstractLookup { 027 028 /** 029 * Looks up the value from the logging event. 030 * @param event The current LogEvent. 031 * @param key the key to be looked up. 032 * @return The value of the specified log event field. 033 */ 034 @Override 035 public String lookup(final LogEvent event, final String key) { 036 if (event == null) { 037 return null; 038 } 039 switch (key) { 040 case "Marker": { 041 return event.getMarker() != null ? event.getMarker().getName() : null; 042 } 043 case "ThreadName": { 044 return event.getThreadName(); 045 } 046 case "Level": { 047 return event.getLevel().toString(); 048 } 049 case "ThreadId": { 050 return Long.toString(event.getThreadId()); 051 } 052 case "Timestamp": { 053 return Long.toString(event.getTimeMillis()); 054 } 055 case "Exception": { 056 if (event.getThrown() != null) { 057 return event.getThrown().getClass().getSimpleName(); 058 } 059 if (event.getThrownProxy() != null) { 060 return event.getThrownProxy().getName(); 061 } 062 return null; 063 } 064 case "Logger": { 065 return event.getLoggerName(); 066 } 067 case "Message": { 068 return event.getMessage().getFormattedMessage(); 069 } 070 default: { 071 return null; 072 } 073 } 074 } 075}