Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Log4j has no support for binary logging format?

Tags:

java

log4j

I have a need to have Log4j log into a binary format so the logs can be easily machine-interpreted. I know I could use XML for this purpose but don't want the associated file size bloat or parsing overhead.

The whole Layout system seems to be inherently String based, which means I can't use the nice file-rolling appenders. LoggingEvent is serializable, so I'm thinking of writing a binary file appender myself, but I feel like I shouldn't be treading new ground here.

Am I missing something obvious?

like image 910
CarlG Avatar asked Apr 11 '11 18:04

CarlG


People also ask

How does Log4j logging work?

Log4j allows logged messages to contain format strings that reference external information through the Java Naming and Directory Interface (JNDI). This allows information to be remotely retrieved across a variety of protocols, including the Lightweight Directory Access Protocol (LDAP).

What is the difference between Log4j and log4j2?

Community support: Log4j 1. x is not actively maintained, whereas Log4j 2 has an active community where questions are answered, features are added and bugs are fixed. Automatically reload its configuration upon modification without losing log events while reconfiguring.

What is Log4j file used for?

The log4j. properties file is a log4j configuration file which stores properties in key-value pairs. The log4j properties file contains the entire runtime configuration used by log4j. This file will contain log4j appenders information, log level information and output file names for file appenders.

Is Log4j thread safe?

Yes, log4j is thread-safe. Log4j components are designed to be used in heavily multithreaded systems.


1 Answers

I had a very similar problem recently. I had to write the log line to binary format but also wanted to have all the other appenders work normally.

What I did was implement my own appender which extends the AppenderSkeleton and takes the LoggingEvent object where the message returned by getMessage() is my own object.

I also write a class implementing ObjectRenderer which a Layout class of the appender will call in order to transform the logging object (the one I logged) to string.

Then for my own appender (which doesn't have a layout) the message is serialized into binary form and written to some byte stream. For other appenders the layout object calls my object renderer and the message is serialized to String.

In this way all common appenders work the same and I'm still able to append to my own format.

In Summary:

  • Write own logging object used this way: logger.info(LogEntry)
  • Implement ObjectRenderer to transform LogEntry to String
  • Extend AppenderSkeleton with my own BinaryFormatAppender

I don't know of any ready made solution for this.

like image 148
Asaf Avatar answered Sep 17 '22 16:09

Asaf