Repository: seratch/notion-sdk-jvm
Pull request #71 (2022-08-23 08:30:32)

Add slf4j-api v2 support
This pull request adds sfl4j-api v2 support as a new pluggable module.


Patch:
diff --git a/README.md b/README.md
index 60200bd..9267804 100644
--- a/README.md
+++ b/README.md
@@ -195,11 +195,11 @@ client.httpClient = OkHttp3Client()
 
 #### Pluggable Logging
 
-You can change the `logger` property of a `NotionClient` instances Currently, this libarary supports its own stdout logger (default), `java.util.logging`, and slf4j-api based ones. Here are the steps to switch to an slf4j-api logger. Add the following optional module along with your favorite implementation (e.g., logback-classic, slf4j-simple).
+You can change the `logger` property of a `NotionClient` instances Currently, this library supports its own stdout logger (default), `java.util.logging`, and slf4j-api based ones. Here are the steps to switch to an slf4j-api logger. Add the following optional module along with your favorite implementation (e.g., logback-classic, slf4j-simple).
 
 ```gradle
 implementation("com.github.seratch:notion-sdk-jvm-slf4j:${notionSdkVersion}") // slf4j-api 1.7
-implementation("org.slf4j:slf4j-simple:1.7.30")
+implementation("org.slf4j:slf4j-simple:1.7.36")
 ```
 
 Now you can switch to your slf4j logger. As with the `httpClient` example, you can use the setter method too.
