Fwd: Add json object to log4j2 when is JSONLayout

classic Classic list List threaded Threaded
8 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Fwd: Add json object to log4j2 when is JSONLayout

Jeus Geek
i will show JSON object as a JSON object in log4j2 when is that configed
JSONLayout .

JSON object:

{"line_id": 12,"play_name":"Jeus"}

import org.apache.logging.log4j.LogManager;import
org.apache.logging.log4j.Logger;
public class Main {

       private static final Logger LOGGER = LogManager.getLogger(Main.class);

       public static void main(String[] args) {


            String message = "{\"line_id\": 12,\"play_name\": \"Jeus\"}";
            LOGGER.info(message);

        }
 }

output is:

{
 "timeMillis":1502361394238,
 "thread":"main",
 "level":"INFO",
 "loggerName":"com.jeus.logger.json.loggerjson.Main",
 "message":"{\"line_id\": 12,\"play_name\": \"Jeus\"}",
 "endOfBatch":false,
 "loggerFqcn":"org.apache.logging.log4j.spi.AbstractLogger",
 "contextMap":[

 ],
 "threadId":1,
 "threadPriority":5,
 "source":{
    "class":"com.jeus.logger.json.loggerjson.Main",
    "method":"main",
    "file":"Main.java",
    "line":62
    }
 }

but i will show message as a json object same this:

 "message":{"line_id": 12,"play_name":"Jeus"},

you can see more detail about my problem in stackOverflow
<https://stackoverflow.com/questions/45611509/add-json-object-to-log4j2-when-is-jsonlayout?noredirect=1#comment78217774_45611509>


thank you for attention
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Fwd: Add json object to log4j2 when is JSONLayout

Gary Gregory-4
Hello,

The JSON layout outputs the log event in JSON, the event message is just a
string in that event, in this case, it is escaped following the JSON rules
to remain a string.

If you want a JSON object back out of the event message you will have to
parse it out of that string.

Feel free to provide a PR on GitHub if you want to add an option to allow
for a JSON object as the log message. I am not sure if it would be accepted
though as it is quite different from the current design.

Thoughts from the community?

Gary


On Aug 11, 2017 07:53, "Jeus Geek" <[hidden email]> wrote:

i will show JSON object as a JSON object in log4j2 when is that configed
JSONLayout .

JSON object:

{"line_id": 12,"play_name":"Jeus"}

import org.apache.logging.log4j.LogManager;import
org.apache.logging.log4j.Logger;
public class Main {

       private static final Logger LOGGER = LogManager.getLogger(Main.
class);

       public static void main(String[] args) {


            String message = "{\"line_id\": 12,\"play_name\": \"Jeus\"}";
            LOGGER.info(message);

        }
 }

output is:

{
 "timeMillis":1502361394238,
 "thread":"main",
 "level":"INFO",
 "loggerName":"com.jeus.logger.json.loggerjson.Main",
 "message":"{\"line_id\": 12,\"play_name\": \"Jeus\"}",
 "endOfBatch":false,
 "loggerFqcn":"org.apache.logging.log4j.spi.AbstractLogger",
 "contextMap":[

 ],
 "threadId":1,
 "threadPriority":5,
 "source":{
    "class":"com.jeus.logger.json.loggerjson.Main",
    "method":"main",
    "file":"Main.java",
    "line":62
    }
 }

but i will show message as a json object same this:

 "message":{"line_id": 12,"play_name":"Jeus"},

you can see more detail about my problem in stackOverflow
<https://stackoverflow.com/questions/45611509/add-json-
object-to-log4j2-when-is-jsonlayout?noredirect=1#comment78217774_45611509>


thank you for attention
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Fwd: Add json object to log4j2 when is JSONLayout

Jeus Geek
Hello

Thank you for your response..

I don't understand what "PR" means, but I think your ​purpose was
"property".

However, my idea is to enable adding a property, such that special
characters will not be replaced after configuration.

I believe many others like me would like to have the flexibility to create
optional messages.

I try to change code in github and add this option  "jsonMessage", such
that if it was true, the special characters will not be replaced in message.

​​

Jeus




On Fri, Aug 11, 2017 at 7:26 PM, Gary Gregory <[hidden email]>
wrote:

