[
https://issues.apache.org/jira/browse/LOG4J2-519?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=13922497#comment-13922497
]
Bruce Brouwer commented on LOG4J2-519:
--------------------------------------
With my work in LOG4J-547, this example would turn into something like this:
{code}
package com.bhb.log4j;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
import org.apache.logging.log4j.Level;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.apache.logging.log4j.Marker;
import org.apache.logging.log4j.message.Message;
import org.apache.logging.log4j.message.MessageFactory;
import org.apache.logging.log4j.simple.SimpleLogger;
import org.apache.logging.log4j.spi.AbstractLogger;
import org.apache.logging.log4j.spi.LoggerExtension;
public final class ExtLogger extends AbstractLogger {
private static final long serialVersionUID = 26876940739430L;
private static final ConcurrentMap<String, ExtLogger> loggers = new
ConcurrentHashMap<String, ExtLogger>();
private final LoggerExtension logger;
private static final String FQCN = ExtLogger.class.getName();
private static final Level DIAG = Level.forName("DIAG", 350);
private static final Level NOTICE = Level.forName("NOTICE", 450);
private static final Level VERBOSE = Level.forName("VERBOSE", 550);
private ExtLogger(LoggerExtension logger) {
super(logger.getName());
this.logger = logger;
}
/**
* Returns a custom Logger with the name of the calling class.
*
* @return The custom Logger for the calling class.
*/
public static ExtLogger getLogger() {
return getLogger(getClassName(2));
}
/**
* Returns a custom Logger using the fully qualified name of the Class as
the Logger name.
*
* @param loggerName The Class whose name should be used as the Logger
name. If null it will
* default to the calling class.
* @return The custom Logger.
*/
public static ExtLogger getLogger(final Class<?> clazz) {
return getLogger(clazz != null ? clazz.getName() : getClassName(2));
}
/**
* Returns a custom Logger using the fully qualified name of the Class as
the Logger name.
*
* @param loggerName The Class whose name should be used as the Logger
name. If null it will
* default to the calling class.
* @param messageFactory The message factory is used only when creating a
logger, subsequent use
* does not change the logger but will log a warning if
mismatched.
* @return The custom Logger.
*/
public static ExtLogger getLogger(final Class<?> clazz, final
MessageFactory factory) {
return getLogger(clazz != null ? clazz.getName() : getClassName(2),
factory);
}
/**
* Returns a custom Logger using the fully qualified class name of the
value as the Logger name.
*
* @param value The value whose class name should be used as the Logger
name. If null the name
* of the calling class will be used as the logger name.
* @return The custom Logger.
*/
public static ExtLogger getLogger(final Object value) {
return getLogger(value != null ? value.getClass().getName() :
getClassName(2));
}
/**
* Returns a custom Logger using the fully qualified class name of the
value as the Logger name.
*
* @param value The value whose class name should be used as the Logger
name. If null the name
* of the calling class will be used as the logger name.
* @param messageFactory The message factory is used only when creating a
logger, subsequent use
* does not change the logger but will log a warning if
mismatched.
* @return The custom Logger.
*/
public static ExtLogger getLogger(final Object value, final MessageFactory
factory) {
return getLogger(value != null ? value.getClass().getName() :
getClassName(2), factory);
}
/**
* Returns a custom Logger with the specified name.
*
* @param name The logger name. If null the name of the calling class will
be used.
* @return The custom Logger.
*/
public static ExtLogger getLogger(final String name) {
return getLogger(name != null ? name : getClassName(2), null);
}
/**
* Returns a custom Logger with the specified name.
*
* @param name The logger name. If null the name of the calling class will
be used.
* @param messageFactory The message factory is used only when creating a
logger, subsequent use
* does not change the logger but will log a warning if
mismatched.
* @return The custom Logger.
*/
public static ExtLogger getLogger(final String name, final MessageFactory
factory) {
final String actualName = name != null ? name : getClassName(2);
if (loggers.containsKey(actualName)) {
return loggers.get(actualName);
}
// Since both ExtLogger and AbstractLogger methods will be entry
points, both are listed for FQCN to make caller location work
final ExtLogger result = new
ExtLogger(LogManager.getContext().extendLogger(actualName, factory,
ExtLogger.class, AbstractLogger.class));
final ExtLogger existing = loggers.putIfAbsent(actualName, result);
return existing == null ? result : existing;
}
/**
* Gets the class name of the caller in the current stack at the given
{@code depth}.
*
* @param depth a 0-based index in the current stack.
* @return a class name
*/
private static String getClassName(final int depth) {
return new Throwable().getStackTrace()[depth].getClassName();
}
/**
* Logs a message with the specific Marker at the {@code DIAG} level.
*
* @param marker the marker data specific to this log statement
* @param msg the message string to be logged
*/
public void diag(final Marker marker, final Message msg) {
logger.log(DIAG, marker, msg, null);
}
/**
* Logs a message with the specific Marker at the {@code DIAG} 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.
*/
public void diag(final Marker marker, final Message msg, final Throwable t)
{
logger.log(DIAG, marker, msg, t);
}
/**
* Logs a message object with the {@code DIAG} level.
*
* @param marker the marker data specific to this log statement
* @param message the message object to log.
*/
public void diag(final Marker marker, final Object message) {
logger.log(DIAG, marker, message, null);
}
/**
* Logs a message at the {@code DIAG} level including the stack trace of
the {@link Throwable}
* {@code t} 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.
*/
public void diag(final Marker marker, final Object message, final Throwable
t) {
logger.log(DIAG, marker, message, t);
}
/**
* Logs a message object with the {@code DIAG} level.
*
* @param marker the marker data specific to this log statement
* @param message the message object to log.
*/
public void diag(final Marker marker, final String message) {
logger.log(DIAG, marker, message, (Throwable) null);
}
/**
* Logs a message with parameters at the {@code DIAG} 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()
*/
public void diag(final Marker marker, final String message, final Object...
params) {
logger.log(DIAG, marker, message, params);
}
/**
* Logs a message at the {@code DIAG} level including the stack trace of
the {@link Throwable}
* {@code t} 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.
*/
public void diag(final Marker marker, final String message, final Throwable
t) {
logger.log(DIAG, marker, message, t);
}
/**
* Logs the specified Message at the {@code DIAG} level.
*
* @param msg the message string to be logged
*/
public void diag(final Message msg) {
logger.log(DIAG, null, msg, null);
}
/**
* Logs the specified Message at the {@code DIAG} level.
*
* @param msg the message string to be logged
* @param t A Throwable or null.
*/
public void diag(final Message msg, final Throwable t) {
logger.log(DIAG, null, msg, t);
}
/**
* Logs a message object with the {@code DIAG} level.
*
* @param message the message object to log.
*/
public void diag(final Object message) {
logger.log(DIAG, null, message, null);
}
/**
* Logs a message at the {@code DIAG} level including the stack trace of
the {@link Throwable}
* {@code t} passed as parameter.
*
* @param message the message to log.
* @param t the exception to log, including its stack trace.
*/
public void diag(final Object message, final Throwable t) {
logger.log(DIAG, null, message, t);
}
/**
* Logs a message object with the {@code DIAG} level.
*
* @param message the message object to log.
*/
public void diag(final String message) {
logger.log(DIAG, null, message, (Throwable) null);
}
/**
* Logs a message with parameters at the {@code DIAG} level.
*
* @param message the message to log; the format depends on the message
factory.
* @param params parameters to the message.
* @see #getMessageFactory()
*/
public void diag(final String message, final Object... params) {
logger.log(DIAG, null, message, params);
}
/**
* Logs a message at the {@code DIAG} level including the stack trace of
the {@link Throwable}
* {@code t} passed as parameter.
*
* @param message the message to log.
* @param t the exception to log, including its stack trace.
*/
public void diag(final String message, final Throwable t) {
logger.log(DIAG, null, message, t);
}
/**
* Logs a message with the specific Marker at the {@code NOTICE} level.
*
* @param marker the marker data specific to this log statement
* @param msg the message string to be logged
*/
public void notice(final Marker marker, final Message msg) {
logger.log(NOTICE, marker, msg, null);
}
/**
* Logs a message with the specific Marker at the {@code NOTICE} 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.
*/
public void notice(final Marker marker, final Message msg, final Throwable
t) {
logger.log(NOTICE, marker, msg, t);
}
/**
* Logs a message object with the {@code NOTICE} level.
*
* @param marker the marker data specific to this log statement
* @param message the message object to log.
*/
public void notice(final Marker marker, final Object message) {
logger.log(NOTICE, marker, message, null);
}
/**
* Logs a message at the {@code NOTICE} level including the stack trace of
the {@link Throwable}
* {@code t} 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.
*/
public void notice(final Marker marker, final Object message, final
Throwable t) {
logger.log(NOTICE, marker, message, t);
}
/**
* Logs a message object with the {@code NOTICE} level.
*
* @param marker the marker data specific to this log statement
* @param message the message object to log.
*/
public void notice(final Marker marker, final String message) {
logger.log(NOTICE, marker, message, (Throwable) null);
}
/**
* Logs a message with parameters at the {@code NOTICE} 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()
*/
public void notice(final Marker marker, final String message, final
Object... params) {
logger.log(NOTICE, marker, message, params);
}
/**
* Logs a message at the {@code NOTICE} level including the stack trace of
the {@link Throwable}
* {@code t} 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.
*/
public void notice(final Marker marker, final String message, final
Throwable t) {
logger.log(NOTICE, marker, message, t);
}
/**
* Logs the specified Message at the {@code NOTICE} level.
*
* @param msg the message string to be logged
*/
public void notice(final Message msg) {
logger.log(NOTICE, null, msg, null);
}
/**
* Logs the specified Message at the {@code NOTICE} level.
*
* @param msg the message string to be logged
* @param t A Throwable or null.
*/
public void notice(final Message msg, final Throwable t) {
logger.log(NOTICE, null, msg, t);
}
/**
* Logs a message object with the {@code NOTICE} level.
*
* @param message the message object to log.
*/
public void notice(final Object message) {
logger.log(NOTICE, null, message, null);
}
/**
* Logs a message at the {@code NOTICE} level including the stack trace of
the {@link Throwable}
* {@code t} passed as parameter.
*
* @param message the message to log.
* @param t the exception to log, including its stack trace.
*/
public void notice(final Object message, final Throwable t) {
logger.log(NOTICE, null, message, t);
}
/**
* Logs a message object with the {@code NOTICE} level.
*
* @param message the message object to log.
*/
public void notice(final String message) {
logger.log(NOTICE, null, message, (Throwable) null);
}
/**
* Logs a message with parameters at the {@code NOTICE} level.
*
* @param message the message to log; the format depends on the message
factory.
* @param params parameters to the message.
* @see #getMessageFactory()
*/
public void notice(final String message, final Object... params) {
logger.log(NOTICE, null, message, params);
}
/**
* Logs a message at the {@code NOTICE} level including the stack trace of
the {@link Throwable}
* {@code t} passed as parameter.
*
* @param message the message to log.
* @param t the exception to log, including its stack trace.
*/
public void notice(final String message, final Throwable t) {
logger.log(NOTICE, null, message, t);
}
/**
* Logs a message with the specific Marker at the {@code VERBOSE} level.
*
* @param marker the marker data specific to this log statement
* @param msg the message string to be logged
*/
public void verbose(final Marker marker, final Message msg) {
logger.log(VERBOSE, marker, msg, null);
}
/**
* Logs a message with the specific Marker at the {@code VERBOSE} 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.
*/
public void verbose(final Marker marker, final Message msg, final Throwable
t) {
logger.log(VERBOSE, marker, msg, t);
}
/**
* Logs a message object with the {@code VERBOSE} level.
*
* @param marker the marker data specific to this log statement
* @param message the message object to log.
*/
public void verbose(final Marker marker, final Object message) {
logger.log(VERBOSE, marker, message, null);
}
/**
* Logs a message at the {@code VERBOSE} level including the stack trace of
the
* {@link Throwable} {@code t} 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.
*/
public void verbose(final Marker marker, final Object message, final
Throwable t) {
logger.log(VERBOSE, marker, message, t);
}
/**
* Logs a message object with the {@code VERBOSE} level.
*
* @param marker the marker data specific to this log statement
* @param message the message object to log.
*/
public void verbose(final Marker marker, final String message) {
logger.log(VERBOSE, marker, message, (Throwable) null);
}
/**
* Logs a message with parameters at the {@code VERBOSE} 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()
*/
public void verbose(final Marker marker, final String message, final
Object... params) {
logger.log(VERBOSE, marker, message, params);
}
/**
* Logs a message at the {@code VERBOSE} level including the stack trace of
the
* {@link Throwable} {@code t} 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.
*/
public void verbose(final Marker marker, final String message, final
Throwable t) {
logger.log(VERBOSE, marker, message, t);
}
/**
* Logs the specified Message at the {@code VERBOSE} level.
*
* @param msg the message string to be logged
*/
public void verbose(final Message msg) {
logger.log(VERBOSE, null, msg, null);
}
/**
* Logs the specified Message at the {@code VERBOSE} level.
*
* @param msg the message string to be logged
* @param t A Throwable or null.
*/
public void verbose(final Message msg, final Throwable t) {
logger.log(VERBOSE, null, msg, t);
}
/**
* Logs a message object with the {@code VERBOSE} level.
*
* @param message the message object to log.
*/
public void verbose(final Object message) {
logger.log(VERBOSE, null, message, null);
}
/**
* Logs a message at the {@code VERBOSE} level including the stack trace of
the
* {@link Throwable} {@code t} passed as parameter.
*
* @param message the message to log.
* @param t the exception to log, including its stack trace.
*/
public void verbose(final Object message, final Throwable t) {
logger.log(VERBOSE, null, message, t);
}
/**
* Logs a message object with the {@code VERBOSE} level.
*
* @param message the message object to log.
*/
public void verbose(final String message) {
logger.log(VERBOSE, null, message, (Throwable) null);
}
/**
* Logs a message with parameters at the {@code VERBOSE} level.
*
* @param message the message to log; the format depends on the message
factory.
* @param params parameters to the message.
* @see #getMessageFactory()
*/
public void verbose(final String message, final Object... params) {
logger.log(VERBOSE, null, message, params);
}
/**
* Logs a message at the {@code VERBOSE} level including the stack trace of
the
* {@link Throwable} {@code t} passed as parameter.
*
* @param message the message to log.
* @param t the exception to log, including its stack trace.
*/
public void verbose(final String message, final Throwable t) {
logger.log(VERBOSE, null, message, t);
}
}
{code}
> Custom/Extended Loggers
> -----------------------
>
> Key: LOG4J2-519
> URL: https://issues.apache.org/jira/browse/LOG4J2-519
> Project: Log4j 2
> Issue Type: New Feature
> Components: API
> Affects Versions: 2.0-rc1
> Reporter: Remko Popma
> Attachments: Generate.java, Generate_old_20140130.java
>
>
> Please try out the attached [^Generate.java] tool that provides the
> functionality described below and give feedback if you notice any issues.
> {anchor:Extending}
> *[#Extending] the Logger interface*
> LOG4J2-41 added support for custom log Levels. Users can now log messages at
> the new custom level by passing the custom level to the {{Logger.log()}}
> method:
> {code}
> final Logger logger = LogManager.getLogger();
> final Level VERBOSE = Level.forName("VERBOSE", 550);
> logger.log(VERBOSE, "a verbose message");
> logger.log(VERBOSE, "another message");
> {code}
> However, custom levels are not as easy to use as the built-in levels: one
> needs to call the generic {{log()}} method and pass a {{Level}} parameter.
> Built-in levels on the other hand have a set of convenience methods on the
> Logger interface. For example, the Logger interface has 14 debug methods that
> support the DEBUG level:
> {code}
> debug(Marker, Message)
> debug(Marker, Message, Throwable)
> debug(Marker, Object)
> debug(Marker, Object, Throwable)
> debug(Marker, String)
> debug(Marker, String, Object...)
> debug(Marker, String, Throwable)
> debug(Message)
> debug(Message, Throwable)
> debug(Object)
> debug(Object, Throwable)
> debug(String)
> debug(String, Object...)
> debug(String, Throwable)
> {code}
> Similar method sets exist for the other built-in levels.
> Several people have expressed the desire to have the same ease of use with
> custom levels, so after declaring a custom VERBOSE level, we would like to be
> able to use code like this:
> {code}
> logger.verbose("a verbose message"); // no need to pass the VERBOSE level as
> a parameter
> logger.verbose("another message");
> {code}
> {anchor:Customizing}
> *[#Customizing] the Logger interface*
> In the above use case, convenience methods were _added_ to the Logger
> interface, in addition to the existing {{trace()}}, {{debug()}}, {{info()}},
> ... methods for the built-in log levels.
> There is another use case, Domain Specific Language loggers, where we want to
> _replace_ the existing {{trace()}}, {{debug()}}, {{info()}}, ... methods with
> all-custom methods.
> For example, for medical devices we could have only {{critical()}},
> {{warning()}}, and {{advisory()}} methods. Another example could be a game
> that has only {{defcon1()}}, {{defcon2()}}, and {{defcon3()}} levels.
> Finally, if it were possible to hide existing log levels, users could
> customize the Logger interface to match their requirements. Some people may
> not want to have a {{FATAL}} or a {{TRACE}} level, for example. They would
> like to be able to create a custom Logger that only has {{debug()}},
> {{info()}}, {{warn()}} and {{error()}} methods.
> {anchor:wrapper}
> *Proposal: Generate source code for a Logger [#wrapper]*
> Common log4j usage is to get an instance of the {{Logger}} interface from the
> {{LogManager}} and call the methods on this interface. This makes it hard to
> achieve the above customization; especially taking away existing methods is
> not possible.
> An alternative is for the user to create a wrapper class that exposes only
> the convenience methods for the desired log levels. When _extending_ the
> {{Logger}} API (_adding_ methods), this wrapper class could subclass
> {{org.apache.logging.log4j.spi.AbstractLoggerWrapper}}. When _customizing_
> the {{Logger}} API (_removing_ built-in methods), the wrapper class would
> simply not extend AbstractLoggerWrapper, so the only public methods would be
> the methods for the custom log levels.
> As the custom log Levels are not known in advance, Log4J cannot provide
> pre-built wrapper classes for these custom log Levels. However, we don't want
> to ask the users to hand-code such a wrapper class: this is cumbersome and
> error-prone: there are 14 methods for each built-in level. To provide
> comparable functionality for custom log Levels one would need to provide 14
> methods for each custom log Level.
> The proposal is to solve this by providing a tool that generates the source
> code for a wrapper class. The user can specify:
> * the fully qualified name of the class to generate
> * the list of custom levels to support and their {{intLevel}} relative
> strength
> * whether to extend {{Logger}} (and keep the existing built-in methods) or
> have only methods for the custom log levels
> and the tool generates the source code for the wrapper class that has exactly
> the required methods. Users would include this source code in the project
> where they want to use custom log levels.
> {anchor:GeneratedWrapperUsage}
> Note that no Log4J API changes are required to support this functionality.
> Users would create instances of the wrapper by calling a factory method on
> the wrapper class, instead of calling the {{LogManager.getLogger()}} methods.
> For example, instead of writing:
> {code}
> import org.apache.logging.log4j.Level;
> import org.apache.logging.log4j.LogManager;
> import org.apache.logging.log4j.Logger;
> final Logger logger = LogManager.getLogger(MyClass.class); // standard log4j
> logger
> final Level VERBOSE = Level.forName("VERBOSE", 550);
> logger.log(VERBOSE, "a verbose message");
> {code}
> users would instead write:
> {code}
> // MyLogger is a generated customized logger wrapper
> import com.mycompany.myproject.MyLogger;
> final MyLogger logger = MyLogger.create(MyClass.class);
> logger.verbose("a verbose message");
> {code}
> Creating an extended or customized Logger is as easy as creating a standard
> Logger (one line of code). Extended Loggers are drop-in replacements for
> standard Loggers - they only add more convenience methods.
--
This message was sent by Atlassian JIRA
(v6.2#6252)
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]