Skip to content

Commit 422bafc

Browse files
authored
Merge pull request #219131 from cilwerner/patch-6
[msid][issue] Instruction to enable MSAL4J logging does not work (GH-92778)
2 parents 0260819 + d1daa99 commit 422bafc

File tree

1 file changed

+52
-26
lines changed

1 file changed

+52
-26
lines changed

articles/active-directory/develop/msal-logging-java.md

Lines changed: 52 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -9,45 +9,71 @@ ms.service: active-directory
99
ms.subservice: develop
1010
ms.topic: conceptual
1111
ms.workload: identity
12-
ms.date: 01/25/2021
12+
ms.date: 11/25/2022
1313
ms.author: dmwendia
14-
ms.reviewer: saeeda, jmprieur
14+
ms.reviewer: saeeda, jmprieur, engagement-fy23
1515
ms.custom: aaddev
1616
---
17+
1718
# Logging in MSAL for Java
1819

1920
[!INCLUDE [MSAL logging introduction](../../../includes/active-directory-develop-error-logging-introduction.md)]
2021

2122
## MSAL for Java logging
2223

23-
MSAL for Java allows you to use the logging library that you are already using with your app, as long as it is compatible with SLF4J. MSAL for Java uses the [Simple Logging Facade for Java](http://www.slf4j.org/) (SLF4J) as a simple facade or abstraction for various logging frameworks, such as [java.util.logging](https://docs.oracle.com/javase/7/docs/api/java/util/logging/package-summary.html), [Logback](http://logback.qos.ch/) and [Log4j](https://logging.apache.org/log4j/2.x/). SLF4J allows the user to plug in the desired logging framework at deployment time.
24-
25-
For example, to use Logback as the logging framework in your application, add the Logback dependency to the Maven pom file for your application:
26-
27-
```xml
28-
<dependency>
29-
<groupId>ch.qos.logback</groupId>
30-
<artifactId>logback-classic</artifactId>
31-
<version>1.2.3</version>
32-
</dependency>
33-
```
34-
35-
Then add the Logback configuration file:
36-
37-
```xml
38-
<?xml version="1.0" encoding="UTF-8"?>
39-
<configuration debug="true">
40-
41-
</configuration>
42-
```
43-
44-
SLF4J automatically binds to Logback at deployment time. MSAL logs will be written to the console.
24+
MSAL for Java allows you to use the logging library that you're already using with your app, as long as it's compatible with SLF4J. MSAL for Java uses the [Simple Logging Facade for Java](http://www.slf4j.org/) (SLF4J) as a simple facade or abstraction for various logging frameworks, such as [java.util.logging](https://docs.oracle.com/javase/7/docs/api/java/util/logging/package-summary.html), [Logback](http://logback.qos.ch/) and [Log4j](https://logging.apache.org/log4j/2.x/). SLF4J allows the user to plug in the desired logging framework at deployment time and automatically binds to Logback at deployment time. MSAL logs will be written to the console.
25+
26+
This article shows how to enable MSAL4J logging using the logback framework in a spring boot web application. You can refer to the [code sample](https://github.com/Azure-Samples/ms-identity-java-webapp/tree/master/msal-java-webapp-sample) for reference.
27+
28+
1. To implement logging, include the `logback` package in the *pom.xml* file.
29+
30+
```xml
31+
<dependency>
32+
<groupId>ch.qos.logback</groupId>
33+
<artifactId>logback-classic</artifactId>
34+
<version>1.2.3</version>
35+
</dependency>
36+
```
37+
38+
2. Navigate to the *resources* folder, and add a file called *logback.xml*, and insert the following code. This will append logs to the console. You can change the appender `class` to write logs to a file, database or any appender of your choosing.
39+
40+
```xml
41+
<?xml version="1.0" encoding="UTF-8"?>
42+
<configuration>
43+
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
44+
<encoder>
45+
<pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</pattern>
46+
</encoder>
47+
</appender>
48+
<root level="debug">
49+
<appender-ref ref="STDOUT" />
50+
</root>
51+
</configuration>
52+
```
53+
3. Next, you should set the *logging.config* property to the location of the *logback.xml* file before the main method. Navigate to *MsalWebSampleApplication.java* and add the following code to the `MsalWebSampleApplication` public class.
54+
55+
```java
56+
@SpringBootApplication
57+
public class MsalWebSampleApplication {
58+
59+
static { System.setProperty("logging.config", "C:\Users\<your path>\src\main\resources\logback.xml"); }
60+
public static void main(String[] arrgs) {
61+
// Console.log("main");
62+
// System.console().printf("Hello");
63+
// System.out.printf("Hello %s!%n", "World");
64+
System.out.printf("%s%n", "Hello World");
65+
SpringApplication.run(MsalWebSampleApplication.class, args);
66+
}
67+
}
68+
```
69+
70+
In your tenant, you'll need separate app registrations for the web app and the web API. For app registration and exposing the web API scope, follow the steps in the scenario [A web app that authenticates users and calls web APIs](/scenario-web-app-call-api-overview).
4571

4672
For instructions on how to bind to other logging frameworks, see the [SLF4J manual](http://www.slf4j.org/manual.html).
4773

4874
### Personal and organization information
4975

50-
By default, MSAL logging does not capture or log any personal or organizational data. In the following example, logging personal or organizational data is off by default:
76+
By default, MSAL logging doesn't capture or log any personal or organizational data. In the following example, logging personal or organizational data is off by default:
5177

5278
```java
5379
PublicClientApplication app2 = PublicClientApplication.builder(PUBLIC_CLIENT_ID)
@@ -68,4 +94,4 @@ PublicClientApplication app2 = PublicClientApplication.builder(PUBLIC_CLIENT_ID)
6894

6995
## Next steps
7096

71-
For more code samples, refer to [Microsoft identity platform code samples](sample-v2-code.md).
97+
For more code samples, refer to [Microsoft identity platform code samples](sample-v2-code.md).

0 commit comments

Comments
 (0)