From STDOUT to Logging
Remember the days we debug Java programs with System.out.println(error) inside an error handling block? Recording such event occurrences and run-time information to the console or a file is very important in order to monitor and analyse the programs we write. This concept of recording the occurrences of run-time events of a software is called logging. Although you can have your own implementations to record these information programmatically to the console/file, it will consume time and resources from your main program. Also it will be a bottleneck when the number of records increases.
This is where the logging tools and frameworks come into the scenario. Without using our own implementations, we can use the logging frameworks which are lighter, faster, and easier in comparison to our implementations.
Characteristics of a good Logging framework
Be fast, efficient, and optimum — In a practical environment, there can be situations where the program generates hundreds or thousands of logging messages within a second and the logger should be able to handle them all balancing logging speed and buffering amounts at an optimum level.
Consume less memory
Support standard logging levels (ERROR, WARN, INFO, DEBUG, TRACE)
Support different destination formats (console, text file, HTML doc, database, SMTP server, or JMS queue destination)
Support flexible file rolling
Logback is a logging framework which satisfies all above criteria and can be recognized as the best logging framework available for Java programs. Now we will see the features of Logback by implementing a sample Maven project in IntelliJ IDEA IDE.
Step 1: Create a Maven project
Create a new Maven project from scratch and add the following dependencies and plugins to the pom.xml file. Choose ‘Enable Auto-Import’ Maven projects prompt at the beginning (to manually enable this, go to File > Settings > search and select ‘Maven’ > choose ‘Importing’ > enable ‘Import Maven projects automatically’).