0

I am designing a custom Appender(How to create a own Appender in log4j?) in Log4j to meet my requirement.

The log files on which I have to do some operation is of the form

{JSONObject}
{JSONObject}
...........
...........
{JSONObject}

Each JSON Object is in the form of

{"operation":"ABC","operation":"xyz","ID":"123".......}

ID in each Object is unique.

I have created my custom Appender. In append(LoggingEvent event) function, How should I deal with event such that I can easily deal with JSON Object ??

I want to segregate each JSON Object.

I came across with an Idea that I will format the event in Strings and at end of each line I will change into Strings.

protected void append(LoggingEvent loggingEvent) {
        changetoJSON(this.layout.format(loggingEvent));

In changetoJSON function I will check for "\n" and convert the string till "\n" to a JSONObject. and save the corresponding index and again do the same thing till end.

Here, In this method my doubt is How does LoggingEvent deals with log files?

Does It take the whole log file in a single line ?

Is there any other way to deal with this situation ?

As I am new to log4j so I don't have too much idea of different functions of log4j. So any help in designing solution for this situation will be quite helpful for me.I am using JAVA for coding.

Thanks,

Community
  • 1
  • 1
Geek_To_Learn
  • 1,816
  • 5
  • 28
  • 48
  • are you trying to read the log file or write a log file? – shlomi33 Apr 20 '15 at 07:09
  • The logging event contains what the user asked to log. You just need to convert this information to a JSON string, and write this JSON string to wherever your appender chooses to write, followed by an EOL. What exactly is the problem? – JB Nizet Apr 20 '15 at 07:15
  • @sholmi33 : I am trying to read a log file – Geek_To_Learn Apr 20 '15 at 07:27
  • @JB Nizet : I am getting a log file which I need to read and again do some operation. Log files are inthe form of combination of JSONObjects so what I was asking if I will directly format this to String then how will I be able to segregate different JSONObjects ?? – Geek_To_Learn Apr 20 '15 at 07:30
  • @JB Nizet : can you please help me how to change logging event to JSON Strings ?? – Geek_To_Learn Apr 20 '15 at 07:31
  • Do you need to read or to write to a logfile? or do you want to modify the log output format in order to be able to read json from it? – T.Gounelle Apr 20 '15 at 09:30
  • @Abbe Resina : I want to read the logfile which is specified format as {jsonobject}{}{}...{} and further I want to segregate jsonObjects and save them somewhere . – Geek_To_Learn Apr 20 '15 at 10:07
  • So you don't want to create your own appender at all. You want to read a file containing JSON. – JB Nizet Apr 20 '15 at 11:21
  • No, I want to read that log file using a custom appender. – Geek_To_Learn Apr 20 '15 at 11:53
  • 1
    Definition of [appender](http://www.wordreference.com/definition/appender) in english: "to add as a piece at the end of a writing". If you need to read some json stuff, you don't need an _appender_. – T.Gounelle Apr 20 '15 at 12:26

0 Answers0