What You'll Learn

Writing log statements in your code is important.

When combined with metrics and tracing, the three pillars of observability are complete. Do some background reading on this. It is not expected within this module, but it will be expected within industry when code is deployed into production, looked after 24⁄7 by others, could be business-critical and could be part of a much larger system of interconnected systems.

To include logging capability on a class, we need to add a Lombok annotation to the class.

In CharityAdminController.

@Controller
@RequestMapping("/admin")
@Slf4j
public class CharityAdminController {

We've introduced @Slf4j. Slf4j is the "Standard Logging Facade for Java". It wraps a number of different logging frameworks with a standard interface.

By adding this annotation, your class now has access to a Logger object accessed via variable log.

We can now write log messages at any point in our code. For example, after validating a form, we could introduce logging thus:

       if(bindingResult.hasErrors()){
        log.debug("THERE ARE ERRORS"+bindingResult.getAllErrors());
        return"admin/charity-form";
        }

The Logger has a number of methods that relate to the logging level. Which log messages are recorded can be controlled. Too much logging can slow down a program, but too little can make it difficult to diagnose issues and recover from them quickly.

Log levels are typically INFO, WARNING, ERROR, DEBUG, etc.

The log methods take a string as the data to log. The Logger will timestamp the record and record the level. The logging frameworks allow the developer to choose the format of the logging record.

Learning more about the additional features of the logging framework are left as an exercise for the student.

Lombok makes adding logging very easy. There are other elements to full logging, but using it (as opposed to System.out.println statements) is preferred.