Repository: logging-log4j2 Updated Branches: refs/heads/master c6663ddaa -> 3fdd73936
Replace some patterns like "foo '" + str + '\'' with "foo " + Strings.quote(str). Use Chars constants too. Project: http://git-wip-us.apache.org/repos/asf/logging-log4j2/repo Commit: http://git-wip-us.apache.org/repos/asf/logging-log4j2/commit/3fdd7393 Tree: http://git-wip-us.apache.org/repos/asf/logging-log4j2/tree/3fdd7393 Diff: http://git-wip-us.apache.org/repos/asf/logging-log4j2/diff/3fdd7393 Branch: refs/heads/master Commit: 3fdd7393646aa6df12d61f9d202b80e47faff815 Parents: c6663dd Author: Gary Gregory <[email protected]> Authored: Thu Mar 5 08:46:34 2015 -0800 Committer: Gary Gregory <[email protected]> Committed: Thu Mar 5 08:46:34 2015 -0800 ---------------------------------------------------------------------- .../org/apache/logging/log4j/LevelLogger.java | 442 +++++++++++++++++++ .../logging/log4j/message/StructuredDataId.java | 4 +- .../org/apache/logging/log4j/util/Strings.java | 64 ++- .../apache/logging/log4j/util/StringsTest.java | 30 ++ .../logging/log4j/core/lookup/StrMatcher.java | 9 +- .../org/apache/logging/log4j/web/WebLookup.java | 3 +- 6 files changed, 527 insertions(+), 25 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/3fdd7393/log4j-api/src/main/java/org/apache/logging/log4j/LevelLogger.java ---------------------------------------------------------------------- diff --git a/log4j-api/src/main/java/org/apache/logging/log4j/LevelLogger.java b/log4j-api/src/main/java/org/apache/logging/log4j/LevelLogger.java new file mode 100644 index 0000000..e1c1cea --- /dev/null +++ b/log4j-api/src/main/java/org/apache/logging/log4j/LevelLogger.java @@ -0,0 +1,442 @@ +/* + * 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.message.Message; +import org.apache.logging.log4j.message.MessageFactory; + +/** + * This is the central interface in the log4j package. Most logging operations, except configuration, are done through + * this interface. + * + * <p> + * The canonical way to obtain a Logger for a class is through {@link LogManager#getLogger()}. Typically, each class + * gets its own Logger named after its fully qualified class name (the default Logger name when obtained through the + * {@link LogManager#getLogger()} method). Thus, the simplest way to use this would be like so: + * </p> + * + * <pre> + * public class MyClass { + * private static final Logger LOGGER = LogManager.getLogger(); + * // ... + * } + * </pre> + * <p> + * For ease of filtering, searching, sorting, etc., it is generally a good idea to create Loggers for each class rather + * than sharing Loggers. Instead, {@link Marker Markers} should be used for shared, filterable identification. + * </p> + * <p> + * For service provider implementations, it is recommended to extend the + * {@link org.apache.logging.log4j.spi.AbstractLogger} class rather than implementing this interface directly. + * </p> + */ +public interface LevelLogger { + + /** + * Logs an exception or error that has been caught. Normally, one may wish to provide additional information with an + * exception while logging it; in these cases, one would not use this method. In other cases where simply logging + * the fact that an exception was swallowed somewhere (e.g., at the top of the stack trace in a {@code main()} + * method), this method is ideal for it. + * + * @param t + * The Throwable. + */ + void catching(Throwable t); + + /** + * Logs entry to a method. Used when the method in question has no parameters or when the parameters should not be + * logged. + */ + void entry(); + + /** + * Logs entry to a method along with its parameters. For example, + * + * <pre> + * public void doSomething(String foo, int bar) { + * LOGGER.entry(foo, bar); + * // do something + * } + * </pre> + * <p> + * The use of methods such as this are more effective when combined with aspect-oriented programming or other + * bytecode manipulation tools. It can be rather tedious (and messy) to use this type of method manually. + * </p> + * + * @param params + * The parameters to the method. TODO Use of varargs results in array creation which can be a substantial + * portion of no-op case. LogMF/LogSF provides several overrides to avoid vararg except in edge cases. (RG) + * LogMF and LogSF implement these in LogXF which calls logger.callAppenders. callAppenders is part of the + * implementation and cannot be used by the API. Adding more methods here and in AbstractLogger is + * sufficient. + */ + void entry(Object... params); + + /** + * Logs exit from a method. Used for methods that do not return anything. + */ + void exit(); + + /** + * Logs exiting from a method with the result. This may be coded as: + * + * <pre> + * return LOGGER.exit(myResult); + * </pre> + * + * @param <R> + * The type of the parameter and object being returned. + * @param result + * The result being returned from the method call. + * @return the result. + */ + <R> R exit(R result); + + /** + * Gets the Level associated with the Logger. + * + * @return the Level associate with the Logger. + */ + Level getLevel(); + + /** + * Gets the message factory used to convert message Objects and Strings into actual log Messages. + * + * @return the message factory. + */ + MessageFactory getMessageFactory(); + + /** + * Gets the logger name. + * + * @return the logger name. + */ + String getName(); + + /** + * Checks whether this Logger is enabled for the {@link Level#DEBUG DEBUG} Level. + * + * @return boolean - {@code true} if this Logger is enabled for level DEBUG, {@code false} otherwise. + */ + boolean isDebugEnabled(); + + /** + * Checks whether this Logger is enabled for the {@link Level#DEBUG DEBUG} Level. + * + * @param marker + * The marker data specific to this log statement. + * @return boolean - {@code true} if this Logger is enabled for level DEBUG, {@code false} otherwise. + */ + boolean isDebugEnabled(Marker marker); + + /** + * Checks whether this Logger is enabled for the the given Level. + * <p> + * Note that passing in {@link Level#OFF OFF} always returns {@code true}. + * </p> + * + * @return boolean - {@code true} if this Logger is enabled for level, {@code false} otherwise. + */ + boolean isEnabled(Level level); + + /** + * Checks whether this logger is enabled at the specified level and an optional Marker. + * + * @param marker + * The marker data specific to this log statement. + * @return boolean - {@code true} if this Logger is enabled for level {@link Level#WARN WARN}, {@code false} + * otherwise. + */ + boolean isEnabled(Marker marker); + + /** + * Checks whether this Logger is enabled for the {@link Level#ERROR ERROR} Level. + * + * @return boolean - {@code true} if this Logger is enabled for level {@link Level#ERROR ERROR}, {@code false} + * otherwise. + */ + boolean isErrorEnabled(); + + /** + * Checks whether this Logger is enabled for the {@link Level#ERROR ERROR} Level. + * + * @param marker + * The marker data specific to this log statement. + * @return boolean - {@code true} if this Logger is enabled for level {@link Level#ERROR ERROR}, {@code false} + * otherwise. + */ + boolean isErrorEnabled(Marker marker); + + /** + * Checks whether this Logger is enabled for the {@link Level#FATAL FATAL} Level. + * + * @return boolean - {@code true} if this Logger is enabled for level {@link Level#FATAL FATAL}, {@code false} + * otherwise. + */ + boolean isFatalEnabled(); + + /** + * Checks whether this Logger is enabled for the {@link Level#FATAL FATAL} Level. + * + * @param marker + * The marker data specific to this log statement. + * @return boolean - {@code true} if this Logger is enabled for level {@link Level#FATAL FATAL}, {@code false} + * otherwise. + */ + boolean isFatalEnabled(Marker marker); + + /** + * Checks whether this Logger is enabled for the {@link Level#INFO INFO} Level. + * + * @return boolean - {@code true} if this Logger is enabled for level INFO, {@code false} otherwise. + */ + boolean isInfoEnabled(); + + /** + * Checks whether this Logger is enabled for the {@link Level#INFO INFO} Level. + * + * @param marker + * The marker data specific to this log statement. + * @return boolean - {@code true} if this Logger is enabled for level INFO, {@code false} otherwise. + */ + boolean isInfoEnabled(Marker marker); + + /** + * Checks whether this Logger is enabled for the {@link Level#TRACE TRACE} level. + * + * @return boolean - {@code true} if this Logger is enabled for level TRACE, {@code false} otherwise. + */ + boolean isTraceEnabled(); + + /** + * Checks whether this Logger is enabled for the {@link Level#TRACE TRACE} level. + * + * @param marker + * The marker data specific to this log statement. + * @return boolean - {@code true} if this Logger is enabled for level TRACE, {@code false} otherwise. + */ + boolean isTraceEnabled(Marker marker); + + /** + * Checks whether this Logger is enabled for the {@link Level#WARN WARN} Level. + * + * @return boolean - {@code true} if this Logger is enabled for level {@link Level#WARN WARN}, {@code false} + * otherwise. + */ + boolean isWarnEnabled(); + + /** + * Checks whether this Logger is enabled for the {@link Level#WARN WARN} Level. + * + * @param marker + * The marker data specific to this log statement. + * @return boolean - {@code true} if this Logger is enabled for level {@link Level#WARN WARN}, {@code false} + * otherwise. + */ + boolean isWarnEnabled(Marker marker); + + /** + * Logs a message with the specific Marker at the given level. + * + * + * @param marker + * the marker data specific to this log statement + * @param msg + * the message string to be logged + */ + void log(Marker marker, Message msg); + + /** + * Logs a message with the specific Marker at the given level. + * + * @param marker + * the marker data specific to this log statement + * @param msg + * the message string to be logged + * @param t + * A Throwable or null. + */ + void log(Marker marker, Message msg, Throwable t); + + /** + * Logs a message object with the given level. + * + * @param marker + * the marker data specific to this log statement + * @param message + * the message object to log. + */ + void log(Marker marker, Object message); + + /** + * Logs a message at the given level including the stack trace of the {@link Throwable} <code>t</code> passed as + * parameter. + * + * @param marker + * the marker data specific to this log statement + * @param message + * the message to log. + * @param t + * the exception to log, including its stack trace. + */ + void log(Marker marker, Object message, Throwable t); + + /** + * Logs a message object with the given level. + * + * + * @param marker + * the marker data specific to this log statement + * @param message + * the message object to log. + */ + void log(Marker marker, String message); + + /** + * Logs a message with parameters at the given level. + * + * @param marker + * the marker data specific to this log statement + * @param message + * the message to log; the format depends on the message factory. + * @param params + * parameters to the message. + * @see #getMessageFactory() + */ + void log(Marker marker, String message, Object... params); + + /** + * Logs a message at the given level including the stack trace of the {@link Throwable} <code>t</code> passed as + * parameter. + * + * @param marker + * the marker data specific to this log statement + * @param message + * the message to log. + * @param t + * the exception to log, including its stack trace. + */ + void log(Marker marker, String message, Throwable t); + + /** + * Logs a message with the specific Marker at the given level. + * + * @param msg + * the message string to be logged + */ + void log(Message msg); + + /** + * Logs a message with the specific Marker at the given level. + * + * @param msg + * the message string to be logged + * @param t + * A Throwable or null. + */ + void log(Message msg, Throwable t); + + /** + * Logs a message object with the given level. + * + * @param message + * the message object to log. + */ + void log(Object message); + + /** + * Logs a message at the given level including the stack trace of the {@link Throwable} <code>t</code> passed as + * parameter. + * + * @param message + * the message to log. + * @param t + * the exception to log, including its stack trace. + */ + void log(Object message, Throwable t); + + /** + * Logs a message object with the given level. + * + * @param message + * the message string to log. + */ + void log(String message); + + /** + * Logs a message with parameters at the given level. + * + * + * @param message + * the message to log; the format depends on the message factory. + * @param params + * parameters to the message. + * @see #getMessageFactory() + */ + void log(String message, Object... params); + + /** + * Logs a message at the given level including the stack trace of the {@link Throwable} <code>t</code> passed as + * parameter. + * + * + * @param message + * the message to log. + * @param t + * the exception to log, including its stack trace. + */ + void log(String message, Throwable t); + + /** + * Logs a formatted message using the specified format string and arguments. + * + * + * @param marker + * the marker data specific to this log statement. + * @param format + * The format String. + * @param params + * Arguments specified by the format. + */ + void printf(Marker marker, String format, Object... params); + + /** + * Logs a formatted message using the specified format string and arguments. + * + * + * @param format + * The format String. + * @param params + * Arguments specified by the format. + */ + void printf(String format, Object... params); + + /** + * Logs an exception or error to be thrown. This may be coded as: + * + * <pre> + * throw logger.throwing(myException); + * </pre> + * + * @param <T> + * the Throwable type. + * @param t + * The Throwable. + * @return the Throwable. + */ + <T extends Throwable> T throwing(T t); +} http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/3fdd7393/log4j-api/src/main/java/org/apache/logging/log4j/message/StructuredDataId.java ---------------------------------------------------------------------- diff --git a/log4j-api/src/main/java/org/apache/logging/log4j/message/StructuredDataId.java b/log4j-api/src/main/java/org/apache/logging/log4j/message/StructuredDataId.java index 33755a2..a200b57 100644 --- a/log4j-api/src/main/java/org/apache/logging/log4j/message/StructuredDataId.java +++ b/log4j-api/src/main/java/org/apache/logging/log4j/message/StructuredDataId.java @@ -18,6 +18,8 @@ package org.apache.logging.log4j.message; import java.io.Serializable; +import org.apache.logging.log4j.util.Strings; + /** * The StructuredData identifier. */ @@ -92,7 +94,7 @@ public class StructuredDataId implements Serializable { throw new IllegalArgumentException("No structured id name was supplied"); } if (name.contains(AT)) { - throw new IllegalArgumentException("Structured id name cannot contain an '" + AT + '\''); + throw new IllegalArgumentException("Structured id name cannot contain an " + Strings.quote(AT)); } if (enterpriseNumber <= 0) { throw new IllegalArgumentException("No enterprise number was supplied"); http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/3fdd7393/log4j-api/src/main/java/org/apache/logging/log4j/util/Strings.java ---------------------------------------------------------------------- diff --git a/log4j-api/src/main/java/org/apache/logging/log4j/util/Strings.java b/log4j-api/src/main/java/org/apache/logging/log4j/util/Strings.java index 5e8c9a5..640df36 100644 --- a/log4j-api/src/main/java/org/apache/logging/log4j/util/Strings.java +++ b/log4j-api/src/main/java/org/apache/logging/log4j/util/Strings.java @@ -18,6 +18,7 @@ package org.apache.logging.log4j.util; /** * <em>Consider this class private.</em> + * * @see <a href="http://commons.apache.org/proper/commons-lang/">Apache Commons Lang</a> */ public final class Strings { @@ -31,7 +32,9 @@ public final class Strings { } /** - * <p>Checks if a CharSequence is empty ("") or null.</p> + * <p> + * Checks if a CharSequence is empty ("") or null. + * </p> * * <pre> * Strings.isEmpty(null) = true @@ -41,13 +44,17 @@ public final class Strings { * Strings.isEmpty(" bob ") = false * </pre> * - * <p>NOTE: This method changed in Lang version 2.0. - * It no longer trims the CharSequence. - * That functionality is available in isBlank().</p> + * <p> + * NOTE: This method changed in Lang version 2.0. It no longer trims the CharSequence. That functionality is + * available in isBlank(). + * </p> * - * <p>Copied from Apache Commons Lang org.apache.commons.lang3.StringUtils.isEmpty(CharSequence)</p> + * <p> + * Copied from Apache Commons Lang org.apache.commons.lang3.StringUtils.isEmpty(CharSequence) + * </p> * - * @param cs the CharSequence to check, may be null + * @param cs + * the CharSequence to check, may be null * @return {@code true} if the CharSequence is empty or null */ public static boolean isEmpty(final CharSequence cs) { @@ -55,7 +62,9 @@ public final class Strings { } /** - * <p>Checks if a CharSequence is not empty ("") and not null.</p> + * <p> + * Checks if a CharSequence is not empty ("") and not null. + * </p> * * <pre> * Strings.isNotEmpty(null) = false @@ -65,9 +74,12 @@ public final class Strings { * Strings.isNotEmpty(" bob ") = true * </pre> * - * <p>Copied from Apache Commons Lang org.apache.commons.lang3.StringUtils.isNotEmpty(CharSequence)</p> + * <p> + * Copied from Apache Commons Lang org.apache.commons.lang3.StringUtils.isNotEmpty(CharSequence) + * </p> * - * @param cs the CharSequence to check, may be null + * @param cs + * the CharSequence to check, may be null * @return {@code true} if the CharSequence is not empty and not null */ public static boolean isNotEmpty(final CharSequence cs) { @@ -78,7 +90,8 @@ public final class Strings { * Checks if a String is blank. A blank string is one that is {@code null}, empty, or when trimmed using * {@link String#trim()} is empty. * - * @param s the String to check, may be {@code null} + * @param s + * the String to check, may be {@code null} * @return {@code true} if the String is {@code null}, empty, or trims to empty. */ public static boolean isBlank(final String s) { @@ -88,7 +101,8 @@ public final class Strings { /** * Checks if a String is not blank. The opposite of {@link #isBlank(String)}. * - * @param s the String to check, may be {@code null} + * @param s + * the String to check, may be {@code null} * @return {@code true} if the String is non-{@code null} and has content after being trimmed. */ public static boolean isNotBlank(final String s) { @@ -96,12 +110,13 @@ public final class Strings { } /** - * <p>Removes control characters (char <= 32) from both - * ends of this String returning {@code null} if the String is + * <p> + * Removes control characters (char <= 32) from both ends of this String returning {@code null} if the String is * empty ("") after the trim or if it is {@code null}. * - * <p>The String is trimmed using {@link String#trim()}. - * Trim removes start and end characters <= 32.</p> + * <p> + * The String is trimmed using {@link String#trim()}. Trim removes start and end characters <= 32. + * </p> * * <pre> * Strings.trimToNull(null) = null @@ -111,15 +126,26 @@ public final class Strings { * Strings.trimToNull(" abc ") = "abc" * </pre> * - * <p>Copied from Apache Commons Lang org.apache.commons.lang3.StringUtils.trimToNull(String)</p> + * <p> + * Copied from Apache Commons Lang org.apache.commons.lang3.StringUtils.trimToNull(String) + * </p> * - * @param str the String to be trimmed, may be null - * @return the trimmed String, - * {@code null} if only chars <= 32, empty or null String input + * @param str + * the String to be trimmed, may be null + * @return the trimmed String, {@code null} if only chars <= 32, empty or null String input */ public static String trimToNull(final String str) { final String ts = str == null ? null : str.trim(); return isEmpty(ts) ? null : ts; } + /** + * Returns a quoted string. + * + * @param str + * @return {@code 'str'} + */ + public static String quote(final String str) { + return Chars.QUOTE + str + Chars.QUOTE; + } } http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/3fdd7393/log4j-api/src/test/java/org/apache/logging/log4j/util/StringsTest.java ---------------------------------------------------------------------- diff --git a/log4j-api/src/test/java/org/apache/logging/log4j/util/StringsTest.java b/log4j-api/src/test/java/org/apache/logging/log4j/util/StringsTest.java new file mode 100644 index 0000000..fc120e7 --- /dev/null +++ b/log4j-api/src/test/java/org/apache/logging/log4j/util/StringsTest.java @@ -0,0 +1,30 @@ +/* + * 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.util; + +import org.junit.Assert; +import org.junit.Test; + +public class StringsTest { + + @Test + public void testQuote() { + Assert.assertEquals("'Q'", Strings.quote("Q")); + } + +} http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/3fdd7393/log4j-core/src/main/java/org/apache/logging/log4j/core/lookup/StrMatcher.java ---------------------------------------------------------------------- diff --git a/log4j-core/src/main/java/org/apache/logging/log4j/core/lookup/StrMatcher.java b/log4j-core/src/main/java/org/apache/logging/log4j/core/lookup/StrMatcher.java index 13fb40c..5d18888 100644 --- a/log4j-core/src/main/java/org/apache/logging/log4j/core/lookup/StrMatcher.java +++ b/log4j-core/src/main/java/org/apache/logging/log4j/core/lookup/StrMatcher.java @@ -18,6 +18,7 @@ package org.apache.logging.log4j.core.lookup; import java.util.Arrays; +import org.apache.logging.log4j.util.Chars; import org.apache.logging.log4j.util.Strings; /** @@ -36,11 +37,11 @@ public abstract class StrMatcher { /** * Matches the tab character. */ - private static final StrMatcher TAB_MATCHER = new CharMatcher('\t'); + private static final StrMatcher TAB_MATCHER = new CharMatcher(Chars.TAB); /** * Matches the space character. */ - private static final StrMatcher SPACE_MATCHER = new CharMatcher(' '); + private static final StrMatcher SPACE_MATCHER = new CharMatcher(Chars.SPACE); /** * Matches the same characters as StringTokenizer, * namely space, tab, newline, formfeed. @@ -53,11 +54,11 @@ public abstract class StrMatcher { /** * Matches the double quote character. */ - private static final StrMatcher SINGLE_QUOTE_MATCHER = new CharMatcher('\''); + private static final StrMatcher SINGLE_QUOTE_MATCHER = new CharMatcher(Chars.QUOTE); /** * Matches the double quote character. */ - private static final StrMatcher DOUBLE_QUOTE_MATCHER = new CharMatcher('"'); + private static final StrMatcher DOUBLE_QUOTE_MATCHER = new CharMatcher(Chars.DQUOTE); /** * Matches the single or double quote character. */ http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/3fdd7393/log4j-web/src/main/java/org/apache/logging/log4j/web/WebLookup.java ---------------------------------------------------------------------- diff --git a/log4j-web/src/main/java/org/apache/logging/log4j/web/WebLookup.java b/log4j-web/src/main/java/org/apache/logging/log4j/web/WebLookup.java index fb90cdd..072f6c9 100644 --- a/log4j-web/src/main/java/org/apache/logging/log4j/web/WebLookup.java +++ b/log4j-web/src/main/java/org/apache/logging/log4j/web/WebLookup.java @@ -23,6 +23,7 @@ import javax.servlet.ServletContext; import org.apache.logging.log4j.core.LogEvent; import org.apache.logging.log4j.core.config.plugins.Plugin; import org.apache.logging.log4j.core.lookup.AbstractLookup; +import org.apache.logging.log4j.util.Strings; @Plugin(name = "web", category = "Lookup") public class WebLookup extends AbstractLookup { @@ -102,7 +103,7 @@ public class WebLookup extends AbstractLookup { return ctx.getInitParameter(key); } - ctx.log(getClass().getName() + " unable to resolve key '" + key + '\''); + ctx.log(getClass().getName() + " unable to resolve key " + Strings.quote(key)); return null; } }
