Log4j Custom Appender
Posted By admin On 01.06.20In log4j2, you would create a plugin for create custom appender in log4j2. When you annotate your custom Appender class with @Plugin(name=”MyCustomAppender”, category=”core” elementType=”appender”,printObject=true) the plugin name becomes the configuration element name, so a configuration with your custom appender would then look like this. I had a recent task where I wanted to log events in a section of our code to our content management system. I leveraged log4net, created a custom appender, and was logging successfully in no time. I used this detailed tutorial to get a better grasp on log4net and I would consider reading it a.
My class code is as below.
And my configuration of log4j.properties are as follows.
This is creating a log for different types, for example, DEBUG, ERROR, and INFO in different log files. But what limitation is it? It is creating larger and larger log files. I want to make log files for, say, of 5 MB, and previous logs should be removed. How can I do that? When I try with RollingFile Appender, I get the below log files only.
Rolling of log files ERROR
,DEBUG
is not done, but INFO
is done.
Log4j Custom Appender Spring
Bhavik AmbaniBhavik Ambani1 Answer
I suggest you derive from RollingFileAppender instead of FileAppender. This will give you the possibility to define how large the log-files will grow, and how many 'old' ones you want to keep. Check out the manual on how to use it later in your log4j.propertiers.
If I understand correctly, you want one file per log-level, is that correct? If so, I suggest you follow this FAQ-Entry rather than 'rolling' your own solution :)
Peter Mortensen