> Hello,
>
> The JSON layout outputs the log event in JSON, the event message is just a
> string in that event, in this case, it is escaped following the JSON rules
> to remain a string.
>
> If you want a JSON object back out of the event message you will have to
> parse it out of that string.
>
> Feel free to provide a PR on GitHub if you want to add an option to allow
> for a JSON object as the log message. I am not sure if it would be accepted
> though as it is quite different from the current design.
>
> Thoughts from the community?
>
> Gary
>
>
> On Aug 11, 2017 07:53, "Jeus Geek" <[hidden email]> wrote:
>
> i will show JSON object as a JSON object in log4j2 when is that configed
> JSONLayout .
>
> JSON object:
>
> {"line_id": 12,"play_name":"Jeus"}
>
> import org.apache.logging.log4j.LogManager;import
> org.apache.logging.log4j.Logger;
> public class Main {
>
>        private static final Logger LOGGER = LogManager.getLogger(Main.
> class);
>
>        public static void main(String[] args) {
>
>
>             String message = "{\"line_id\": 12,\"play_name\": \"Jeus\"}";
>             LOGGER.info(message);
>
>         }
>  }
>
> output is:
>
> {
>  "timeMillis":1502361394238,
>  "thread":"main",
>  "level":"INFO",
>  "loggerName":"com.jeus.logger.json.loggerjson.Main",
>  "message":"{\"line_id\": 12,\"play_name\": \"Jeus\"}",
>  "endOfBatch":false,
>  "loggerFqcn":"org.apache.logging.log4j.spi.AbstractLogger",
>  "contextMap":[
>
>  ],
>  "threadId":1,
>  "threadPriority":5,
>  "source":{
>     "class":"com.jeus.logger.json.loggerjson.Main",
>     "method":"main",
>     "file":"Main.java",
>     "line":62
>     }
>  }
>
> but i will show message as a json object same this:
>
>  "message":{"line_id": 12,"play_name":"Jeus"},
>
> you can see more detail about my problem in stackOverflow
> <https://stackoverflow.com/questions/45611509/add-json-
> object-to-log4j2-when-is-jsonlayout?noredirect=1#comment78217774_45611509>
>
>
> thank you for attention
>
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Fwd: Add json object to log4j2 when is JSONLayout

Matt Sicker
PR is pull request, as in a GitHub pull request. A patch file uploaded to
JIRA also works.

On 11 August 2017 at 11:03, Jeus Geek <[hidden email]> wrote:

> Hello
>
> Thank you for your response..
>
> I don't understand what "PR" means, but I think your ​purpose was
> "property".
>
> However, my idea is to enable adding a property, such that special
> characters will not be replaced after configuration.
>
> I believe many others like me would like to have the flexibility to create
> optional messages.
>
> I try to change code in github and add this option  "jsonMessage", such
> that if it was true, the special characters will not be replaced in
> message.
>
> ​​
>
> Jeus
> ​
> ​
>
>
> On Fri, Aug 11, 2017 at 7:26 PM, Gary Gregory <[hidden email]>
> wrote:
>
> > Hello,
> >
> > The JSON layout outputs the log event in JSON, the event message is just
> a
> > string in that event, in this case, it is escaped following the JSON
> rules
> > to remain a string.
> >
> > If you want a JSON object back out of the event message you will have to
> > parse it out of that string.
> >
> > Feel free to provide a PR on GitHub if you want to add an option to allow
> > for a JSON object as the log message. I am not sure if it would be
> accepted
> > though as it is quite different from the current design.
> >
> > Thoughts from the community?
> >
> > Gary
> >
> >
> > On Aug 11, 2017 07:53, "Jeus Geek" <[hidden email]> wrote:
> >
> > i will show JSON object as a JSON object in log4j2 when is that configed
> > JSONLayout .
> >
> > JSON object:
> >
> > {"line_id": 12,"play_name":"Jeus"}
> >
> > import org.apache.logging.log4j.LogManager;import
> > org.apache.logging.log4j.Logger;
> > public class Main {
> >
> >        private static final Logger LOGGER = LogManager.getLogger(Main.
> > class);
> >
> >        public static void main(String[] args) {
> >
> >
> >             String message = "{\"line_id\": 12,\"play_name\": \"Jeus\"}";
> >             LOGGER.info(message);
> >
> >         }
> >  }
> >
> > output is:
> >
> > {
> >  "timeMillis":1502361394238,
> >  "thread":"main",
> >  "level":"INFO",
> >  "loggerName":"com.jeus.logger.json.loggerjson.Main",
> >  "message":"{\"line_id\": 12,\"play_name\": \"Jeus\"}",
> >  "endOfBatch":false,
> >  "loggerFqcn":"org.apache.logging.log4j.spi.AbstractLogger",
> >  "contextMap":[
> >
> >  ],
> >  "threadId":1,
> >  "threadPriority":5,
> >  "source":{
> >     "class":"com.jeus.logger.json.loggerjson.Main",
> >     "method":"main",
> >     "file":"Main.java",
> >     "line":62
> >     }
> >  }
> >
> > but i will show message as a json object same this:
> >
> >  "message":{"line_id": 12,"play_name":"Jeus"},
> >
> > you can see more detail about my problem in stackOverflow
> > <https://stackoverflow.com/questions/45611509/add-json-
> > object-to-log4j2-when-is-jsonlayout?noredirect=1#
> comment78217774_45611509>
> >
> >
> > thank you for attention
> >
>



