Skip to content

Commit

Permalink
root logger
Browse files Browse the repository at this point in the history
  • Loading branch information
turboFei committed Dec 23, 2024
1 parent 82c25af commit 04ec3e6
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ import scala.collection.JavaConverters._
import io.swagger.v3.oas.annotations.media.{Content, Schema}
import io.swagger.v3.oas.annotations.responses.ApiResponse
import io.swagger.v3.oas.annotations.tags.Tag
import org.apache.commons.lang3.StringUtils
import org.apache.logging.log4j.{Level, LogManager}
import org.apache.logging.log4j.core.LoggerContext
import org.apache.logging.log4j.core.config.Configurator
Expand All @@ -46,7 +45,7 @@ class LoggerResource extends ApiRequestContext {
description = "Get the logger level, return all loggers if no name specified.")
@GET
def getLoggerLevel(@QueryParam("name") name: String): LoggerInfos = {
if (StringUtils.isNotBlank(name)) {
if (null != name) {
new LoggerInfos().addLoggersItem(
new LoggerInfo().name(name).level(LogManager.getLogger(name).getLevel.toString))
} else {
Expand All @@ -72,6 +71,6 @@ class LoggerResource extends ApiRequestContext {
val newLevel = Level.toLevel(request.getLevel)
Configurator.setLevel(loggerName, newLevel)
new HandleResponse().success(true).message(
s"Set logger $loggerName level from $originalLevel to $newLevel")
s"Set logger `$loggerName` level from `$originalLevel` to `$newLevel`.`")
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,29 @@ abstract class ApiV1BaseResourceSuite extends HttpTestHelper {
assert(loggers.exists(l => l.getName == loggerName && l.getLevel == "DEBUG"))
// root logger
assert(loggers.exists(l => l.getName == "" && l.getLevel == "INFO"))

// update root logger level
val response5 =
webTarget.path("loggers").request(MediaType.APPLICATION_JSON).post(Entity.entity(
new LoggerInfo().name("").level("DEBUG"),
MediaType.APPLICATION_JSON))
assert(HttpServletResponse.SC_OK == response5.getStatus)

// check root logger level is DEBUG
val response6 = webTarget.path("loggers")
.queryParam("name", "")
.request(MediaType.APPLICATION_JSON).get()
assert(HttpServletResponse.SC_OK == response6.getStatus)
val loggerInfo3 = response6.readEntity(classOf[LoggerInfos]).getLoggers.get(0)
assert("" == loggerInfo3.getName)
assert(loggerInfo3.getLevel == "DEBUG")

// reset root logger level
val response7 =
webTarget.path("loggers").request(MediaType.APPLICATION_JSON).post(Entity.entity(
new LoggerInfo().name(loggerName).level("INFO"),
MediaType.APPLICATION_JSON))
assert(HttpServletResponse.SC_OK == response7.getStatus)
}

test("thread_dump") {
Expand Down

0 comments on commit 04ec3e6

Please sign in to comment.