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?
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).
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.
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.
Yes, log4j is thread-safe. Log4j components are designed to be used in heavily multithreaded systems.
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:
logger.info(LogEntry)
LogEntry
to String
BinaryFormatAppender
I don't know of any ready made solution for this.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With