[ 
https://issues.apache.org/jira/browse/LOG4J2-2970?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=17244584#comment-17244584
 ] 

Ralph Goers commented on LOG4J2-2970:
-------------------------------------

The one disadvantage to that pattern, which Colin showed in his example, is 
that log4j could resolve the variables.  But the only way this could really 
work at this point is to introduce a new method that looks like
{code:java}
public void error(StringBuilder result, String format, Object... params){code}
Where the rendered message is returned in the StringBuilder. That said, if you 
really wanted to do this it would make more sense to me to add aa method like
{code:java}
public String formatMessage(String format, Object... params){code}
then you would do:
{code:java}
String msg = logger.format("The number of records is {} which exceed the 
threshold {}",  num, threshold);
logger.error(msg);
throw new ValidationException(msg);{code}

> Adding String return type of each log method
> --------------------------------------------
>
>                 Key: LOG4J2-2970
>                 URL: https://issues.apache.org/jira/browse/LOG4J2-2970
>             Project: Log4j 2
>          Issue Type: New Feature
>          Components: API
>    Affects Versions: 2.13.3
>            Reporter: Colin Zhang
>            Priority: Critical
>              Labels: API, user-experience
>   Original Estimate: 168h
>  Remaining Estimate: 168h
>
> In many industry systems,  we need to log the error and then throw a new 
> exception with the same message. The logger will populate the message with 
> markers and variables.
> But we need to manually populated it by string + again when construct the 
> exception message. This is an anti-pattern.
> Please take below snippet as example:
> {code:java}
> log.error("The number of records is {} which exceed the threshold {}",  num, 
> threshold); 
> throw new ValidationException("The number of record is " + num + " which 
> exceed the threshold " + threshold);
> {code}
>  
> If the API can return populated message, the code would turn much better:
> {code:java}
> String error = log.error("The number of records is {} which exceed the 
> threshold {}",  num, threshold);
> throw new ValidationException(error);
> {code}
>  
> The only concern is that this may only works for synchronous logging, not 
> work for asynchronous way. But I believe it could be resolved.



--
This message was sent by Atlassian Jira
(v8.3.4#803005)

Reply via email to