Skip to content
Merged
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
3 changes: 2 additions & 1 deletion src/main/resources/logback-spring.xml
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<configuration>
<springProperty scope="context" name="SLACK_WEBHOOK_URI" source="slack.logging.error"/>
<!-- local: 이 파일 안에서만 쓴다. context로 두면 LogstashEncoder가 모든 JSON 로그 줄에 웹훅 주소를 함께 기록한다. -->
<springProperty scope="local" name="SLACK_WEBHOOK_URI" source="slack.logging.error"/>
<property name="defaultLogPattern"
value="[%d{yyyy-MM-dd'T'HH:mm:ss.SSS}] %-5level --- [%15.15thread] %-40.40logger{36} : %msg%n"/>
<property name="consoleLogPattern"
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package in.koreatech.koin.unit.global.logging;

import static java.nio.charset.StandardCharsets.UTF_8;
import static org.assertj.core.api.Assertions.assertThat;

import org.junit.jupiter.api.Test;
import org.slf4j.LoggerFactory;
import org.springframework.boot.logging.LoggingInitializationContext;
import org.springframework.boot.logging.LoggingSystem;
import org.springframework.mock.env.MockEnvironment;

import ch.qos.logback.classic.AsyncAppender;
import ch.qos.logback.classic.Level;
import ch.qos.logback.classic.Logger;
import ch.qos.logback.classic.LoggerContext;
import ch.qos.logback.classic.spi.LoggingEvent;
import ch.qos.logback.core.OutputStreamAppender;
import ch.qos.logback.core.encoder.Encoder;
import com.github.maricn.logback.SlackAppender;

class LogbackSecretPropertyTest {

private static final String WEBHOOK = "http://127.0.0.1:9/slack-webhook-secret-marker";

@Test
void 파일_JSON_로그에_Slack_웹훅_주소를_기록하지_않는다() {
LoggingSystem loggingSystem = LoggingSystem.get(getClass().getClassLoader());
try {
initialize(loggingSystem, profileEnvironment("prod").withProperty("slack.logging.error", WEBHOOK));
LoggerContext context = (LoggerContext)LoggerFactory.getILoggerFactory();
Logger root = context.getLogger(Logger.ROOT_LOGGER_NAME);
OutputStreamAppender<?> file = (OutputStreamAppender<?>)root.getAppender("FILE");

@SuppressWarnings("unchecked")
byte[] encoded = ((Encoder<LoggingEvent>)file.getEncoder())
.encode(new LoggingEvent(getClass().getName(), root, Level.ERROR, "test", null, null));

assertThat(new String(encoded, UTF_8)).contains("test").doesNotContain("slack-webhook-secret-marker");
assertThat(context.getProperty("SLACK_WEBHOOK_URI")).isNull();
SlackAppender slack = (SlackAppender)((AsyncAppender)root.getAppender("ASYNC_SLACK")).getAppender("SLACK");
assertThat(slack.getWebhookUri()).isEqualTo(WEBHOOK);
} finally {
// 다른 테스트가 같은 JVM에서 쓰는 test 프로필 로그 설정으로 되돌린다.
initialize(loggingSystem, profileEnvironment("test"));
}
}

private MockEnvironment profileEnvironment(String profile) {
MockEnvironment environment = new MockEnvironment();
environment.setActiveProfiles(profile);
return environment;
}

private void initialize(LoggingSystem loggingSystem, MockEnvironment environment) {
loggingSystem.cleanUp();
loggingSystem.beforeInitialize();
loggingSystem.initialize(new LoggingInitializationContext(environment), null, null);
}
}
Loading