--
Matt Sicker <[hidden email]>
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Fwd: Add json object to log4j2 when is JSONLayout

Gary Gregory-4
On Fri, Aug 11, 2017 at 10:12 AM, Matt Sicker <[hidden email]> wrote:

> PR is pull request, as in a GitHub pull request. A patch file uploaded to
> JIRA also works.
>

With a unit test :-)

Gary


>
> On 11 August 2017 at 11:03, Jeus Geek <[hidden email]> wrote:
>
> > Hello
> >
> > Thank you for your response..
> >
> > I don't understand what "PR" means, but I think your ​purpose was
> > "property".
> >
> > However, my idea is to enable adding a property, such that special
> > characters will not be replaced after configuration.
> >
> > I believe many others like me would like to have the flexibility to
> create
> > optional messages.
> >
> > I try to change code in github and add this option  "jsonMessage", such
> > that if it was true, the special characters will not be replaced in
> > message.
> >
> > ​​
> >
> > Jeus
> > ​
> > ​
> >
> >
> > On Fri, Aug 11, 2017 at 7:26 PM, Gary Gregory <[hidden email]>
> > wrote:
> >
> > > Hello,
> > >
> > > The JSON layout outputs the log event in JSON, the event message is
> just
> > a
> > > string in that event, in this case, it is escaped following the JSON
> > rules
> > > to remain a string.
> > >
> > > If you want a JSON object back out of the event message you will have
> to
> > > parse it out of that string.
> > >
> > > Feel free to provide a PR on GitHub if you want to add an option to
> allow
> > > for a JSON object as the log message. I am not sure if it would be
> > accepted
> > > though as it is quite different from the current design.
> > >
> > > Thoughts from the community?
> > >
> > > Gary
> > >
> > >
> > > On Aug 11, 2017 07:53, "Jeus Geek" <[hidden email]> wrote:
> > >
> > > i will show JSON object as a JSON object in log4j2 when is that
> configed
> > > JSONLayout .
> > >
> > > JSON object:
> > >
> > > {"line_id": 12,"play_name":"Jeus"}
> > >
> > > import org.apache.logging.log4j.LogManager;import
> > > org.apache.logging.log4j.Logger;
> > > public class Main {
> > >
> > >        private static final Logger LOGGER = LogManager.getLogger(Main.
> > > class);
> > >
> > >        public static void main(String[] args) {
> > >
> > >
> > >             String message = "{\"line_id\": 12,\"play_name\":
> \"Jeus\"}";
> > >             LOGGER.info(message);
> > >
> > >         }
> > >  }
> > >
> > > output is:
> > >
> > > {
> > >  "timeMillis":1502361394238,
> > >  "thread":"main",
> > >  "level":"INFO",
> > >  "loggerName":"com.jeus.logger.json.loggerjson.Main",
> > >  "message":"{\"line_id\": 12,\"play_name\": \"Jeus\"}",
> > >  "endOfBatch":false,
> > >  "loggerFqcn":"org.apache.logging.log4j.spi.AbstractLogger",
> > >  "contextMap":[
> > >
> > >  ],
> > >  "threadId":1,
> > >  "threadPriority":5,
> > >  "source":{
> > >     "class":"com.jeus.logger.json.loggerjson.Main",
> > >     "method":"main",
> > >     "file":"Main.java",
> > >     "line":62
> > >     }
> > >  }
> > >
> > > but i will show message as a json object same this:
> > >
> > >  "message":{"line_id": 12,"play_name":"Jeus"},
> > >
> > > you can see more detail about my problem in stackOverflow
> > > <https://stackoverflow.com/questions/45611509/add-json-
> > > object-to-log4j2-when-is-jsonlayout?noredirect=1#
> > comment78217774_45611509>
> > >
> > >
> > > thank you for attention
> > >
> >
>
>
>
> --
> Matt Sicker <[hidden email]>
>
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Fwd: Add json object to log4j2 when is JSONLayout

