Configure Log4J2 with properties file

When you write an application you set out to make it work. However, quite often once an application is deployed in the field you can encounter problems. Having a log file that can help you trace where the problem lies can be an invaluable tool.

In this session we are going to look at adding a log file to our application. We want to be able to run our application whilst simultaneously having it write the progress of the program into a text file.
But not just that. We want more. Each time we run our application we want the information added to the end of the file. We don’t want to over write our log file we want to append information to the end of the file.

But more than that. If we just keep doing that then our file will get huge. What we want is for our file to be archived if it gets beyond a certain size. It would also be handy to know the date and time of the change over.

And still I want more. I want to create a new file each day or hour or every x minutes. So even if the file does not get too big I want a new file say every day.

I’m not done yet. I don’t want to have log files going back 365 days. The information in those old log files will not be terribly useful. If we get a problem we will likely know about it within days, so only keep the latest x archive files

And finally! I want to change how and what we log. Should we encounter an issue in the field I want to configure logging on the specific machine so I can trace the problem. I want to do this outside the application. I don’t want to have to change the code.

Now I’m happy!

Leave a Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.