From 2be2d9881a4cc5a0c7a6d47dc5689b9c2285ebe1 Mon Sep 17 00:00:00 2001 From: Matt Sicker Date: Sun, 26 Jan 2014 23:28:26 -0600 Subject: [PATCH] Add @Loggable. --- .../main/java/org/apache/logging/log4j/Log.java | 85 ++++++++++++++++++++++ .../apache/logging/log4j/annotations/Loggable.java | 50 +++++++++++++ .../java/org/apache/logging/log4j/LogTest.java | 80 ++++++++++++++++++++ 3 files changed, 215 insertions(+) create mode 100644 log4j-api/src/main/java/org/apache/logging/log4j/Log.java create mode 100644 log4j-api/src/main/java/org/apache/logging/log4j/annotations/Loggable.java create mode 100644 log4j-api/src/test/java/org/apache/logging/log4j/LogTest.java diff --git a/log4j-api/src/main/java/org/apache/logging/log4j/Log.java b/log4j-api/src/main/java/org/apache/logging/log4j/Log.java new file mode 100644 index 0000000..d3fd025 --- /dev/null +++ b/log4j-api/src/main/java/org/apache/logging/log4j/Log.java @@ -0,0 +1,85 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.logging.log4j; + +import org.apache.logging.log4j.annotations.Loggable; +import org.apache.logging.log4j.status.StatusLogger; + +/** + * Provides a convenient way of getting a logger for a class. Combined with the {@code @Loggable} annotation, instead + * of storing a Logger in every class, {@link #get()} can be called inside methods to automagically get the proper + * logger for that class. Alternatively, this also provides a convenient way to choose the right logger based on the + * class's annotation. Thus, you can still use: + *
+ *     {@code private static final Logger LOGGER = Log.get();}
+ * 
+ */ +public final class Log { + + private static final Logger LOGGER = StatusLogger.getLogger(); + + private Log() {} + + /** + * Gets the logger configured for the calling class. + * + * @return logger for the calling class + */ + public static Logger get() { + final Class caller = getCallingClass(); + final Loggable loggable = getLoggable(caller); + return getLogger(loggable, caller); + } + + private static Loggable getLoggable(final Class clazz) { + return clazz.getAnnotation(Loggable.class); + } + + private static Class getCallingClass() { + final String className = new Throwable().getStackTrace()[2].getClassName(); + LOGGER.debug(className); + try { + return Class.forName(className); + } catch (ClassNotFoundException e) { + // Didn't we just get this class from the call stack? How could we not load it, too? + LOGGER.fatal("What sorcery is this?", e); + return null; + } + } + + private static Logger getLogger(final Loggable loggable, final Class clazz) { + if (loggable == null) { + return LogManager.getLogger(clazz); + } + + if (loggable.root()) { + return LogManager.getRootLogger(); + } + + if (loggable.name() != null && !loggable.name().isEmpty()) { + return LogManager.getLogger(loggable.name()); + } + + if (loggable.value() == Void.class) { + return LogManager.getLogger(clazz); + } + + return LogManager.getLogger(loggable.value()); + } + +} diff --git a/log4j-api/src/main/java/org/apache/logging/log4j/annotations/Loggable.java b/log4j-api/src/main/java/org/apache/logging/log4j/annotations/Loggable.java new file mode 100644 index 0000000..42f9304 --- /dev/null +++ b/log4j-api/src/main/java/org/apache/logging/log4j/annotations/Loggable.java @@ -0,0 +1,50 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.logging.log4j.annotations; + +import java.lang.annotation.Documented; +import java.lang.annotation.Retention; +import java.lang.annotation.Target; + +import static java.lang.annotation.ElementType.TYPE; +import static java.lang.annotation.RetentionPolicy.RUNTIME; + +/** + * Annotation to configure a class's Logger. Said Logger is retrievable using {@link org.apache.logging.log4j.Log#get()}. + */ +@Documented +@Retention(RUNTIME) +@Target(TYPE) +public @interface Loggable { + + /** + * Class to derive logger name from. If this value is not changed from the default, then the logger name will be + * derived from either the {@link #name()} or the caller class to {@link org.apache.logging.log4j.Log#get()}. + */ + Class value() default Void.class; + + /** + * Logger name to use. Overrides the class from {@link #value()} if {@code value} is still the default class. + */ + String name() default ""; + + /** + * Indicates whether the root logger should be used instead of a named logger. + */ + boolean root() default false; +} diff --git a/log4j-api/src/test/java/org/apache/logging/log4j/LogTest.java b/log4j-api/src/test/java/org/apache/logging/log4j/LogTest.java new file mode 100644 index 0000000..e52c12d --- /dev/null +++ b/log4j-api/src/test/java/org/apache/logging/log4j/LogTest.java @@ -0,0 +1,80 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.logging.log4j; + +import org.apache.logging.log4j.annotations.Loggable; +import org.junit.Before; +import org.junit.Test; + +import static org.hamcrest.CoreMatchers.equalTo; +import static org.hamcrest.CoreMatchers.is; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertThat; + +@Loggable +public class LogTest { + + private TestLogger logger; + + @Before + public void setUp() throws Exception { + logger = (TestLogger) Log.get(); + } + + @Test + public void testLogGetNamedAfterThisClass() throws Exception { + assertThat(Log.get().getName(), is(equalTo(LogTest.class.getName()))); + } + + @Test + public void testLog() throws Exception { + Log.get().warn("This is a test warning message."); + assertFalse(logger.getEntries().isEmpty()); + } + + @Test + public void testNamedLog() throws Exception { + final InnerTest test = new InnerTest(); + test.log("Test message."); + final TestLogger innerLogger = (TestLogger) LogManager.getLogger("com.example.TestLogger"); + assertFalse(innerLogger.getEntries().isEmpty()); + } + + @Test + public void testClassLog() throws Exception { + final ClassTest test = new ClassTest(); + test.mention("Richard Stallman"); + final TestLogger testLogger = (TestLogger) LogManager.getLogger(Test.class); + assertThat(testLogger.getName(), is(equalTo(Test.class.getName()))); + assertFalse(testLogger.getEntries().isEmpty()); + } + + @Loggable(name = "com.example.TestLogger") + public static class InnerTest { + public void log(final String message) { + Log.get().info(message); + } + } + + @Loggable(Test.class) + public static class ClassTest { + public void mention(final String person) { + Log.get().warn("Look out for " + person + '!'); + } + } +} -- 1.8.5.3