|
| 1 | +/* |
| 2 | + * Copyright 2025 LINE Corporation |
| 3 | + * |
| 4 | + * LINE Corporation licenses this file to you under the Apache License, |
| 5 | + * version 2.0 (the "License"); you may not use this file except in compliance |
| 6 | + * with the License. You may obtain a copy of the License at: |
| 7 | + * |
| 8 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 12 | + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 13 | + * License for the specific language governing permissions and limitations |
| 14 | + * under the License. |
| 15 | + */ |
| 16 | +package com.linecorp.centraldogma.server.internal.api; |
| 17 | + |
| 18 | +import java.util.List; |
| 19 | + |
| 20 | +import javax.annotation.Nullable; |
| 21 | + |
| 22 | +import org.slf4j.LoggerFactory; |
| 23 | + |
| 24 | +import com.fasterxml.jackson.annotation.JsonProperty; |
| 25 | +import com.fasterxml.jackson.databind.JsonNode; |
| 26 | +import com.google.common.collect.ImmutableList; |
| 27 | +import com.google.common.collect.ImmutableList.Builder; |
| 28 | + |
| 29 | +import com.linecorp.armeria.common.HttpResponse; |
| 30 | +import com.linecorp.armeria.common.HttpStatus; |
| 31 | +import com.linecorp.armeria.common.MediaType; |
| 32 | +import com.linecorp.armeria.server.annotation.ConsumesJson; |
| 33 | +import com.linecorp.armeria.server.annotation.Get; |
| 34 | +import com.linecorp.armeria.server.annotation.Param; |
| 35 | +import com.linecorp.armeria.server.annotation.ProducesJson; |
| 36 | +import com.linecorp.armeria.server.annotation.Put; |
| 37 | +import com.linecorp.centraldogma.server.internal.api.auth.RequiresSystemAdministrator; |
| 38 | + |
| 39 | +import ch.qos.logback.classic.Level; |
| 40 | +import ch.qos.logback.classic.Logger; |
| 41 | +import ch.qos.logback.classic.LoggerContext; |
| 42 | + |
| 43 | +@RequiresSystemAdministrator |
| 44 | +@ProducesJson |
| 45 | +public class LoggerService { |
| 46 | + |
| 47 | + private static final org.slf4j.Logger logger = LoggerFactory.getLogger(LoggerService.class); |
| 48 | + |
| 49 | + private static final List<String> LEVELS = ImmutableList.of( |
| 50 | + "ALL", "TRACE", "DEBUG", "INFO", "WARN", "ERROR", "OFF"); |
| 51 | + |
| 52 | + private final LoggerContext loggerContext = (LoggerContext) LoggerFactory.getILoggerFactory(); |
| 53 | + |
| 54 | + @Put("/loggers/{logger}") |
| 55 | + @ConsumesJson |
| 56 | + public HttpResponse setLogLevel(@Param("logger") String loggerName, JsonNode jsonNode) { |
| 57 | + final Logger logger = loggerContext.exists(loggerName); |
| 58 | + if (logger == null) { |
| 59 | + return HttpResponse.of(HttpStatus.NOT_FOUND, MediaType.PLAIN_TEXT_UTF_8, |
| 60 | + "Logger not found: " + loggerName); |
| 61 | + } |
| 62 | + final JsonNode levelNode = jsonNode.get("level"); |
| 63 | + if (levelNode == null) { |
| 64 | + return HttpResponse.of(HttpStatus.BAD_REQUEST, MediaType.PLAIN_TEXT_UTF_8, |
| 65 | + "Missing 'level' field in request body."); |
| 66 | + } |
| 67 | + |
| 68 | + if (levelNode.isNull()) { |
| 69 | + logger.setLevel(null); |
| 70 | + LoggerService.logger.info("Set log level of '{}' to null. effectiveLevel='{}'", |
| 71 | + loggerName, logger.getEffectiveLevel()); |
| 72 | + return HttpResponse.ofJson(LoggerInfo.of(logger)); |
| 73 | + } |
| 74 | + |
| 75 | + if (!levelNode.isTextual()) { |
| 76 | + return HttpResponse.of(HttpStatus.BAD_REQUEST, MediaType.PLAIN_TEXT_UTF_8, |
| 77 | + "'level' field must be a string. Found: " + levelNode.getNodeType()); |
| 78 | + } |
| 79 | + |
| 80 | + final String level = levelNode.asText(); |
| 81 | + final String upperCase = level.toUpperCase(); |
| 82 | + if (!LEVELS.contains(upperCase)) { |
| 83 | + return HttpResponse.of(HttpStatus.BAD_REQUEST, MediaType.PLAIN_TEXT_UTF_8, |
| 84 | + "Invalid log level: " + level + ". Valid levels are: " + LEVELS); |
| 85 | + } |
| 86 | + logger.setLevel(Level.toLevel(upperCase)); |
| 87 | + LoggerService.logger.info("Set log level of '{}' to '{}'.", loggerName, upperCase); |
| 88 | + return HttpResponse.ofJson(LoggerInfo.of(logger)); |
| 89 | + } |
| 90 | + |
| 91 | + @Get("/loggers/{logger}") |
| 92 | + public HttpResponse getLogLevel(@Param("logger") String loggerName) { |
| 93 | + final Logger logger = loggerContext.exists(loggerName); |
| 94 | + if (logger == null) { |
| 95 | + return HttpResponse.of(HttpStatus.NOT_FOUND, MediaType.PLAIN_TEXT_UTF_8, |
| 96 | + "Logger not found: " + loggerName); |
| 97 | + } |
| 98 | + return HttpResponse.ofJson(LoggerInfo.of(logger)); |
| 99 | + } |
| 100 | + |
| 101 | + @Get("/loggers") |
| 102 | + @ProducesJson |
| 103 | + public HttpResponse getLogLevels() { |
| 104 | + final Builder<LoggerInfo> builder = ImmutableList.builder(); |
| 105 | + for (Logger logger : loggerContext.getLoggerList()) { |
| 106 | + builder.add(LoggerInfo.of(logger)); |
| 107 | + } |
| 108 | + return HttpResponse.ofJson(builder.build()); |
| 109 | + } |
| 110 | + |
| 111 | + private static final class LoggerInfo { |
| 112 | + |
| 113 | + static LoggerInfo of(Logger logger) { |
| 114 | + return new LoggerInfo( |
| 115 | + logger.getName(), |
| 116 | + logger.getLevel() != null ? logger.getLevel().toString() : null, |
| 117 | + logger.getEffectiveLevel().toString()); |
| 118 | + } |
| 119 | + |
| 120 | + private final String name; |
| 121 | + @Nullable |
| 122 | + private final String level; |
| 123 | + private final String effectiveLevel; |
| 124 | + |
| 125 | + LoggerInfo(String name, @Nullable String level, String effectiveLevel) { |
| 126 | + this.name = name; |
| 127 | + this.level = level; |
| 128 | + this.effectiveLevel = effectiveLevel; |
| 129 | + } |
| 130 | + |
| 131 | + @JsonProperty("name") |
| 132 | + String name() { |
| 133 | + return name; |
| 134 | + } |
| 135 | + |
| 136 | + @Nullable |
| 137 | + @JsonProperty("level") |
| 138 | + String level() { |
| 139 | + return level; |
| 140 | + } |
| 141 | + |
| 142 | + @JsonProperty("effectiveLevel") |
| 143 | + String effectiveLevel() { |
| 144 | + return effectiveLevel; |
| 145 | + } |
| 146 | + } |
| 147 | +} |
0 commit comments