You are viewing a plain text version of this content. The canonical link for it is here.
Posted to log4j-dev@logging.apache.org by Paul Benedict <pb...@apache.org> on 2012/08/09 22:26:22 UTC
Pluggable message formats?
Forgive me if this feature already exists, but I was hoping to find if
log4j 2 contained a way to configure the format *provider* of
parametrized messages. This is an important aspect for my own work
because I have some sophisticated formatting in my logs that use
String.format() [1] -- especially for floating point values like
currency.
Rather than:
logger.debug("Logging in user {} with id {}", user.getName(), user.getId());
I would like some more complexity like:
logger.debug("Logging in user {%s} with id {%10s}", user.getName(),
user.getId());
I am eager to hear any opinions on my proposed feature.
[1] http://docs.oracle.com/javase/1.5.0/docs/api/java/util/Formatter.html#syntax
Paul
---------------------------------------------------------------------
To unsubscribe, e-mail: log4j-dev-unsubscribe@logging.apache.org
For additional commands, e-mail: log4j-dev-help@logging.apache.org
Re: Pluggable message formats?
Posted by Ralph Goers <ra...@dslextreme.com>.
I created StringFormattedMessage and tested it. It is anywhere from 5 to 10 times slower than ParameterizedMessage on my Mac. But it is there if you want to use it.
Ralph
On Aug 9, 2012, at 10:08 PM, Paul Benedict wrote:
> On Thu, Aug 9, 2012 at 4:32 PM, Ralph Goers <ra...@dslextreme.com> wrote:
>> This topic has come up several times, although your request seems slightly different.
>>
>> Every log event in Log4j 2 is handled by a message object. When you specify logger.debug("", p1, p2) that gets converted essentially to logger.debug(new ParameterizedMessage("", p1, p2)); . So if you want to use a different message format you could create a JavaStringMessage and just do logger.debug(new JavaStringMessage("", p2, p2));
>>
>> Most proposals to date have asked to use String.format for the whole String while you seem to be suggesting that each item within {} be replaced by whatever String.format returns from the single parameter passed to it. I would think that would be slower than handling the whole String.
>>
>> Ceki has tested java.text.MessageFormat and said that it is at least 10 times slower than the way ParameterizedMessage work. I don't know if he, or anyone else, has compared Formatter or String.format against ParameterizedMessage but I'd find it hard to believe it isn't considerably slower also. That said, if someone wants to implement it I have no problem adding it. If it is demonstrated to be nearly the same speed I would also have no problem making it the default.
>>
>> Ralph
>
> Good points. Message formatting speed isn't too much of my concern
> since most of my logging is solely for DEBUG/TRACE level. I will
> investigate if there's a happy medium and get back to you.
>
> Paul
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: log4j-dev-unsubscribe@logging.apache.org
> For additional commands, e-mail: log4j-dev-help@logging.apache.org
>
---------------------------------------------------------------------
To unsubscribe, e-mail: log4j-dev-unsubscribe@logging.apache.org
For additional commands, e-mail: log4j-dev-help@logging.apache.org
Re: Pluggable message formats?
Posted by Paul Benedict <pb...@apache.org>.
On Thu, Aug 9, 2012 at 4:32 PM, Ralph Goers <ra...@dslextreme.com> wrote:
> This topic has come up several times, although your request seems slightly different.
>
> Every log event in Log4j 2 is handled by a message object. When you specify logger.debug("", p1, p2) that gets converted essentially to logger.debug(new ParameterizedMessage("", p1, p2)); . So if you want to use a different message format you could create a JavaStringMessage and just do logger.debug(new JavaStringMessage("", p2, p2));
>
> Most proposals to date have asked to use String.format for the whole String while you seem to be suggesting that each item within {} be replaced by whatever String.format returns from the single parameter passed to it. I would think that would be slower than handling the whole String.
>
> Ceki has tested java.text.MessageFormat and said that it is at least 10 times slower than the way ParameterizedMessage work. I don't know if he, or anyone else, has compared Formatter or String.format against ParameterizedMessage but I'd find it hard to believe it isn't considerably slower also. That said, if someone wants to implement it I have no problem adding it. If it is demonstrated to be nearly the same speed I would also have no problem making it the default.
>
> Ralph
Good points. Message formatting speed isn't too much of my concern
since most of my logging is solely for DEBUG/TRACE level. I will
investigate if there's a happy medium and get back to you.
Paul
---------------------------------------------------------------------
To unsubscribe, e-mail: log4j-dev-unsubscribe@logging.apache.org
For additional commands, e-mail: log4j-dev-help@logging.apache.org
Re: Pluggable message formats?
Posted by Ralph Goers <ra...@dslextreme.com>.
This topic has come up several times, although your request seems slightly different.
Every log event in Log4j 2 is handled by a message object. When you specify logger.debug("", p1, p2) that gets converted essentially to logger.debug(new ParameterizedMessage("", p1, p2)); . So if you want to use a different message format you could create a JavaStringMessage and just do logger.debug(new JavaStringMessage("", p2, p2));
Most proposals to date have asked to use String.format for the whole String while you seem to be suggesting that each item within {} be replaced by whatever String.format returns from the single parameter passed to it. I would think that would be slower than handling the whole String.
Ceki has tested java.text.MessageFormat and said that it is at least 10 times slower than the way ParameterizedMessage work. I don't know if he, or anyone else, has compared Formatter or String.format against ParameterizedMessage but I'd find it hard to believe it isn't considerably slower also. That said, if someone wants to implement it I have no problem adding it. If it is demonstrated to be nearly the same speed I would also have no problem making it the default.
Ralph
On Aug 9, 2012, at 1:26 PM, Paul Benedict wrote:
> Forgive me if this feature already exists, but I was hoping to find if
> log4j 2 contained a way to configure the format *provider* of
> parametrized messages. This is an important aspect for my own work
> because I have some sophisticated formatting in my logs that use
> String.format() [1] -- especially for floating point values like
> currency.
>
> Rather than:
> logger.debug("Logging in user {} with id {}", user.getName(), user.getId());
>
> I would like some more complexity like:
> logger.debug("Logging in user {%s} with id {%10s}", user.getName(),
> user.getId());
>
> I am eager to hear any opinions on my proposed feature.
>
> [1] http://docs.oracle.com/javase/1.5.0/docs/api/java/util/Formatter.html#syntax
>
> Paul
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: log4j-dev-unsubscribe@logging.apache.org
> For additional commands, e-mail: log4j-dev-help@logging.apache.org
>
---------------------------------------------------------------------
To unsubscribe, e-mail: log4j-dev-unsubscribe@logging.apache.org
For additional commands, e-mail: log4j-dev-help@logging.apache.org