Spring Boot Logging and Exception Handling Essentials Quiz Quiz

Assess your understanding of Spring Boot logging mechanisms and exception handling best practices. Improve your foundational knowledge on configuring log levels, handling errors, and ensuring effective application debugging in Spring Boot projects.

  1. Logging Levels

    Which logging level should you use in Spring Boot to indicate an error event that might still allow the application to continue running?

    1. WARN
    2. TRACE
    3. ERROR
    4. FATAL

    Explanation: The ERROR logging level is intended for error events that could allow the application to continue running. FATAL is not a standard Spring Boot logging level. WARN is used for potentially harmful situations, while TRACE is for finer-grained informational events. Using ERROR helps highlight critical issues without stopping the application.

  2. Default Logging Framework

    By default, which logging framework does Spring Boot use when no custom configuration is present?

    1. Log4j2
    2. Java Util Logging
    3. Commons Logging
    4. Logback

    Explanation: Spring Boot uses Logback as its default logging framework when you do not provide any custom configuration. Log4j2 and Java Util Logging require additional setup. Commons Logging is used for logging abstraction, not as an implementation. Logback is chosen for its versatility and performance.

  3. Enabling Debug Output

    How can you enable debug logging output in a Spring Boot application using the application.properties file?

    1. debug=true
    2. logging.level.root=DEBUG
    3. log.debug.enable=true
    4. root.debug=on

    Explanation: Setting logging.level.root=DEBUG in the application.properties file sets the logging level for the root logger to DEBUG. The option debug=true enables additional diagnostic output but not standard debug logs. The last two options are not recognized Spring Boot properties; therefore, only logging.level.root=DEBUG is correct.

  4. Custom Exception Handling Annotation

    Which annotation should you place on a method to handle exceptions globally in a Spring Boot REST controller?

    1. @CatchException
    2. @ErrorCatch
    3. @ExceptionHandler
    4. @HandleError

    Explanation: @ExceptionHandler allows you to specify a method to handle specific exceptions in Spring controllers. The other options, such as @ErrorCatch, @HandleError, and @CatchException, are not standard Spring annotations and will not have the intended effect. Placing @ExceptionHandler on a method ensures correct exception handling.

  5. Exception Response Status

    How can you specify an HTTP status code for a custom exception in a Spring Boot application?

    1. @HttpStatus
    2. @StatusCode
    3. @ResponseStatus
    4. @SetStatus

    Explanation: Annotating a custom exception class with @ResponseStatus sets the HTTP status code returned when the exception is thrown. The other options, like @StatusCode, @SetStatus, and @HttpStatus, are not valid Spring annotations, so they won't work. @ResponseStatus is the correct way to manage HTTP error responses.

  6. Log File Location

    What is the default location for the log file when you set logging.file.name=app.log in the application.properties file?

    1. logs directory
    2. src/main/resources
    3. Current working directory
    4. user/home/app

    Explanation: When you specify logging.file.name=app.log, the file is created in the current working directory by default. src/main/resources is used for resource files, not log files. The logs directory is not the default location unless specified. user/home/app is not a standard location for log files.

  7. Handling Specific Exception Types

    If you want to handle only NullPointerExceptions in a controller advice, what should you specify in the @ExceptionHandler annotation?

    1. @ExceptionHandler(NullPointerException.class)
    2. @ExceptionHandler(RuntimeException)
    3. @ExceptionHandler(IOException)
    4. @ExceptionHandler(Exception.class)

    Explanation: Using @ExceptionHandler(NullPointerException.class) ensures only NullPointerExceptions are handled. Exception and RuntimeException would capture broader or different sets of exceptions. IOException is unrelated and irrelevant for NullPointerExceptions. Be specific in exception types for targeted handling.

  8. Logging Format Configuration

    Which property can you set in application.properties to customize the log message format in Spring Boot?

    1. logging.pattern.console
    2. pattern.log.console
    3. console.pattern.log
    4. log.format.pattern

    Explanation: The logging.pattern.console property allows you to define the format of log messages in the console. The other options are not recognized by Spring Boot, so changing them will have no effect. logging.pattern.console gives flexibility in controlling log output appearance.

  9. Disabling Console Logging

    What is the correct way to disable console logging output in a Spring Boot application?

    1. Redirect STDOUT
    2. Set logging.pattern.console=null
    3. Set logging.console.enabled=false
    4. No direct property; needs custom configuration

    Explanation: There is no direct property to disable console logging; you need to provide a custom logging configuration to exclude the console appender. The first two options are not valid Spring Boot properties. Redirecting STDOUT is a workaround, not a configuration approach. Using custom configuration is the proper method.

  10. Handling Uncaught Exceptions Globally

    Which Spring annotation allows you to handle uncaught exceptions from controllers application-wide?

    1. @RestController
    2. @GlobalException
    3. @ControllerAdvice
    4. @ExceptionController

    Explanation: @ControllerAdvice enables global handling of exceptions thrown by controllers across the application. @RestController is for defining REST endpoints, not exception handling. @GlobalException and @ExceptionController are not standard annotations and would not provide global exception handling. Always use @ControllerAdvice for consistent error management.