Summary
This article provides step-by-step instructions to enable Microsoft Authentication Library for Java (MSAL4J) logging by using the Logback framework in a Spring Boot web application.
Code sample
The complete code sample and configuration guide for this implementation are available on GitHub.
Enable MSAL4J logging
-
Add the following dependency to your Pom.xml file to include the Logback framework:
<dependency> <groupid>ch.qos.logback</groupid> <artifactid>logback-classic</artifactid> <version>1.2.3</version> </dependency> -
In your app project, create a file in the src/main/resources folder, and name the file Logback.xml. Then, add the following content:
<?xml version="1.0" encoding="UTF-8"?> <configuration> <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender"> <encoder> <pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</pattern> </encoder> </appender> <root level="debug"> <appender-ref ref="STDOUT" /> </root> </configuration>This Appender configuration logs messages to the console. You can adjust the logging level to
error,warn,info, orverbosebased on your preference. For more information, see LogBack: Appenders. -
Set the logging.config property to the location of the Logback.xml file before the main method:
@SpringBootApplication public class MsalB2CWebSampleApplication { static { System.setProperty("logging.config", "C:\\Users\\<your path>\\src\\main\\resources\\logback.xml");} public static void main(String[] args) { // Console.log("main"); // System.console().printf("hello"); // System.out.printf("Hello %s!%n", "World"); System.out.printf("%s%n", "Hello World"); SpringApplication.run(MsalB2CWebSampleApplication.class, args); } }
Configuration for running the code sample
Enable HTTPs support
This code sample is set up to run on the local server (localhost) by using the HTTPS protocol. Follow the steps in Configure the sample to use your Azure AD B2C tenant to generate a self-signed certificate. Put the keystore.p12 file in the resources folder.
App registration configuration
To configure app registration in Azure AD B2C, follow these steps:
- Create two app registrations in your Azure AD B2C tenant: One for the web application and the other for the web API.
- Expose the required scope in the web API. For more information, see Configure web API app scopes.
- Configure the web API scope in the API Permissions blade for the web application.
- Grant admin consent to all configured permissions in the web application.
For more information, see Configure authentication in a sample web app that calls a web API by using Azure AD B2C.
Example configuration:
:::image type="content" source="media/enable-msal4j-logging-spring-boot-webapp/app-reg.png" alt-text="Diagram that shows configured app registration." border="true" lightbox="media/enable-msal4j-logging-spring-boot-webapp/app-reg.png":::
Logging output example
If the app is configured correctly, the logging output should resemble the following output.
:::image type="content" source="media/enable-msal4j-logging-spring-boot-webapp/log-sample.png" alt-text="Diagram that shows logging output." border="true" lightbox="media/enable-msal4j-logging-spring-boot-webapp/log-sample.png":::
Third-party information disclaimer
The third-party products that this article discusses are manufactured by companies that are independent of Microsoft. Microsoft makes no warranty, implied or otherwise, about the performance or reliability of these products.
Source: Microsoft Learn / IT Pro troubleshooting by Microsoft and contributors (upstream author metadata: bachoang). Documentation licensed CC BY 4.0. Code samples are covered by the MIT License. Converted from Markdown, with links normalized and embedded media omitted.
