Im creating a log file system. the log file will be in json format so a server can read it after but i dont think thats too important. What i need to know is can log4j be configured to write into to a file but without any tags like info,debug, timestamp etc in the file. I have looked here
but this polutes the file with with other things. I want ONLY the data i write to show up in the file. I'd also like to set some kind of file rotation on the file if it gets too big after a max size is reached.
You should be able to write a custom log appender, I have a vague recollection of there being an example to do this with MongoDB. MongoDD stores its data as JSON, so it have converted the log to json format before inserting it.
I would start by searching for a MondgoDB appender, and looking at the file appender that is shipped, that should give you a starting point for an appender if one doesnt already exist.
http://log4mongo.org/display/PUB/Home
What you want is the PatternLayout with %m%n only, and combine with the answer to previously asked question here
This is relatively easy, using a
log4j.properties
configuration file (place it at the top of your classpath, and Log4j will 'just find it'):All the classes in the
com.foo.bar
package (and subpackages) will log to console, those incom.example
(and below) will log to/var/log/generic.log
.To emit JSON, just use
Jackson
(com.fasterxml
) convert your data to a JSON object and write it out as a string.