[jira] [Commented] (LOG4J2-2118) MutableLogEvent does not retain the Message format string

2018-04-07 Thread ASF GitHub Bot (JIRA)

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

ASF GitHub Bot commented on LOG4J2-2118:


Github user cakofony commented on the issue:

https://github.com/apache/logging-log4j2/pull/131
  
@uncleyo @zslayton This should be resolved as of 
[this](https://github.com/apache/logging-log4j2/commit/967bb0f71155aa00acadbd95362a90832bed69f6)
 commit (included in 2.11.0).


[LOG4J2-2307](https://github.com/apache/logging-log4j2/commit/0738cf970c111a07a7307549b8c16af4d44af8a4)
 may also be relevant for scenarios where a memento is created (AsyncAppender, 
for instance)


> MutableLogEvent does not retain the Message format string
> -
>
> Key: LOG4J2-2118
> URL: https://issues.apache.org/jira/browse/LOG4J2-2118
> Project: Log4j 2
>  Issue Type: Bug
>  Components: Layouts
>Affects Versions: 2.8
> Environment: Mac OS X 10.11.6
> Java 1.8.0_141
>Reporter: Zack Slayton
>Assignee: Remko Popma
>Priority: Major
>  Labels: layout
> Fix For: 2.11.0
>
>
> I'm attempting to create a custom binary Layout that stores the provided 
> format string separate from the parameters rather than eagerly formatting 
> them all into a single piece of text. However, when I went to implement 
> `Layout::toByteArray`, I discovered that the `Message::getFormat` method 
> always returns null.
> {code:java}
> import org.apache.logging.log4j.LogManager;
> import org.apache.logging.log4j.Logger;
> import org.apache.logging.log4j.core.LogEvent;
> import org.apache.logging.log4j.core.config.plugins.Plugin;
> import org.apache.logging.log4j.core.config.plugins.PluginFactory;
> import org.apache.logging.log4j.core.layout.AbstractLayout;
> import java.io.Serializable;
> import java.util.Arrays;
> @Plugin(name = "MyLayout", category = "Core", elementType = "layout", 
> printObject = true)
> public class MyLayout extends AbstractLayout {
> public MyLayout() {
> super(null, null, null);
> }
> @PluginFactory
> public static MyLayout createLayout(){
> return new MyLayout();
> }
> public static void main(String[] args) {
> Logger log = LogManager.getLogger(MyLayout.class);
> log.info("Here's a thing: {}. And another: {}", 75, "walrus");
> }
> @Override
> public byte[] toByteArray(LogEvent event) {
> System.out.println("Format: " + event.getMessage().getFormat());
> System.out.println("Parameters: " + 
> Arrays.toString(event.getMessage().getParameters()));
> return new byte[0];
> }
> @Override
> public Serializable toSerializable(LogEvent event) {
> return null;
> }
> @Override
> public String getContentType() {
> return null;
> }
> @Override
> public byte[] getFooter() {
> return new byte[0];
> }
> @Override
> public byte[] getHeader() {
> return new byte[0];
> }
> }
> {code}
> If I run `main` (with an extra `PatternLayout`+`Console` appender in my 
> config), I see the following output:
> {code}
> 17:55:28.524 [main] INFO  com.example.logging.plugins.MyLayout - Here's a 
> thing: 75. And another: walrus
> Format: null
> Parameters: [75, walrus]
> {code}
> I had expected to be able to see the format string and the array of 
> parameters, but instead I can only see the parameters. The format string is 
> always null.
> This appears to be due to the implementation of `MutableLogEvent::setMessage` 
> highlighted [in 
> LOG4J2-1510|https://issues.apache.org/jira/browse/LOG4J2-1510]. Because the 
> messages are reusable, they appear to be eagerly formatted and the format 
> string itself is discarded. The `getFormat` method is hardcoded to return 
> `null`.
> Is this by design? If so, what is the prescribed approach to getting the 
> format string in my layout? (Preferably one that does not disable the 
> performance optimizations offered by enabling threadlocals.)
> Thanks!



--
This message was sent by Atlassian JIRA
(v7.6.3#76005)


[jira] [Commented] (LOG4J2-2118) MutableLogEvent does not retain the Message format string

2018-02-26 Thread Remko Popma (JIRA)

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

Remko Popma commented on LOG4J2-2118:
-

There is some work in progress that seems related (duplicate?) of this ticket.

I just merged a pull request for LOG4J2-2252 into master, and hopefully a 
similar one into the release-2.x branch soon.
Can you check if that resolves the issue?

> MutableLogEvent does not retain the Message format string
> -
>
> Key: LOG4J2-2118
> URL: https://issues.apache.org/jira/browse/LOG4J2-2118
> Project: Log4j 2
>  Issue Type: Bug
>  Components: Layouts
>Affects Versions: 2.8
> Environment: Mac OS X 10.11.6
> Java 1.8.0_141
>Reporter: Zack Slayton
>Assignee: Remko Popma
>Priority: Major
>  Labels: layout
>
> I'm attempting to create a custom binary Layout that stores the provided 
> format string separate from the parameters rather than eagerly formatting 
> them all into a single piece of text. However, when I went to implement 
> `Layout::toByteArray`, I discovered that the `Message::getFormat` method 
> always returns null.
> {code:java}
> import org.apache.logging.log4j.LogManager;
> import org.apache.logging.log4j.Logger;
> import org.apache.logging.log4j.core.LogEvent;
> import org.apache.logging.log4j.core.config.plugins.Plugin;
> import org.apache.logging.log4j.core.config.plugins.PluginFactory;
> import org.apache.logging.log4j.core.layout.AbstractLayout;
> import java.io.Serializable;
> import java.util.Arrays;
> @Plugin(name = "MyLayout", category = "Core", elementType = "layout", 
> printObject = true)
> public class MyLayout extends AbstractLayout {
> public MyLayout() {
> super(null, null, null);
> }
> @PluginFactory
> public static MyLayout createLayout(){
> return new MyLayout();
> }
> public static void main(String[] args) {
> Logger log = LogManager.getLogger(MyLayout.class);
> log.info("Here's a thing: {}. And another: {}", 75, "walrus");
> }
> @Override
> public byte[] toByteArray(LogEvent event) {
> System.out.println("Format: " + event.getMessage().getFormat());
> System.out.println("Parameters: " + 
> Arrays.toString(event.getMessage().getParameters()));
> return new byte[0];
> }
> @Override
> public Serializable toSerializable(LogEvent event) {
> return null;
> }
> @Override
> public String getContentType() {
> return null;
> }
> @Override
> public byte[] getFooter() {
> return new byte[0];
> }
> @Override
> public byte[] getHeader() {
> return new byte[0];
> }
> }
> {code}
> If I run `main` (with an extra `PatternLayout`+`Console` appender in my 
> config), I see the following output:
> {code}
> 17:55:28.524 [main] INFO  com.example.logging.plugins.MyLayout - Here's a 
> thing: 75. And another: walrus
> Format: null
> Parameters: [75, walrus]
> {code}
> I had expected to be able to see the format string and the array of 
> parameters, but instead I can only see the parameters. The format string is 
> always null.
> This appears to be due to the implementation of `MutableLogEvent::setMessage` 
> highlighted [in 
> LOG4J2-1510|https://issues.apache.org/jira/browse/LOG4J2-1510]. Because the 
> messages are reusable, they appear to be eagerly formatted and the format 
> string itself is discarded. The `getFormat` method is hardcoded to return 
> `null`.
> Is this by design? If so, what is the prescribed approach to getting the 
> format string in my layout? (Preferably one that does not disable the 
> performance optimizations offered by enabling threadlocals.)
> Thanks!



--
This message was sent by Atlassian JIRA
(v7.6.3#76005)


[jira] [Commented] (LOG4J2-2118) MutableLogEvent does not retain the Message format string

2017-12-28 Thread ASF GitHub Bot (JIRA)

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

ASF GitHub Bot commented on LOG4J2-2118:


Github user uncleyo commented on the issue:

https://github.com/apache/logging-log4j2/pull/131
  
Facing the same issue. It stands in our way of having nice structured 
logging, so definitely +1


> MutableLogEvent does not retain the Message format string
> -
>
> Key: LOG4J2-2118
> URL: https://issues.apache.org/jira/browse/LOG4J2-2118
> Project: Log4j 2
>  Issue Type: Bug
>  Components: Layouts
>Affects Versions: 2.8
> Environment: Mac OS X 10.11.6
> Java 1.8.0_141
>Reporter: Zack Slayton
>  Labels: layout
>
> I'm attempting to create a custom binary Layout that stores the provided 
> format string separate from the parameters rather than eagerly formatting 
> them all into a single piece of text. However, when I went to implement 
> `Layout::toByteArray`, I discovered that the `Message::getFormat` method 
> always returns null.
> {code:java}
> import org.apache.logging.log4j.LogManager;
> import org.apache.logging.log4j.Logger;
> import org.apache.logging.log4j.core.LogEvent;
> import org.apache.logging.log4j.core.config.plugins.Plugin;
> import org.apache.logging.log4j.core.config.plugins.PluginFactory;
> import org.apache.logging.log4j.core.layout.AbstractLayout;
> import java.io.Serializable;
> import java.util.Arrays;
> @Plugin(name = "MyLayout", category = "Core", elementType = "layout", 
> printObject = true)
> public class MyLayout extends AbstractLayout {
> public MyLayout() {
> super(null, null, null);
> }
> @PluginFactory
> public static MyLayout createLayout(){
> return new MyLayout();
> }
> public static void main(String[] args) {
> Logger log = LogManager.getLogger(MyLayout.class);
> log.info("Here's a thing: {}. And another: {}", 75, "walrus");
> }
> @Override
> public byte[] toByteArray(LogEvent event) {
> System.out.println("Format: " + event.getMessage().getFormat());
> System.out.println("Parameters: " + 
> Arrays.toString(event.getMessage().getParameters()));
> return new byte[0];
> }
> @Override
> public Serializable toSerializable(LogEvent event) {
> return null;
> }
> @Override
> public String getContentType() {
> return null;
> }
> @Override
> public byte[] getFooter() {
> return new byte[0];
> }
> @Override
> public byte[] getHeader() {
> return new byte[0];
> }
> }
> {code}
> If I run `main` (with an extra `PatternLayout`+`Console` appender in my 
> config), I see the following output:
> {code}
> 17:55:28.524 [main] INFO  com.example.logging.plugins.MyLayout - Here's a 
> thing: 75. And another: walrus
> Format: null
> Parameters: [75, walrus]
> {code}
> I had expected to be able to see the format string and the array of 
> parameters, but instead I can only see the parameters. The format string is 
> always null.
> This appears to be due to the implementation of `MutableLogEvent::setMessage` 
> highlighted [in 
> LOG4J2-1510|https://issues.apache.org/jira/browse/LOG4J2-1510]. Because the 
> messages are reusable, they appear to be eagerly formatted and the format 
> string itself is discarded. The `getFormat` method is hardcoded to return 
> `null`.
> Is this by design? If so, what is the prescribed approach to getting the 
> format string in my layout? (Preferably one that does not disable the 
> performance optimizations offered by enabling threadlocals.)
> Thanks!



--
This message was sent by Atlassian JIRA
(v6.4.14#64029)


[jira] [Commented] (LOG4J2-2118) MutableLogEvent does not retain the Message format string

2017-11-19 Thread Zack Slayton (JIRA)

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

Zack Slayton commented on LOG4J2-2118:
--

Thank you for the suggestion. Unfortunately, both `ReusableObjectMessage` and 
`ReusableSimpleMessage` require `String.valueOf` to be called, on an `Object` 
and a `CharSequence` respectively. It would appear that we would need a version 
of `getFormat` that accepts a buffer as a parameter to completely avoid 
allocations.


> MutableLogEvent does not retain the Message format string
> -
>
> Key: LOG4J2-2118
> URL: https://issues.apache.org/jira/browse/LOG4J2-2118
> Project: Log4j 2
>  Issue Type: Bug
>  Components: Layouts
>Affects Versions: 2.8
> Environment: Mac OS X 10.11.6
> Java 1.8.0_141
>Reporter: Zack Slayton
>  Labels: layout
>
> I'm attempting to create a custom binary Layout that stores the provided 
> format string separate from the parameters rather than eagerly formatting 
> them all into a single piece of text. However, when I went to implement 
> `Layout::toByteArray`, I discovered that the `Message::getFormat` method 
> always returns null.
> {code:java}
> import org.apache.logging.log4j.LogManager;
> import org.apache.logging.log4j.Logger;
> import org.apache.logging.log4j.core.LogEvent;
> import org.apache.logging.log4j.core.config.plugins.Plugin;
> import org.apache.logging.log4j.core.config.plugins.PluginFactory;
> import org.apache.logging.log4j.core.layout.AbstractLayout;
> import java.io.Serializable;
> import java.util.Arrays;
> @Plugin(name = "MyLayout", category = "Core", elementType = "layout", 
> printObject = true)
> public class MyLayout extends AbstractLayout {
> public MyLayout() {
> super(null, null, null);
> }
> @PluginFactory
> public static MyLayout createLayout(){
> return new MyLayout();
> }
> public static void main(String[] args) {
> Logger log = LogManager.getLogger(MyLayout.class);
> log.info("Here's a thing: {}. And another: {}", 75, "walrus");
> }
> @Override
> public byte[] toByteArray(LogEvent event) {
> System.out.println("Format: " + event.getMessage().getFormat());
> System.out.println("Parameters: " + 
> Arrays.toString(event.getMessage().getParameters()));
> return new byte[0];
> }
> @Override
> public Serializable toSerializable(LogEvent event) {
> return null;
> }
> @Override
> public String getContentType() {
> return null;
> }
> @Override
> public byte[] getFooter() {
> return new byte[0];
> }
> @Override
> public byte[] getHeader() {
> return new byte[0];
> }
> }
> {code}
> If I run `main` (with an extra `PatternLayout`+`Console` appender in my 
> config), I see the following output:
> {code}
> 17:55:28.524 [main] INFO  com.example.logging.plugins.MyLayout - Here's a 
> thing: 75. And another: walrus
> Format: null
> Parameters: [75, walrus]
> {code}
> I had expected to be able to see the format string and the array of 
> parameters, but instead I can only see the parameters. The format string is 
> always null.
> This appears to be due to the implementation of `MutableLogEvent::setMessage` 
> highlighted [in 
> LOG4J2-1510|https://issues.apache.org/jira/browse/LOG4J2-1510]. Because the 
> messages are reusable, they appear to be eagerly formatted and the format 
> string itself is discarded. The `getFormat` method is hardcoded to return 
> `null`.
> Is this by design? If so, what is the prescribed approach to getting the 
> format string in my layout? (Preferably one that does not disable the 
> performance optimizations offered by enabling threadlocals.)
> Thanks!



--
This message was sent by Atlassian JIRA
(v6.4.14#64029)


[jira] [Commented] (LOG4J2-2118) MutableLogEvent does not retain the Message format string

2017-11-15 Thread JIRA

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

Mikael Ståldal commented on LOG4J2-2118:


Maybe a _read optimized format_ like Ion is not the best one for logging?

> MutableLogEvent does not retain the Message format string
> -
>
> Key: LOG4J2-2118
> URL: https://issues.apache.org/jira/browse/LOG4J2-2118
> Project: Log4j 2
>  Issue Type: Bug
>  Components: Layouts
>Affects Versions: 2.8
> Environment: Mac OS X 10.11.6
> Java 1.8.0_141
>Reporter: Zack Slayton
>  Labels: layout
>
> I'm attempting to create a custom binary Layout that stores the provided 
> format string separate from the parameters rather than eagerly formatting 
> them all into a single piece of text. However, when I went to implement 
> `Layout::toByteArray`, I discovered that the `Message::getFormat` method 
> always returns null.
> {code:java}
> import org.apache.logging.log4j.LogManager;
> import org.apache.logging.log4j.Logger;
> import org.apache.logging.log4j.core.LogEvent;
> import org.apache.logging.log4j.core.config.plugins.Plugin;
> import org.apache.logging.log4j.core.config.plugins.PluginFactory;
> import org.apache.logging.log4j.core.layout.AbstractLayout;
> import java.io.Serializable;
> import java.util.Arrays;
> @Plugin(name = "MyLayout", category = "Core", elementType = "layout", 
> printObject = true)
> public class MyLayout extends AbstractLayout {
> public MyLayout() {
> super(null, null, null);
> }
> @PluginFactory
> public static MyLayout createLayout(){
> return new MyLayout();
> }
> public static void main(String[] args) {
> Logger log = LogManager.getLogger(MyLayout.class);
> log.info("Here's a thing: {}. And another: {}", 75, "walrus");
> }
> @Override
> public byte[] toByteArray(LogEvent event) {
> System.out.println("Format: " + event.getMessage().getFormat());
> System.out.println("Parameters: " + 
> Arrays.toString(event.getMessage().getParameters()));
> return new byte[0];
> }
> @Override
> public Serializable toSerializable(LogEvent event) {
> return null;
> }
> @Override
> public String getContentType() {
> return null;
> }
> @Override
> public byte[] getFooter() {
> return new byte[0];
> }
> @Override
> public byte[] getHeader() {
> return new byte[0];
> }
> }
> {code}
> If I run `main` (with an extra `PatternLayout`+`Console` appender in my 
> config), I see the following output:
> {code}
> 17:55:28.524 [main] INFO  com.example.logging.plugins.MyLayout - Here's a 
> thing: 75. And another: walrus
> Format: null
> Parameters: [75, walrus]
> {code}
> I had expected to be able to see the format string and the array of 
> parameters, but instead I can only see the parameters. The format string is 
> always null.
> This appears to be due to the implementation of `MutableLogEvent::setMessage` 
> highlighted [in 
> LOG4J2-1510|https://issues.apache.org/jira/browse/LOG4J2-1510]. Because the 
> messages are reusable, they appear to be eagerly formatted and the format 
> string itself is discarded. The `getFormat` method is hardcoded to return 
> `null`.
> Is this by design? If so, what is the prescribed approach to getting the 
> format string in my layout? (Preferably one that does not disable the 
> performance optimizations offered by enabling threadlocals.)
> Thanks!



--
This message was sent by Atlassian JIRA
(v6.4.14#64029)


[jira] [Commented] (LOG4J2-2118) MutableLogEvent does not retain the Message format string

2017-11-14 Thread Zack Slayton (JIRA)

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

Zack Slayton commented on LOG4J2-2118:
--

[~rem...@yahoo.com] Ah, that makes sense! Thank you for taking the time to 
explain it. I'll look into making a custom `Message` type.

[~garydgregory] I'm planning to make a binary `Layout` that encodes each 
message's components using [Jackson 2.9's new support for the Ion 
format|https://github.com/FasterXML/jackson/wiki/Jackson-Release-2.9#dataformat-ion].
 It ticks a lot of the feature boxes mentioned in 
[LOG4J2-1305|https://issues.apache.org/jira/browse/LOG4J2-1305]: it supports 
string lookup tables, has bindings in several languages, and field byte lengths 
are included in the encoding. As an added bonus, you don't have to encode 
logged parameters as text -- there's [a rich type 
system|https://amzn.github.io/ion-docs/] to take advantage of.

> MutableLogEvent does not retain the Message format string
> -
>
> Key: LOG4J2-2118
> URL: https://issues.apache.org/jira/browse/LOG4J2-2118
> Project: Log4j 2
>  Issue Type: Bug
>  Components: Layouts
>Affects Versions: 2.8
> Environment: Mac OS X 10.11.6
> Java 1.8.0_141
>Reporter: Zack Slayton
>  Labels: layout
>
> I'm attempting to create a custom binary Layout that stores the provided 
> format string separate from the parameters rather than eagerly formatting 
> them all into a single piece of text. However, when I went to implement 
> `Layout::toByteArray`, I discovered that the `Message::getFormat` method 
> always returns null.
> {code:java}
> import org.apache.logging.log4j.LogManager;
> import org.apache.logging.log4j.Logger;
> import org.apache.logging.log4j.core.LogEvent;
> import org.apache.logging.log4j.core.config.plugins.Plugin;
> import org.apache.logging.log4j.core.config.plugins.PluginFactory;
> import org.apache.logging.log4j.core.layout.AbstractLayout;
> import java.io.Serializable;
> import java.util.Arrays;
> @Plugin(name = "MyLayout", category = "Core", elementType = "layout", 
> printObject = true)
> public class MyLayout extends AbstractLayout {
> public MyLayout() {
> super(null, null, null);
> }
> @PluginFactory
> public static MyLayout createLayout(){
> return new MyLayout();
> }
> public static void main(String[] args) {
> Logger log = LogManager.getLogger(MyLayout.class);
> log.info("Here's a thing: {}. And another: {}", 75, "walrus");
> }
> @Override
> public byte[] toByteArray(LogEvent event) {
> System.out.println("Format: " + event.getMessage().getFormat());
> System.out.println("Parameters: " + 
> Arrays.toString(event.getMessage().getParameters()));
> return new byte[0];
> }
> @Override
> public Serializable toSerializable(LogEvent event) {
> return null;
> }
> @Override
> public String getContentType() {
> return null;
> }
> @Override
> public byte[] getFooter() {
> return new byte[0];
> }
> @Override
> public byte[] getHeader() {
> return new byte[0];
> }
> }
> {code}
> If I run `main` (with an extra `PatternLayout`+`Console` appender in my 
> config), I see the following output:
> {code}
> 17:55:28.524 [main] INFO  com.example.logging.plugins.MyLayout - Here's a 
> thing: 75. And another: walrus
> Format: null
> Parameters: [75, walrus]
> {code}
> I had expected to be able to see the format string and the array of 
> parameters, but instead I can only see the parameters. The format string is 
> always null.
> This appears to be due to the implementation of `MutableLogEvent::setMessage` 
> highlighted [in 
> LOG4J2-1510|https://issues.apache.org/jira/browse/LOG4J2-1510]. Because the 
> messages are reusable, they appear to be eagerly formatted and the format 
> string itself is discarded. The `getFormat` method is hardcoded to return 
> `null`.
> Is this by design? If so, what is the prescribed approach to getting the 
> format string in my layout? (Preferably one that does not disable the 
> performance optimizations offered by enabling threadlocals.)
> Thanks!



--
This message was sent by Atlassian JIRA
(v6.4.14#64029)


[jira] [Commented] (LOG4J2-2118) MutableLogEvent does not retain the Message format string

2017-11-14 Thread ASF GitHub Bot (JIRA)

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

ASF GitHub Bot commented on LOG4J2-2118:


Github user zslayton commented on a diff in the pull request:

https://github.com/apache/logging-log4j2/pull/131#discussion_r150963138
  
--- Diff: 
log4j-core/src/main/java/org/apache/logging/log4j/core/impl/MutableLogEvent.java
 ---
@@ -295,7 +301,7 @@ public Message memento() {
 return message;
 }
 final Object[] params = parameters == null ? new Object[0] : 
Arrays.copyOf(parameters, parameterCount);
-return new ParameterizedMessage(messageText.toString(), params);
+return new ParameterizedMessage(getFormat(), params);
--- End diff --

The original version of this code appears to cause nested pattern 
interpretation. For example, if you created a message with the pattern `"{}"` 
and the first parameter was `"json: [{}]"`, calling `memento()` on this event 
would cause the fully formatted message (`json: [{}]`) to be treated as the 
format for the `ParameterizedMessage` being constructed here. Because it 
contains braces, the message would attempt to pull from the parameter list to 
format it.


> MutableLogEvent does not retain the Message format string
> -
>
> Key: LOG4J2-2118
> URL: https://issues.apache.org/jira/browse/LOG4J2-2118
> Project: Log4j 2
>  Issue Type: Bug
>  Components: Layouts
>Affects Versions: 2.8
> Environment: Mac OS X 10.11.6
> Java 1.8.0_141
>Reporter: Zack Slayton
>  Labels: layout
>
> I'm attempting to create a custom binary Layout that stores the provided 
> format string separate from the parameters rather than eagerly formatting 
> them all into a single piece of text. However, when I went to implement 
> `Layout::toByteArray`, I discovered that the `Message::getFormat` method 
> always returns null.
> {code:java}
> import org.apache.logging.log4j.LogManager;
> import org.apache.logging.log4j.Logger;
> import org.apache.logging.log4j.core.LogEvent;
> import org.apache.logging.log4j.core.config.plugins.Plugin;
> import org.apache.logging.log4j.core.config.plugins.PluginFactory;
> import org.apache.logging.log4j.core.layout.AbstractLayout;
> import java.io.Serializable;
> import java.util.Arrays;
> @Plugin(name = "MyLayout", category = "Core", elementType = "layout", 
> printObject = true)
> public class MyLayout extends AbstractLayout {
> public MyLayout() {
> super(null, null, null);
> }
> @PluginFactory
> public static MyLayout createLayout(){
> return new MyLayout();
> }
> public static void main(String[] args) {
> Logger log = LogManager.getLogger(MyLayout.class);
> log.info("Here's a thing: {}. And another: {}", 75, "walrus");
> }
> @Override
> public byte[] toByteArray(LogEvent event) {
> System.out.println("Format: " + event.getMessage().getFormat());
> System.out.println("Parameters: " + 
> Arrays.toString(event.getMessage().getParameters()));
> return new byte[0];
> }
> @Override
> public Serializable toSerializable(LogEvent event) {
> return null;
> }
> @Override
> public String getContentType() {
> return null;
> }
> @Override
> public byte[] getFooter() {
> return new byte[0];
> }
> @Override
> public byte[] getHeader() {
> return new byte[0];
> }
> }
> {code}
> If I run `main` (with an extra `PatternLayout`+`Console` appender in my 
> config), I see the following output:
> {code}
> 17:55:28.524 [main] INFO  com.example.logging.plugins.MyLayout - Here's a 
> thing: 75. And another: walrus
> Format: null
> Parameters: [75, walrus]
> {code}
> I had expected to be able to see the format string and the array of 
> parameters, but instead I can only see the parameters. The format string is 
> always null.
> This appears to be due to the implementation of `MutableLogEvent::setMessage` 
> highlighted [in 
> LOG4J2-1510|https://issues.apache.org/jira/browse/LOG4J2-1510]. Because the 
> messages are reusable, they appear to be eagerly formatted and the format 
> string itself is discarded. The `getFormat` method is hardcoded to return 
> `null`.
> Is this by design? If so, what is the prescribed approach to getting the 
> format string in my layout? (Preferably one that does not disable the 
> performance optimizations offered by enabling threadlocals.)
> Thanks!



--
This message was sent by Atlassian JIRA
(v6.4.14#64029)


[jira] [Commented] (LOG4J2-2118) MutableLogEvent does not retain the Message format string

2017-11-14 Thread ASF GitHub Bot (JIRA)

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

ASF GitHub Bot commented on LOG4J2-2118:


GitHub user zslayton opened a pull request:

https://github.com/apache/logging-log4j2/pull/131

[LOG4J2-2118] Modify MutableLogEvent to preserve message format string.

Addresses [LOG4J2-2118](https://issues.apache.org/jira/browse/LOG4J2-2118)

You can merge this pull request into a Git repository by running:

$ git pull https://github.com/zslayton/logging-log4j2 
mutable-log-event-format

Alternatively you can review and apply these changes as the patch at:

https://github.com/apache/logging-log4j2/pull/131.patch

To close this pull request, make a commit to your master/trunk branch
with (at least) the following in the commit message:

This closes #131


commit 8cefba464b53412f957ffb7035f8033909fafd97
Author: Zack Slayton 
Date:   2017-11-14T20:56:35Z

[LOG4J2-2118] Modify MutableLogEvent to preserve message format string.




> MutableLogEvent does not retain the Message format string
> -
>
> Key: LOG4J2-2118
> URL: https://issues.apache.org/jira/browse/LOG4J2-2118
> Project: Log4j 2
>  Issue Type: Bug
>  Components: Layouts
>Affects Versions: 2.8
> Environment: Mac OS X 10.11.6
> Java 1.8.0_141
>Reporter: Zack Slayton
>  Labels: layout
>
> I'm attempting to create a custom binary Layout that stores the provided 
> format string separate from the parameters rather than eagerly formatting 
> them all into a single piece of text. However, when I went to implement 
> `Layout::toByteArray`, I discovered that the `Message::getFormat` method 
> always returns null.
> {code:java}
> import org.apache.logging.log4j.LogManager;
> import org.apache.logging.log4j.Logger;
> import org.apache.logging.log4j.core.LogEvent;
> import org.apache.logging.log4j.core.config.plugins.Plugin;
> import org.apache.logging.log4j.core.config.plugins.PluginFactory;
> import org.apache.logging.log4j.core.layout.AbstractLayout;
> import java.io.Serializable;
> import java.util.Arrays;
> @Plugin(name = "MyLayout", category = "Core", elementType = "layout", 
> printObject = true)
> public class MyLayout extends AbstractLayout {
> public MyLayout() {
> super(null, null, null);
> }
> @PluginFactory
> public static MyLayout createLayout(){
> return new MyLayout();
> }
> public static void main(String[] args) {
> Logger log = LogManager.getLogger(MyLayout.class);
> log.info("Here's a thing: {}. And another: {}", 75, "walrus");
> }
> @Override
> public byte[] toByteArray(LogEvent event) {
> System.out.println("Format: " + event.getMessage().getFormat());
> System.out.println("Parameters: " + 
> Arrays.toString(event.getMessage().getParameters()));
> return new byte[0];
> }
> @Override
> public Serializable toSerializable(LogEvent event) {
> return null;
> }
> @Override
> public String getContentType() {
> return null;
> }
> @Override
> public byte[] getFooter() {
> return new byte[0];
> }
> @Override
> public byte[] getHeader() {
> return new byte[0];
> }
> }
> {code}
> If I run `main` (with an extra `PatternLayout`+`Console` appender in my 
> config), I see the following output:
> {code}
> 17:55:28.524 [main] INFO  com.example.logging.plugins.MyLayout - Here's a 
> thing: 75. And another: walrus
> Format: null
> Parameters: [75, walrus]
> {code}
> I had expected to be able to see the format string and the array of 
> parameters, but instead I can only see the parameters. The format string is 
> always null.
> This appears to be due to the implementation of `MutableLogEvent::setMessage` 
> highlighted [in 
> LOG4J2-1510|https://issues.apache.org/jira/browse/LOG4J2-1510]. Because the 
> messages are reusable, they appear to be eagerly formatted and the format 
> string itself is discarded. The `getFormat` method is hardcoded to return 
> `null`.
> Is this by design? If so, what is the prescribed approach to getting the 
> format string in my layout? (Preferably one that does not disable the 
> performance optimizations offered by enabling threadlocals.)
> Thanks!



--
This message was sent by Atlassian JIRA
(v6.4.14#64029)


[jira] [Commented] (LOG4J2-2118) MutableLogEvent does not retain the Message format string

2017-11-13 Thread Remko Popma (JIRA)

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

Remko Popma commented on LOG4J2-2118:
-

The eager formatting is to prevent race conditions with asynchronous logging. 
However there is no reason not to store the format string. 
Feel free to submit a patch or a GitHub pull request. 

> MutableLogEvent does not retain the Message format string
> -
>
> Key: LOG4J2-2118
> URL: https://issues.apache.org/jira/browse/LOG4J2-2118
> Project: Log4j 2
>  Issue Type: Bug
>  Components: Layouts
>Affects Versions: 2.8
> Environment: Mac OS X 10.11.6
> Java 1.8.0_141
>Reporter: Zack Slayton
>  Labels: layout
>
> I'm attempting to create a custom binary Layout that stores the provided 
> format string separate from the parameters rather than eagerly formatting 
> them all into a single piece of text. However, when I went to implement 
> `Layout::toByteArray`, I discovered that the `Message::getFormat` method 
> always returns null.
> {code:java}
> import org.apache.logging.log4j.LogManager;
> import org.apache.logging.log4j.Logger;
> import org.apache.logging.log4j.core.LogEvent;
> import org.apache.logging.log4j.core.config.plugins.Plugin;
> import org.apache.logging.log4j.core.config.plugins.PluginFactory;
> import org.apache.logging.log4j.core.layout.AbstractLayout;
> import java.io.Serializable;
> import java.util.Arrays;
> @Plugin(name = "MyLayout", category = "Core", elementType = "layout", 
> printObject = true)
> public class MyLayout extends AbstractLayout {
> public MyLayout() {
> super(null, null, null);
> }
> @PluginFactory
> public static MyLayout createLayout(){
> return new MyLayout();
> }
> public static void main(String[] args) {
> Logger log = LogManager.getLogger(MyLayout.class);
> log.info("Here's a thing: {}. And another: {}", 75, "walrus");
> }
> @Override
> public byte[] toByteArray(LogEvent event) {
> System.out.println("Format: " + event.getMessage().getFormat());
> System.out.println("Parameters: " + 
> Arrays.toString(event.getMessage().getParameters()));
> return new byte[0];
> }
> @Override
> public Serializable toSerializable(LogEvent event) {
> return null;
> }
> @Override
> public String getContentType() {
> return null;
> }
> @Override
> public byte[] getFooter() {
> return new byte[0];
> }
> @Override
> public byte[] getHeader() {
> return new byte[0];
> }
> }
> {code}
> If I run `main` (with an extra `PatternLayout`+`Console` appender in my 
> config), I see the following output:
> {code}
> 17:55:28.524 [main] INFO  com.example.logging.plugins.MyLayout - Here's a 
> thing: 75. And another: walrus
> Format: null
> Parameters: [75, walrus]
> {code}
> I had expected to be able to see the format string and the array of 
> parameters, but instead I can only see the parameters. The format string is 
> always null.
> This appears to be due to the implementation of `MutableLogEvent::setMessage` 
> highlighted [in 
> LOG4J2-1510|https://issues.apache.org/jira/browse/LOG4J2-1510]. Because the 
> messages are reusable, they appear to be eagerly formatted and the format 
> string itself is discarded. The `getFormat` method is hardcoded to return 
> `null`.
> Is this by design? If so, what is the prescribed approach to getting the 
> format string in my layout? (Preferably one that does not disable the 
> performance optimizations offered by enabling threadlocals.)
> Thanks!



--
This message was sent by Atlassian JIRA
(v6.4.14#64029)