๋ก๊น
์ด ํ์ด์ง์ ๋ด์ฉ
๊ฐ์
์ด ๊ฐ์ด๋์์๋ MongoDB ์ฝํ๋ฆฐ(Kotlin) ๋๋ผ์ด๋ฒ์์ ๋ก๊ฑฐ๋ฅผ ์ค์ ํ๊ณ ๊ตฌ์ฑํ๋ ๋ฐฉ๋ฒ์ ๋ฐฐ์ธ ์ ์์ต๋๋ค.
๋ค์ ํญ๋ชฉ์ ์ํํ๋ ๋ฐฉ๋ฒ์ ๋ฐฐ์๋๋ค:
SLF4J(Simple Logging Facade For Java)๋ฅผ ์ฌ์ฉํ ๋ก๊ฑฐ ์ค์
๋ก๊ฑฐ์ ๋ก๊ทธ ์์ค ๊ตฌ์ฑ
์ด ๊ฐ์ด๋ ์์๋ ์ด์ ์ ์์ ์ด๋ฒคํธ๋ฅผ ๊ธฐ๋ก ํ๋ ๋ฐฉ๋ฒ์ ๋ณด์ฌ ์ค๋๋ค. ์ฝ๋์์ ์ด์ ์ ์ ํ๋์ ๋ํ ์ ๋ณด๋ฅผ ์ฌ์ฉํ๋ ๋ฐฉ๋ฒ์ ํ์ต ๋ณด๋ ค๋ฉด๋ชจ๋ํฐ๋ง ๊ฐ์ด๋๋ํ ๊ฐ์ด๋๋ฅผ ์ฝ์ด๋ณด์ธ์.
๋ก๊ฑฐ(Logger) ์ค์
์ด ์น์ ์์๋ ๋ก๊ฑฐ๋ฅผ ์ค์ ํ๋ ค๋ฉด ํ์ํ ์ข ์์ฑ์ ๋ํ ๋ฐฐ๊ฒฝ ์ ๋ณด๋ฅผ ์ ๊ณตํ๊ณ ๋ก๊ฑฐ ์ค์ ์์๋ฅผ ์ ๊ณตํฉ๋๋ค.
๋ฐฐ๊ฒฝ
MongoDB ์ฝํ๋ฆฐ (Kotlin) ์ด์ ์ SLF4J(Simple Logging Facade For Java )๋ฅผ ์ฌ์ฉํฉ๋๋ค. SLF4J๋ฅผ ์ฌ์ฉํ๋ฉด ๋ฐฐํฌ์๋ฒ ์ ์ํ๋ ๋ก๊น ํ๋ ์์ํฌ ์ง์ ํ ์ ์์ต๋๋ค. SLF4J, ์ ๋ํ ์์ธํ ๋ด์ฉ์ SLF4J ๋ฌธ์๋ฅผ ์ฐธ์กฐํ์ธ์.
๋ก๊ฑฐ ์ค์ ์ ์ ํ ์ฌํญ์
๋๋ค. ์ ํ๋ฆฌ์ผ์ด์
์ ์์ํ๋ฉด MongoDB Kotlin ๋๋ผ์ด๋ฒ๋ ํด๋์ค ๊ฒฝ๋ก์์ slf4j-api
์ํฐํฉํธ๋ฅผ ์ฐพ์ต๋๋ค. ๋๋ผ์ด๋ฒ๊ฐ slf4j-api
์ํฐํฉํธ๋ฅผ ์ฐพ์ ์ ์๋ ๊ฒฝ์ฐ ๋๋ผ์ด๋ฒ๋ java.util.logging
๋ฅผ ์ฌ์ฉํ์ฌ ๋ค์ ๊ฒฝ๊ณ ๋ฅผ ๊ธฐ๋กํ๊ณ ๋ชจ๋ ์ถ๊ฐ ๋ก๊น
์ ๋นํ์ฑํํฉ๋๋ค.
WARNING: SLF4J not found on the classpath. Logging is disabled for the 'org.mongodb.driver' component
๋ก๊ฑฐ๋ฅผ ์ค์ ํ๋ ค๋ฉด ํ๋ก์ ํธ์ ๋ค์์ ํฌํจํด์ผ ํฉ๋๋ค.
slf4j-api
์ํฐํฉํธ๋ก๊น ํ๋ ์์ํฌ
๋ฐ์ธ๋ฉ
์ฐธ๊ณ
๊ฐ์ฅ ๋ง์ด ์ฌ์ฉ๋๋ ๋ก๊น
ํ๋ ์์ํฌ์ ๊ฒฝ์ฐ slf4j-api
์ ๋ก๊น
ํ๋ ์์ํฌ๋ฅผ ์ข
์์ฑ์ผ๋ก ๋์ดํ๋ ๋จ์ผ ๋ฐ์ธ๋ฉ ์ํฐํฉํธ๊ฐ ์๋ ๊ฒฝ์ฐ๊ฐ ๋ง์ต๋๋ค. ์ด๋ ํ๋ก์ ํธ์ ์ข
์์ฑ ๋ชฉ๋ก์ ํ๋์ ์ํฐํฉํธ๋ฅผ ์ถ๊ฐํ์ฌ ๋ก๊ฑฐ๋ฅผ ์ค์ ํ ์ ์์์ ์๋ฏธํฉ๋๋ค. ์๋ ์์์์ ์ด๋ฅผ ํ์ธํ ์ ์์ต๋๋ค.
๊ฒฐํฉ์ slf4j-api
์ํฐํฉํธ๋ฅผ ๋ก๊น
ํ๋ ์์ํฌ์ ์ฐ๊ฒฐํ๋ ์ค๋ํซ์
๋๋ค. ๋ค์ ์์๋ ๊ฐ์ฅ ๋ง์ด ์ฌ์ฉ๋๋ ๋ ๊ฐ์ง ๋ก๊น
ํ๋ ์์ํฌ์ธ Log4j2์ Logback์ slf4j-api
์ํฐํฉํธ๋ฅผ ๋ฐ์ธ๋ฉํ๋ ๋ฐฉ๋ฒ์ ๋ณด์ฌ ์ค๋๋ค.
์์ - ์ค์
๋ณธ ์์๋ ๋ก๊ฑฐ๋ฅผ ์ค์ ํ๋ ๋ฐฉ๋ฒ์ ์ค๋ช ํฉ๋๋ค. ํ๋ก์ ํธ์์ ์ฌ์ฉํ๋ ค๋ ๋ก๊น ํ๋ ์์ํฌ์ ํด๋นํ๋ ํญ์ ํด๋ฆญํ์ธ์.
ํ
์ข ์์ฑ ๋ฒ์
๋์ด๋ ๋ค์ ๋ฒ์ ์ ๊ณต์์ ์ธ ์ ๋ณด๊ฐ ์๋๋ฉฐ ์ค๋ช ์ ์ํ ๊ฒ์ ๋๋ค. ํ์คํ ์ต์ ๋ฒ์ ์ ๋ณด๋ฅผ ํ์ธํ๋ ค๋ฉด SLF4J ๋ฐ ์ ํํ ๋ก๊น ํ๋ ์์ํฌ์ ๋ํ ๊ณต์ ๋ฌธ์๋ฅผ ํ์ธํ์ธ์.
ํ๋ก์ ํธ์์ ์ฌ์ฉ ์ค์ธ ๋น๋ ๋๊ตฌ๋ฅผ ์ ํํฉ๋๋ค.
pom.xml
ํ์ผ์ ๋ค์ ์ข
์์ฑ์ ์ถ๊ฐํฉ๋๋ค.
<dependencies> <dependency> <groupId>ch.qos.logback</groupId> <artifactId>logback-classic</artifactId> <version>1.2.11</version> </dependency> </dependencies>
build.gradle.kts
ํ์ผ ์ ๋ค์ ์ข
์์ฑ์ ์ถ๊ฐํฉ๋๋ค.
dependencies { implementation("ch.qos.logback:logback-classic:1.2.11") }
์์ ์ข ์์ฑ์ ํฌํจํ์ผ๋ฉด MongoDB ์ธ์คํด์ค์ ์ฐ๊ฒฐํ๊ณ ๋ค์ ์ฝ๋๋ฅผ ์ฌ์ฉํ์ฌ ๋ฌธ์๋ฅผ ๊ฒ์ํฉ๋๋ค.
val mongoClient = MongoClient.create("<connection string>"); val database = mongoClient.getDatabase(DB_NAME_PLACEHOLDER); val collection = database.getCollection<Document>(COLLECTION_NAME_PLACEHOLDER); collection.find().firstOrNull()
... 12:14:55.853 [main] DEBUG org.mongodb.driver.connection - Opened connection [connectionId{localValue:3, serverValue:3}] to <MongoDB hostname> 12:14:55.861 [main] DEBUG org.mongodb.driver.protocol.command - Command "find" started on database <database> using a connection with driver-generated ID 3 and server-generated ID 3 to <MongoDB hostname>. The request ID is 5. Command: {"find": "<collection>", "filter": {}, "limit": 1, "singleBatch": true, "$db": "<database>", "lsid": {"id": {"$binary": {"base64": "<_id>", "subType": "04"}}}, "$readPreference": {"mode": "primaryPreferred"}} 12:14:55.864 [main] DEBUG org.mongodb.driver.protocol.command - Command "find" succeeded in 4.34 ms using a connection with driver-generated ID 3 and server-generated ID 3 to <MongoDB hostname. The request ID is 5. Command reply: {"cursor": {"id": 0, "ns": "<database>.<collection>", "firstBatch": []}, "ok": 1.0, "$clusterTime": {"clusterTime": {"$timestamp": {"t": 1673778535, "i": 1}}, "signature": {"hash": {"$binary": {"base64": "<_id>", "subType": "00"}}, "keyId": 0}}, "operationTime": {"$timestamp": {"t": 1673778535, "i": 1}}}
์ฐธ๊ณ
๊ธฐ๋ณธ๊ฐ ๋ก๊ทธ ์์ค
Logback์ ๊ธฐ๋ณธ๊ฐ ๋ก๊ทธ ์์ค์ DEBUG์ ๋๋ค. Logback ๋ก๊ฑฐ์ ๋ก๊ทธ ์์ค์ ๋ณ๊ฒฝํ๋ ๋ฐฉ๋ฒ์ ํ์ต ๋ณด๋ ค๋ฉด ์ด ํ์ด์ง์ ๋ก๊ฑฐ ๊ตฌ์ฑ ์น์ ์ ์๋ ์์ ๋ฅผ ์ฐธ์กฐํ์ธ์.
Logback์ ๋ํ ์์ธํ ๋ด์ฉ์ Logback ๋งค๋ด์ผ์ ์ฐธ์กฐํ์ธ์.
ํ๋ก์ ํธ์์ ์ฌ์ฉ ์ค์ธ ๋น๋ ๋๊ตฌ๋ฅผ ์ ํํฉ๋๋ค.
pom.xml
ํ์ผ์ ๋ค์ ์ข
์์ฑ์ ์ถ๊ฐํฉ๋๋ค.
<dependencies> <dependency> <groupId>org.apache.logging.log4j</groupId> <artifactId>log4j-slf4j-impl</artifactId> <version>2.17.1</version> </dependency> </dependencies>
build.gradle.kts
ํ์ผ์ ๋ค์ ์ข
์์ฑ์ ์ถ๊ฐํฉ๋๋ค.
dependencies { implementation("org.apache.logging.log4j:log4j-slf4j-impl:2.17.1") }
์์ ์ข ์์ฑ์ ํฌํจํ์ผ๋ฉด ๋ค์ ์ฝ๋๋ฅผ ์ฌ์ฉํ์ฌ ์ค๋ฅ๋ฅผ ๋ก๊ทธ ํฉ๋๋ค.
val loggerParent = LoggerFactory.getLogger("parent") val loggerChild = LoggerFactory.getLogger("parent.child")
val loggerParent = LoggerFactory.getLogger("parent") val loggerChild = LoggerFactory.getLogger("parent.child")
12:35:00.438 [main] ERROR <my package path> - Logging an Error
์ฐธ๊ณ
๊ธฐ๋ณธ๊ฐ ๋ก๊ทธ ์์ค
๋ก๊ทธ4J2 ์ ๊ธฐ๋ณธ๊ฐ ๋ก๊ทธ ์์ค์ ERROR์ ๋๋ค. ์ฆ, MongoDB ์ฝํ๋ฆฐ (Kotlin) ์ด์ ์ ์์ ํ์ค ์์ ์ ์คํ ํ๋ฉด ๊ตฌ์ฑ ์์ด ๋ก๊ทธ4J2 ์ถ๋ ฅ์ด ์์ฑ๋์ง ์์ต๋๋ค. Log4J2 ๋ก๊ฑฐ์ ๋ก๊ทธ ์์ค์ ๋ณ๊ฒฝํ๋ ๋ฐฉ๋ฒ์ ํ์ต ๋ณด๋ ค๋ฉด ์ด ํ์ด์ง์ ๋ก๊ฑฐ ๊ตฌ์ฑ ์น์ ์ ์๋ ์์ ๋ฅผ ์ฐธ์กฐํ์ธ์.
๋ก๊ทธ4j ์ ๋ํ2 ์์ธํ ๋ด์ฉ์ ๋ก๊ทธ4j2 ๋งค๋ด์ผ์ ์ฐธ์กฐํ์ญ์์ค.
๋ก๊ฑฐ ๊ตฌ์ฑ
๋ก๊ฑฐ๋ฅผ ๊ตฌ์ฑํ๋ ค๋ฉด SLF4J์ ๋ฐ์ธ๋ฉ๋ ๋ก๊น ํ๋ ์์ํฌ์ ๊ตฌ์ฑ ์์คํ ์ ์ฌ์ฉํด์ผ ํฉ๋๋ค.
๋ค์ ์์์์๋ ๋ก๊น ํ๋ ์์ํฌ์ ๊ตฌ์ฑ ์์คํ ์ ์ฌ์ฉํ์ฌ ๋ก๊ฑฐ์ ๋ก๊ทธ ์์ค์ ์ค์ ํ๋ ๋ฐฉ๋ฒ์ ์ค๋ช ํฉ๋๋ค.
๋ก๊ฑฐ์ ๋ก๊ทธ ์์ค์ ๋ก๊ฑฐ๊ฐ ๋ฉ์์ง๋ฅผ ์ถ๋ ฅํ๊ธฐ ์ํ ๋ฉ์์ง์ ๊ธด๊ธ์ฑ ์ ๋์ ํํ์ ์ง์ ํฉ๋๋ค.
์์ - ๊ตฌ์ฑ
์ด ์์์์๋ ๋ก๊ฑฐ์ ๋ก๊ทธ ์์ค์ INFO๋ก ๊ตฌ์ฑํ๋ ๋ฐฉ๋ฒ์ ๋ณด์ฌ์ค๋๋ค. ํ๋ก์ ํธ์์ ์ฌ์ฉ ์ค์ธ ๋ก๊น ํ๋ ์์ํฌ์ ํด๋นํ๋ ํญ์ ์ ํํฉ๋๋ค.
logback.xml
(์ด)๋ผ๋ ํ์ผ์ Logback ๊ตฌ์ฑ์ ์ง์ ํฉ๋๋ค. logback.xml
ํ์ผ์ ํน์ ์์น์ ์์ ํ์๋ ์์ง๋ง ํด๋์ค ๊ฒฝ๋ก์์ ์ก์ธ์คํ ์ ์์ด์ผ ํฉ๋๋ค.
Logback ํ๋ ์์ํฌ๋ ๋ค์๊ณผ ๊ฐ์ ๋ก๊ทธ ์์ค์ ์ ์ํฉ๋๋ค. ๋ค์์ ๊ฐ์ฅ ๊ธด๊ธํ ๊ฒ๋ถํฐ ๊ฐ์ฅ ๋ ๊ธด๊ธํ ๊ฒ๊น์ง ์์๋๋ก ๋ก๊ทธ ์์ค์ ๋์ดํ ๊ฒ์ ๋๋ค:
์ค๋ฅ
๊ฒฝ๊ณ
์ ๋ณด
DEBUG
ํธ๋ ์ด์ค
logback.xml
ํ์ผ์ ๋ค์์ผ๋ก ์ค์ ํฉ๋๋ค.
<configuration> <appender name="CONSOLE" class="ch.qos.logback.core.ConsoleAppender"> <encoder> <pattern> %-4relative [%thread] %-5level %logger{30} - %msg%n </pattern> </encoder> </appender> <root level="INFO"> <appender-ref ref="CONSOLE" /> </root> </configuration>
๋ก๊ฑฐ ๊ตฌ์ฑ์ด ์ฑ๊ณต์ ์ผ๋ก ์ด๋ฃจ์ด์ก๋์ง ํ ์คํธํ๋ ค๋ฉด ๋ค์ ์ฝ๋๋ฅผ ์คํํฉ๋๋ค.
val mongoClient = MongoClient.create("<connection string>"); val database = mongoClient.getDatabase(DB_NAME_PLACEHOLDER); val collection = database.getCollection<Document>(COLLECTION_NAME_PLACEHOLDER); collection.find().firstOrNull()
... 1317 [cluster-ClusterId{value='<your cluster id>', description='null'}-<your connection uri>] INFO org.mongodb.driver.cluster - Discovered replica set primary <your connection uri> 1568 [main] INFO org.mongodb.driver.connection - Opened connection [connectionId{localValue:7, serverValue:<server value>}] to <your connection uri>
Logback ๊ตฌ์ฑ์ ๋ํ ์์ธํ ๋ด์ฉ ์ Logback ๋งค๋ด์ผ์ ์ฐธ์กฐํ์ธ์.
log4j2.xml
ํ์ผ ์ ๋ก๊ทธ4j2 ๊ตฌ์ฑ์ ์ง์ ํฉ๋๋ค. log4j2.xml
ํ์ผ ์ด ํน์ ์์น ์ ์์ ํ์๋ ์์ง๋ง ํด๋์ค ๊ฒฝ๋ก์์ ์ก์ธ์คํ ์ ์์ด์ผ ํฉ๋๋ค.
Log4J2 ํ๋ ์์ํฌ๋ ๋ค์๊ณผ ๊ฐ์ ๋ก๊ทธ ์์ค์ ์ ์ํฉ๋๋ค. ๋ค์์ ๊ฐ์ฅ ๊ธด๊ธํ ๊ฒ๋ถํฐ ๊ฐ์ฅ ๋ ๊ธด๊ธํ ๊ฒ๊น์ง ์์๋๋ก ๋ก๊ทธ ์์ค์ ๋์ดํ ๊ฒ์ ๋๋ค.
์น๋ช ์
์ค๋ฅ
๊ฒฝ๊ณ
์ ๋ณด
DEBUG
ํธ๋ ์ด์ค
ALL
log4j2.xml
ํ์ผ์ ๋ค์์ผ๋ก ์ค์ ํฉ๋๋ค.
<Configuration status="INFO"> <Appenders> <Console name="Console" target="SYSTEM_OUT"> <PatternLayout pattern="%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n"/> </Console> </Appenders> <Loggers> <Root level="INFO"> <AppenderRef ref="Console"/> </Root> </Loggers> </Configuration>
๋ก๊ฑฐ ๊ตฌ์ฑ์ด ์ฑ๊ณต์ ์ผ๋ก ์ด๋ฃจ์ด์ก๋์ง ํ ์คํธํ๋ ค๋ฉด ๋ค์ ์ฝ๋๋ฅผ ์คํํฉ๋๋ค.
val mongoClient = MongoClient.create("<connection string>"); val database = mongoClient.getDatabase(DB_NAME_PLACEHOLDER); val collection = database.getCollection<Document>(COLLECTION_NAME_PLACEHOLDER); collection.find().firstOrNull()
... 10:14:57.633 [cluster-ClusterId{value=<your cluster id>, description='null'}-<your connection uri>] INFO org.mongodb.driver.cluster - Discovered replica set primary <your connection uri> 10:14:57.790 [main] INFO org.mongodb.driver.connection - Opened connection [connectionId{localValue:7, serverValue:<your server value>}] to <your connection uri>
๋ก๊ทธ4j ๊ตฌ์ฑ์ ๋ํ2 ์์ธํ ๋ด์ฉ์ ๊ณต์ ๋ก๊ทธ4j2 ๊ตฌ์ฑ ๊ฐ์ด๋ ๋ฅผ ์ฐธ์กฐํ์ธ์.
๋ก๊ฑฐ(Logger) ์ด๋ฆ
๋ก๊ฑฐ๋ ๋ก๊ฑฐ ์ด๋ฆ์ ์ฌ์ฉํ์ฌ ๋ค์ํ ๋ก๊น
์ด๋ฒคํธ๋ฅผ ๊ตฌ์ฑํ๋ ๋ฐ ๋์์ ์ค๋๋ค. ๋ก๊ฑฐ ์ด๋ฆ์ ๊ณ์ธต ๊ตฌ์กฐ๋ฅผ ํ์ฑํ๋ ๋ฌธ์์ด์
๋๋ค. ์ด๋ฆ ๋ค์ "."
๊ฐ ๋ค๋ฅธ ๋ก๊ฑฐ ์ด๋ฆ์ ์ ๋์ฌ์ธ ๊ฒฝ์ฐ ๋ก๊ฑฐ๋ ๋ค๋ฅธ ๋ก๊ฑฐ์ ์กฐ์์
๋๋ค. ์๋ฅผ ๋ค์ด, "grandparent"
๋ "grandparent.parent.child"
์ ์กฐ์์ธ "grandparent.parent"
์ ์กฐ์์
๋๋ค.
๊ตฌ์ฒด์ ์ธ ์๋ฅผ ๋ค์ด, ์ฝ๋์์ ๋ก๊ฑฐ ๊ณ์ธต ๊ตฌ์กฐ๋ ๋ค์๊ณผ ๊ฐ์ต๋๋ค.
import org.slf4j.LoggerFactory
val loggerParent = LoggerFactory.getLogger("parent") val loggerChild = LoggerFactory.getLogger("parent.child")
๋ก๊ฑฐ๋ ๊ธฐ์กด ๋ก๊ฑฐ์ ์์ฑ์ ์์๋ฐ์ผ๋ฉฐ ์์ฑ์ ์์ฒด์ ์ผ๋ก ์ ์ํ ์ ์์ต๋๋ค. ์ด๋ ์ฝํ๋ฆฐ(Kotlin)์ ํด๋์ค ์์๊ณผ ์ ์ฌํ๋ค๊ณ ์๊ฐํ๋ฉด ๋ฉ๋๋ค.
MongoDB ์ฝํ๋ฆฐ(Kotlin) ๋๋ผ์ด๋ฒ๋ ๋๋ผ์ด๋ฒ์์ ๋ค์ํ ๋ก๊น ์ด๋ฒคํธ๋ฅผ ๊ตฌ์ฑํ๊ธฐ ์ํด ๋ค์๊ณผ ๊ฐ์ ๋ก๊ฑฐ ์ด๋ฆ์ ์ ์ํฉ๋๋ค. ๋ค์์ ๋๋ผ์ด๋ฒ์ ์ ์๋ ๋ก๊ฑฐ ์ด๋ฆ๊ณผ ํด๋นํ๋ ๋ก๊น ์ด๋ฒคํธ์ ๋๋ค.
org.mongodb.driver.authenticator
: ์ธ์ฆorg.mongodb.driver.client
:MongoClient
์ธ์คํด์ค์ ๊ด๋ จ๋ ์ด๋ฒคํธorg.mongodb.driver.cluster
: MongoDB ์๋ฒ ๋ชจ๋ํฐ๋งorg.mongodb.driver.connection
: ์ฐ๊ฒฐ ๋ฐ ์ฐ๊ฒฐ ํorg.mongodb.driver.connection.tls
: TLS/SSLorg.mongodb.driver.operation
: ์๋ ์ฌ์๋์ ๊ด๋ จ๋ ๋ก๊น ์ ํฌํจํ ์์org.mongodb.driver.protocol
: MongoDB Server๋ก ์ ์ก๋ ๋ช ๋ น ๋ฐ MongoDB Server์์ ์์ ํ ์๋ตorg.mongodb.driver.uri
: ์ฐ๊ฒฐ ๋ฌธ์์ด ๊ตฌ๋ฌธ ๋ถ์org.mongodb.driver.management
: JMX(Java Management Extensions)
์์ - ์ด๋ฆ
์ด ์์์๋ ํน์ ๋๋ผ์ด๋ฒ ๋ก๊ฑฐ์ ๋ก๊ทธ ์์ค์ ๋ณ๊ฒฝํ๋ ๋ฐฉ๋ฒ์ ๋ณด์ฌ ์ค๋๋ค. ๋ฃจํธ ๋ก๊ฑฐ๋ฅผ OFF๋ก ์ค์ ํ๊ณ org.mongodb.driver.connection
๋ก๊ฑฐ๋ฅผ INFO๋ก ์ค์ ํฉ๋๋ค. ์ด๋ ๊ฒ ํ๋ฉด ์ ํ๋ฆฌ์ผ์ด์
์ด MongoDB ์ธ์คํด์ค ์ฐ๊ฒฐ๊ณผ ๊ด๋ จ๋ ๋ฉ์์ง๋ง ๊ธฐ๋กํฉ๋๋ค.
ํ๋ก์ ํธ์์ ์ฌ์ฉ ์ค์ธ ๋ก๊น ํ๋ ์์ํฌ์ ํด๋นํ๋ ํญ์ ์ ํํ์ธ์.
logback.xml
ํ์ผ์ ๋ค์์ผ๋ก ์ค์ ํฉ๋๋ค.
<configuration> <appender name="CONSOLE" class="ch.qos.logback.core.ConsoleAppender"> <encoder> <pattern> %-4relative [%thread] %-5level %logger{30} - %msg%n </pattern> </encoder> </appender> <logger name="org.mongodb.driver.connection" level="INFO" additivity="true"/> <root level="OFF"> <appender-ref ref="CONSOLE" /> </root> </configuration>
๋ก๊ฑฐ ๊ตฌ์ฑ์ด ์ฑ๊ณตํ๋์ง ํ ์คํธํ๋ ค๋ฉด ๋ค์ ์ฝ๋๋ฅผ ์คํํ์ธ์.
val mongoClient = MongoClient.create("<connection string>"); val database = mongoClient.getDatabase(DB_NAME_PLACEHOLDER); val collection = database.getCollection<Document>(COLLECTION_NAME_PLACEHOLDER); collection.find().firstOrNull()
... 829 [cluster-rtt-ClusterId{value='<some value>', description='null'}-<your connection URI>] INFO org.mongodb.driver.connection - Opened connection [connectionId{localValue:2, serverValue:<your server value>}] to <your connection uri> 977 [main] INFO org.mongodb.driver.connection - Opened connection [connectionId{localValue:7, serverValue:<your server value>}] to <your connection uri>
Logback ๊ตฌ์ฑ์ ๋ํ ์์ธํ ๋ด์ฉ์ ๊ณต์ Logback ๊ตฌ์ฑ ๊ฐ์ด๋ ๋ฅผ ์ฐธ์กฐํ์ธ์.
log4j2.xml
ํ์ผ์ ๋ค์์ผ๋ก ์ค์ ํฉ๋๋ค.
<Configuration status="INFO"> <Appenders> <Console name="Console" target="SYSTEM_OUT"> <PatternLayout pattern="%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n"/> </Console> </Appenders> <Loggers> <Logger name="org.mongodb.driver.connection" level="INFO"/> <Root level="OFF"> <AppenderRef ref="Console"/> </Root> </Loggers> </Configuration>
๋ก๊ฑฐ ๊ตฌ์ฑ์ด ์ฑ๊ณต์ ์ผ๋ก ์ด๋ฃจ์ด์ก๋์ง ํ ์คํธํ๋ ค๋ฉด ๋ค์ ์ฝ๋๋ฅผ ์คํํฉ๋๋ค.
val mongoClient = MongoClient.create("<connection string>"); val database = mongoClient.getDatabase(DB_NAME_PLACEHOLDER); val collection = database.getCollection<Document>(COLLECTION_NAME_PLACEHOLDER); collection.find().firstOrNull()
... 15:40:23.005 [cluster-ClusterId{value='<some value>', description='null'}-<your connection uri>] INFO org.mongodb.driver.connection - Opened connection [connectionId{localValue:3, serverValue:<your server value>}] to <your connection uri> 15:40:23.159 [main] INFO org.mongodb.driver.connection - Opened connection [connectionId{localValue:7, serverValue:<your server value>}] to <your connection uri>
Log4j ๊ตฌ์ฑ์ ๋ํ2 ์์ธํ ๋ด์ฉ์ ๊ณต์ Log4J2 ๊ตฌ์ฑ ๊ฐ์ด๋ ๋ฅผ ์ฐธ์กฐํ์ธ์.