@@ -219,6 +219,13 @@ val client = NotionClient(
 )
 ```
 
+If you desire to use slf4j-api v2 instead, you can use the module for the major version as below:
+
+```gradle
+implementation("com.github.seratch:notion-sdk-jvm-slf4j2:${notionSdkVersion}") // slf4j-api 2.0
+implementation("org.slf4j:slf4j-simple:2.0.0")
+```
+
 #### Why isn't JSON serialization pluggable?
 
 We don't support other JSON libraries yet. There are two reasons:
diff --git a/pom.xml b/pom.xml
index 30d6ca4..66f7ca2 100644
--- a/pom.xml
+++ b/pom.xml
@@ -26,6 +26,7 @@
         <module>okhttp4</module>
         <module>okhttp5</module>
         <module>slf4j</module>
+        <module>slf4j2</module>
     </modules>
 
     <parent>
@@ -43,6 +44,7 @@
         <okhttp5.version>5.0.0-alpha.10</okhttp5.version>
         <gson.version>2.9.1</gson.version>
         <slf4j.version>1.7.36</slf4j.version>
+        <slf4j2.version>2.0.0</slf4j2.version>
         <logback.version>1.2.10</logback.version>
         <kotlin.version>1.7.10</kotlin.version>
         <kotlin.code.style>official</kotlin.code.style>
diff --git a/slf4j2/pom.xml b/slf4j2/pom.xml
new file mode 100644
index 0000000..47c2895
--- /dev/null
+++ b/slf4j2/pom.xml
@@ -0,0 +1,46 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
+    <modelVersion>4.0.0</modelVersion>
+
+    <parent>
+        <groupId>com.github.seratch</groupId>
+        <artifactId>notion-sdk-jvm-parent</artifactId>
+        <version>1.6.1-SNAPSHOT</version>
+    </parent>
+
+    <artifactId>notion-sdk-jvm-slf4j2</artifactId>
+    <version>1.6.1-SNAPSHOT</version>
+    <packaging>jar</packaging>
+
+    <properties>
+        <maven.compiler.target>1.8</maven.compiler.target>
+        <maven.compiler.source>1.8</maven.compiler.source>
+    </properties>
+
+    <dependencies>
+        <dependency>
+            <groupId>com.github.seratch</groupId>
+            <artifactId>notion-sdk-jvm-core</artifactId>
+            <version>${project.version}</version>
+        </dependency>
+        <dependency>
+            <groupId>org.slf4j</groupId>
+            <artifactId>slf4j-api</artifactId>
+            <version>${slf4j2.version}</version>
+        </dependency>
+
+        <dependency>
+            <groupId>org.slf4j</groupId>
+            <artifactId>slf4j-simple</artifactId>
+            <version>${slf4j2.version}</version>
+            <scope>test</scope>
+        </dependency>
+        <dependency>
+            <groupId>com.github.seratch</groupId>
+            <artifactId>notion-sdk-jvm-okhttp3</artifactId>
+            <version>${project.version}</version>
+            <scope>test</scope>
+        </dependency>
+    </dependencies>
+</project>
\ No newline at end of file
diff --git a/slf4j2/src/main/kotlin/notion/api/v1/logging/Slf4jLogger.kt b/slf4j2/src/main/kotlin/notion/api/v1/logging/Slf4jLogger.kt
new file mode 100644
index 0000000..4dc7ce3
--- /dev/null
+++ b/slf4j2/src/main/kotlin/notion/api/v1/logging/Slf4jLogger.kt
@@ -0,0 +1,41 @@
+package notion.api.v1.logging
+
+import org.slf4j.LoggerFactory
+
+class Slf4jLogger(name: String = Slf4jLogger::class.java.canonicalName) : NotionLogger {
+  private val logger = LoggerFactory.getLogger(name)
+
+  override fun isDebugEnabled(): Boolean = logger.isDebugEnabled
+
+  override fun debug(message: String, e: Throwable?) {
+    if (e != null) {
+      logger.debug(message, e)
+    } else {
+      logger.debug(message)
+    }
+  }
+
+  override fun info(message: String, e: Throwable?) {
+    if (e != null) {
+      logger.info(message, e)
+    } else {
+      logger.info(message)
+    }
+  }
+
+  override fun warn(message: String, e: Throwable?) {
+    if (e != null) {
+      logger.warn(message, e)
+    } else {
+      logger.warn(message)
+    }
+  }
+
+  override fun error(message: String, e: Throwable?) {
+    if (e != null) {
+      logger.error(message, e)
+    } else {
+      logger.error(message)
+    }
+  }
+}


Test patch:
diff --git a/slf4j2/src/test/kotlin/integration_tests/SimpleJavaTest.java b/slf4j2/src/test/kotlin/integration_tests/SimpleJavaTest.java
new file mode 100644
index 0000000..58d6330
--- /dev/null
+++ b/slf4j2/src/test/kotlin/integration_tests/SimpleJavaTest.java
@@ -0,0 +1,21 @@
+package integration_tests;
+
+import notion.api.v1.NotionClient;
+import notion.api.v1.logging.Slf4jLogger;
+import notion.api.v1.model.databases.Databases;
+import notion.api.v1.model.search.SearchResults;
+import org.junit.Assert;
+
+public class SimpleJavaTest {
+
+    public static void main(String[] args) {
+        NotionClient client = new NotionClient(System.getenv("NOTION_TOKEN"));
+        client.setLogger(new Slf4jLogger());
+        try {
+            SearchResults results = client.search("Test Database");
+            Assert.assertNotNull(results);
+        } finally {
+            client.close();
+        }
+    }
+}
diff --git a/slf4j2/src/test/kotlin/integration_tests/SimpleTest.kt b/slf4j2/src/test/kotlin/integration_tests/SimpleTest.kt
new file mode 100644
index 0000000..4745cf8
--- /dev/null
+++ b/slf4j2/src/test/kotlin/integration_tests/SimpleTest.kt
@@ -0,0 +1,82 @@
+package integration_tests
+
+import kotlin.test.assertNotNull
+import notion.api.v1.NotionClient
+import notion.api.v1.http.OkHttp3Client
+import notion.api.v1.logging.Slf4jLogger
+import notion.api.v1.model.pages.Page
+import notion.api.v1.model.pages.PageParent
+import notion.api.v1.model.pages.PageProperty
+import notion.api.v1.request.search.SearchRequest
+import org.junit.Test
+
+typealias prop = PageProperty
+
+class SimpleTest {
+
+  @Test
+  fun pages() {
+    NotionClient(token = System.getenv("NOTION_TOKEN")).use { client ->
+      client.httpClient = OkHttp3Client()
+      client.logger = Slf4jLogger()
+
+      val database =
+          client
+              .search(
+                  query = "Test Database",
+                  filter = SearchRequest.SearchFilter("database", property = "object"))
+              .results
+              .find { it.asDatabase().properties.containsKey("Severity") }
+              ?.asDatabase()
+              ?: throw IllegalStateException(
+                  "Create a database named 'Test Database' and invite this app's user!")
+
+      // All the options for "Severity" property (select type)
+      val severityOptions = database.properties?.get("Severity")?.select?.options
+      // All the options for "Tags" property (multi_select type)
+      val tagOptions = database.properties?.get("Tags")?.multiSelect?.options
+      // The user object for "Assignee" property (people type)
+      val assignee = client.listUsers().results[0] // just picking the first user up
+
+      val newPage: Page =
+          client.createPage(
+              // Use the "Test Database" as this page's parent
+              parent = PageParent.database(databaseId = database.id),
+              // Set values to the page's properties
+              // (these must be pre-defined before this API call)
+              properties =
+                  mapOf(
+                      "Title" to
+                          prop(
+                              title =
+                                  listOf(
+                                      PageProperty.RichText(
+                                          text =
+                                              PageProperty.RichText.Text(content = "Fix a bug")))),
+                      "Severity" to prop(select = severityOptions?.find { it.name == "High" }),
+                      "Tags" to prop(multiSelect = tagOptions),
+                      "Due" to
+                          prop(date = PageProperty.Date(start = "2021-05-13", end = "2021-12-31")),
+                      "Velocity Points" to prop(number = 3),
+                      "Assignee" to prop(people = listOf(assignee)),
+                      "Done" to prop(checkbox = true),
+                      "Link" to prop(url = "https://www.example.com"),
+                      "Contact" to prop(email = "foo@example.com"),
+                  ))
+      assertNotNull(newPage)
+
+      val patchResult =
+          client.updatePage(
+              pageId = newPage.id,
+              // Update only "Severity" property
+              properties =
+                  mapOf(
+                      "Severity" to prop(select = severityOptions?.find { it.name == "Medium" }),
+                  ))
+      assertNotNull(patchResult)
+
+      val fetchedPage = client.retrievePage(newPage.id)
+      assertNotNull(fetchedPage)
+    }
+  }
+}
diff --git a/slf4j2/src/test/resources/simplelogger.properties b/slf4j2/src/test/resources/simplelogger.properties
new file mode 100644
index 0000000..eafa2b0
--- /dev/null
+++ b/slf4j2/src/test/resources/simplelogger.properties
@@ -0,0 +1,1 @@
+org.slf4j.simpleLogger.defaultLogLevel=debug
\ No newline at end of file