Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ dependencies {
implementation(libs.flyway.core)
implementation(libs.auth0.jwt)
implementation(libs.kotlin.reflect)
implementation(libs.logback.logstash.encoder)

developmentOnly(libs.spring.boot.devtools)

Expand Down
2 changes: 2 additions & 0 deletions libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
apache-commons-text = "1.11.0"
auth0-jwt = "3.18.3"
kotlin-plugin = "1.9.23"
logback-logstash-encoder = "7.4"
spotless = "6.21.0"
spring-boot = "3.2.4"
spring-dependency-management = "1.1.4"
Expand All @@ -14,6 +15,7 @@ flyway-core = { module = "org.flywaydb:flyway-core" }
jackson-datatype-jsr310 = { module = "com.fasterxml.jackson.datatype:jackson-datatype-jsr310" }
jackson-module-kotlin = { module = "com.fasterxml.jackson.module:jackson-module-kotlin" }
kotlin-reflect = { module = "org.jetbrains.kotlin:kotlin-reflect" }
logback-logstash-encoder = { module = "net.logstash.logback:logstash-logback-encoder", version.ref = "logback-logstash-encoder" }
postgresql = { module = "org.postgresql:postgresql" }
springdoc-webmvc-ui = { module = "org.springdoc:springdoc-openapi-starter-webmvc-ui", version.ref = "spring-doc" }
spring-boot-actuator = { module = "org.springframework.boot:spring-boot-starter-actuator" }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import org.springframework.core.annotation.Order
import org.springframework.stereotype.Component
import org.springframework.web.filter.OncePerRequestFilter

@Component
@Component("customCorsFilter")
@Order(value = FilterOrderingConstants.CORS_FILTER_ORDER)
class CorsFilter : OncePerRequestFilter() {
override fun doFilterInternal(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@ package com.nagpal.shivam.workout.manager.filters

import com.nagpal.shivam.workout.manager.utils.Constants
import com.nagpal.shivam.workout.manager.utils.FilterOrderingConstants
import com.nagpal.shivam.workout.manager.utils.Slf4jUtils
import jakarta.servlet.FilterChain
import jakarta.servlet.http.HttpServletRequest
import jakarta.servlet.http.HttpServletResponse
import org.slf4j.LoggerFactory
import org.slf4j.MDC
import org.springframework.core.annotation.Order
import org.springframework.stereotype.Component
import org.springframework.web.filter.OncePerRequestFilter
Expand All @@ -21,10 +21,20 @@ class HttpLoggingFilter : OncePerRequestFilter() {
response: HttpServletResponse,
filterChain: FilterChain,
) {
MDC.put(Constants.REQUEST_ID, request.getAttribute(Constants.REQUEST_ID) as String)
filterChain.doFilter(request, response)
log.info("Request to {}: {} - {}", request.method, formURI(request), response.status)
MDC.remove(Constants.REQUEST_ID)
Slf4jUtils.mdcPutMulti(
mapOf(
Pair(Constants.REQUEST_ID, request.getAttribute(Constants.REQUEST_ID) as String),
Pair(Constants.HTTP_METHOD, request.method),
Pair(Constants.PATH, formURI(request)),
),
)
.use {
log.info("HTTP Request received")
filterChain.doFilter(request, response)
Slf4jUtils.mdcPut(Constants.HTTP_STATUS, response.status.toString()).use {
log.info("HTTP Request responded")
}
}
}

private fun formURI(request: HttpServletRequest): String {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,8 @@ object Constants {
const val USER_ID = "userId"
const val ROLES = "roles"
const val REQUEST_ID = "requestId"
const val HTTP_METHOD = "httpMethod"
const val PATH = "path"
const val HTTP_STATUS = "status"
const val REQUEST_ARRIVAL_TIME = "requestArrivalTime"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package com.nagpal.shivam.workout.manager.utils

import org.slf4j.MDC
import java.io.Closeable

object Slf4jUtils {
class MDCMultiCloseable(private val keys: Set<String>) : Closeable {
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should Implement AutoCloseable instead of Closeable

override fun close() {
keys.forEach { MDC.remove(it) }
}
}

fun mdcPut(key: String, value: String): MDCMultiCloseable {
MDC.put(key, value)
return MDCMultiCloseable(setOf(key))
}

fun mdcPutMulti(entryMap: Map<String, String>): MDCMultiCloseable {
entryMap.forEach {
MDC.put(it.key, it.value)
}
return MDCMultiCloseable(entryMap.keys)
}
}
1 change: 1 addition & 0 deletions src/main/resources/application-staging.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
logging.config=classpath:logback-json.xml
2 changes: 1 addition & 1 deletion src/main/resources/application.properties
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,6 @@ spring.jpa.hibernate.ddl-auto=validate
# Show message in error
server.error.include-message=always
# Logging file path
logging.pattern.level=%5p %highlight(%X{requestId})
logging.pattern.level=%5p %highlight(%X)
logging.file.path=logs
auth.token.public.key=${AUTH_TOKEN_PUBLIC_KEY}
21 changes: 21 additions & 0 deletions src/main/resources/logback-json.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<configuration>
<appender name="CONSOLE" class="ch.qos.logback.core.ConsoleAppender">
<encoder class="net.logstash.logback.encoder.LoggingEventCompositeJsonEncoder">
<providers>
<timestamp>
<fieldName>timestamp</fieldName>
</timestamp>
<logLevel/>
<mdc/>
<threadName/>
<loggerName/>
<message/>
<stackTrace/>
</providers>
</encoder>
</appender>
<!-- Root logger -->
<root level="info">
<appender-ref ref="CONSOLE"/>
</root>
</configuration>