Mikael Ståldal-4
In reply to this post by Gary Gregory-4
Note that if we add such option to JsonLayout, then all log messages
passed to that layout need to be well-formed JSON, otherwise the output
will not be well-formed JSON. We would make it easy for the user to
shoot himself in the foot.

I think it would be better to create a new subclass of Message,
JsonMessage, and let JsonLayout handle such messages specially and
output as a nested JSON structure (instead as a quoted and escaped
string as today).

However, how do we represent a JSON structure without adding dependency
on Jackson or other 3rd party JSON library? Message subcasses are in
log4j-api, and we do not want log4j-api to depend on any such library.
Or maybe we should put this new JsonMessage in log4j-core? Or maybe put
it in a new log4j-json module?

Should we also have XmlMessage for XmlLayout?

What about YamlLayout?


On 2017-08-11 16:56, Gary Gregory wrote:

> Hello,
>
> The JSON layout outputs the log event in JSON, the event message is just a
> string in that event, in this case, it is escaped following the JSON rules
> to remain a string.
>
> If you want a JSON object back out of the event message you will have to
> parse it out of that string.
>
> Feel free to provide a PR on GitHub if you want to add an option to allow
> for a JSON object as the log message. I am not sure if it would be accepted
> though as it is quite different from the current design.
>
> Thoughts from the community?
>
> Gary
>
>
> On Aug 11, 2017 07:53, "Jeus Geek" <[hidden email]> wrote:
>
> i will show JSON object as a JSON object in log4j2 when is that configed
> JSONLayout .
>
> JSON object:
>
> {"line_id": 12,"play_name":"Jeus"}
>
> import org.apache.logging.log4j.LogManager;import
> org.apache.logging.log4j.Logger;
> public class Main {
>
>         private static final Logger LOGGER = LogManager.getLogger(Main.
> class);
>
>         public static void main(String[] args) {
>
>
>              String message = "{\"line_id\": 12,\"play_name\": \"Jeus\"}";
>              LOGGER.info(message);
>
>          }
>   }
>
> output is:
>
> {
>   "timeMillis":1502361394238,
>   "thread":"main",
>   "level":"INFO",
>   "loggerName":"com.jeus.logger.json.loggerjson.Main",
>   "message":"{\"line_id\": 12,\"play_name\": \"Jeus\"}",
>   "endOfBatch":false,
>   "loggerFqcn":"org.apache.logging.log4j.spi.AbstractLogger",
>   "contextMap":[
>
>   ],
>   "threadId":1,
>   "threadPriority":5,
>   "source":{
>      "class":"com.jeus.logger.json.loggerjson.Main",
>      "method":"main",
>      "file":"Main.java",
>      "line":62
>      }
>   }
>
> but i will show message as a json object same this:
>
>   "message":{"line_id": 12,"play_name":"Jeus"},
>
> you can see more detail about my problem in stackOverflow
> <https://stackoverflow.com/questions/45611509/add-json-
> object-to-log4j2-when-is-jsonlayout?noredirect=1#comment78217774_45611509>
>
>
> thank you for attention
>


---------------------------------------------------------------------
To unsubscribe, e-mail: [hidden email]
For additional commands, e-mail: [hidden email]

Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Fwd: Add json object to log4j2 when is JSONLayout

Gary Gregory-4
Hi All,

Let's find out what the use-case is from the user first. If the JSON is
generated on the fly or without a backing model, it may not be possible to
use Jackson. Also you are assuming the user is also using Jackson, which
may not be the case.

Gary

On Fri, Aug 11, 2017 at 1:39 PM, Mikael Ståldal <[hidden email]> wrote:

> Note that if we add such option to JsonLayout, then all log messages
> passed to that layout need to be well-formed JSON, otherwise the output
> will not be well-formed JSON. We would make it easy for the user to shoot
> himself in the foot.
>
> I think it would be better to create a new subclass of Message,
> JsonMessage, and let JsonLayout handle such messages specially and output
> as a nested JSON structure (instead as a quoted and escaped string as
> today).
>
> However, how do we represent a JSON structure without adding dependency on
> Jackson or other 3rd party JSON library? Message subcasses are in
> log4j-api, and we do not want log4j-api to depend on any such library. Or
> maybe we should put this new JsonMessage in log4j-core? Or maybe put it in
> a new log4j-json module?
>
> Should we also have XmlMessage for XmlLayout?
>
> What about YamlLayout?
>
>
> On 2017-08-11 16:56, Gary Gregory wrote:
>
>> Hello,
>>
>> The JSON layout outputs the log event in JSON, the event message is just a
>> string in that event, in this case, it is escaped following the JSON rules
>> to remain a string.
>>
>> If you want a JSON object back out of the event message you will have to
>> parse it out of that string.
>>
>> Feel free to provide a PR on GitHub if you want to add an option to allow
>> for a JSON object as the log message. I am not sure if it would be
>> accepted
>> though as it is quite different from the current design.
>>
>> Thoughts from the community?
>>
>> Gary
>>
>>
>> On Aug 11, 2017 07:53, "Jeus Geek" <[hidden email]> wrote:
>>
>> i will show JSON object as a JSON object in log4j2 when is that configed
>> JSONLayout .
>>
>> JSON object:
>>
>> {"line_id": 12,"play_name":"Jeus"}
>>
>> import org.apache.logging.log4j.LogManager;import
>> org.apache.logging.log4j.Logger;
>> public class Main {
>>
>>         private static final Logger LOGGER = LogManager.getLogger(Main.
>> class);
>>
>>         public static void main(String[] args) {
>>
>>
>>              String message = "{\"line_id\": 12,\"play_name\": \"Jeus\"}";
>>              LOGGER.info(message);
>>
>>          }
>>   }
>>
>> output is:
>>
>> {
>>   "timeMillis":1502361394238,
>>   "thread":"main",
>>   "level":"INFO",
>>   "loggerName":"com.jeus.logger.json.loggerjson.Main",
>>   "message":"{\"line_id\": 12,\"play_name\": \"Jeus\"}",
>>   "endOfBatch":false,
>>   "loggerFqcn":"org.apache.logging.log4j.spi.AbstractLogger",
>>   "contextMap":[
>>
>>   ],
>>   "threadId":1,
>>   "threadPriority":5,
>>   "source":{
>>      "class":"com.jeus.logger.json.loggerjson.Main",
>>      "method":"main",
>>      "file":"Main.java",
>>      "line":62
>>      }
>>   }
>>
>> but i will show message as a json object same this:
>>
>>   "message":{"line_id": 12,"play_name":"Jeus"},
>>
>> you can see more detail about my problem in stackOverflow
>> <https://stackoverflow.com/questions/45611509/add-json-
>> object-to-log4j2-when-is-jsonlayout?noredirect=1#comment7821
>> 7774_45611509>
>>
>>
>> thank you for attention
>>
>>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: [hidden email]
> For additional commands, e-mail: [hidden email]
>
>
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Fwd: Add json object to log4j2 when is JSONLayout

Jeus Geek
In reply to this post by Gary Gregory-4
​​

certainly :D

On Fri, Aug 11, 2017 at 10:40 PM, Gary Gregory <[hidden email]>
wrote:

> On Fri, Aug 11, 2017 at 10:12 AM, Matt Sicker <[hidden email]> wrote:
>
> > PR is pull request, as in a GitHub pull request. A patch file uploaded to
> > JIRA also works.
> >
>
> With a unit test :-)
>
> Gary
>
>
> >
> > On 11 August 2017 at 11:03, Jeus Geek <[hidden email]> wrote:
> >
> > > Hello
> > >
> > > Thank you for your response..
> > >
> > > I don't understand what "PR" means, but I think your ​purpose was
> > > "property".
> > >
> > > However, my idea is to enable adding a property, such that special
> > > characters will not be replaced after configuration.
> > >
> > > I believe many others like me would like to have the flexibility to
> > create
> > > optional messages.
> > >
> > > I try to change code in github and add this option  "jsonMessage", such
> > > that if it was true, the special characters will not be replaced in
> > > message.
> > >
> > > ​​
> > >
> > > Jeus
> > > ​
> > > ​
> > >
> > >
> > > On Fri, Aug 11, 2017 at 7:26 PM, Gary Gregory <[hidden email]>
> > > wrote:
> > >
> > > > Hello,
> > > >
> > > > The JSON layout outputs the log event in JSON, the event message is
> > just
> > > a
> > > > string in that event, in this case, it is escaped following the JSON
> > > rules
> > > > to remain a string.
> > > >
> > > > If you want a JSON object back out of the event message you will have
> > to
> > > > parse it out of that string.
> > > >
> > > > Feel free to provide a PR on GitHub if you want to add an option to
> > allow
> > > > for a JSON object as the log message. I am not sure if it would be
> > > accepted
> > > > though as it is quite different from the current design.
> > > >
> > > > Thoughts from the community?
> > > >
> > > > Gary
> > > >
> > > >
> > > > On Aug 11, 2017 07:53, "Jeus Geek" <[hidden email]> wrote:
> > > >
> > > > i will show JSON object as a JSON object in log4j2 when is that
> > configed
> > > > JSONLayout .
> > > >
> > > > JSON object:
> > > >
> > > > {"line_id": 12,"play_name":"Jeus"}
> > > >
> > > > import org.apache.logging.log4j.LogManager;import
> > > > org.apache.logging.log4j.Logger;
> > > > public class Main {
> > > >
> > > >        private static final Logger LOGGER =
> LogManager.getLogger(Main.
> > > > class);
> > > >
> > > >        public static void main(String[] args) {
> > > >
> > > >
> > > >             String message = "{\"line_id\": 12,\"play_name\":
> > \"Jeus\"}";
> > > >             LOGGER.info(message);
> > > >
> > > >         }
> > > >  }
> > > >
> > > > output is:
> > > >
> > > > {
> > > >  "timeMillis":1502361394238,
> > > >  "thread":"main",
> > > >  "level":"INFO",
> > > >  "loggerName":"com.jeus.logger.json.loggerjson.Main",
> > > >  "message":"{\"line_id\": 12,\"play_name\": \"Jeus\"}",
> > > >  "endOfBatch":false,
> > > >  "loggerFqcn":"org.apache.logging.log4j.spi.AbstractLogger",
> > > >  "contextMap":[
> > > >
> > > >  ],
> > > >  "threadId":1,
> > > >  "threadPriority":5,
> > > >  "source":{
> > > >     "class":"com.jeus.logger.json.loggerjson.Main",
> > > >     "method":"main",
> > > >     "file":"Main.java",
> > > >     "line":62
> > > >     }
> > > >  }
> > > >
> > > > but i will show message as a json object same this:
> > > >
> > > >  "message":{"line_id": 12,"play_name":"Jeus"},
> > > >
> > > > you can see more detail about my problem in stackOverflow
> > > > <https://stackoverflow.com/questions/45611509/add-json-
> > > > object-to-log4j2-when-is-jsonlayout?noredirect=1#
> > > comment78217774_45611509>
> > > >
> > > >
> > > > thank you for attention
> > > >
> > >
> >
> >
> >
> > --
> > Matt Sicker <[hidden email]>
> >
>
Loading...