๐Ÿ“œ CleanCode

[CleanCode]๊ฒฝ๊ณ„ 2

๋‰ด์ด NUEY 2024. 10. 1. 21:01
๋ฐ˜์‘ํ˜•

 

log4j
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.apache.logging.log4j.core.LoggerContext;
import org.apache.logging.log4j.core.config.Configuration;
import org.apache.logging.log4j.core.config.LoggerConfig;
import org.apache.logging.log4j.core.layout.PatternLayout;
import org.apache.logging.log4j.core.appender.ConsoleAppender;
import org.junit.Before;
import org.junit.Test;

public class LogTest {
    private Logger logger;

    @Before
    public void initialize() {
        logger = LogManager.getLogger("logger");

        // ๋ชจ๋“  Appender ์ œ๊ฑฐ
        LoggerContext context = (LoggerContext) LogManager.getContext(false);
        Configuration config = context.getConfiguration();
        LoggerConfig loggerConfig = config.getLoggerConfig(LogManager.ROOT_LOGGER_NAME);
        loggerConfig.removeAppenders();
        context.updateLoggers(); // ์„ค์ • ๋ฐ˜์˜
    }

    @Test
    public void basicLogger() {
        // ๊ฐ„๋‹จํ•œ ๊ธฐ๋ณธ ์„ค์ •์œผ๋กœ ๋กœ๊ฑฐ ์ดˆ๊ธฐํ™”
        LoggerContext context = (LoggerContext) LogManager.getContext(false);
        Configuration config = context.getConfiguration();
        LoggerConfig loggerConfig = config.getLoggerConfig(LogManager.ROOT_LOGGER_NAME);

        PatternLayout layout = PatternLayout.newBuilder().withPattern("%p %t %m%n").build();
        ConsoleAppender appender = ConsoleAppender.newBuilder()
                .setName("ConsoleAppender")
                .setLayout(layout)
                .setTarget(ConsoleAppender.Target.SYSTEM_OUT)
                .build();
        appender.start();
        loggerConfig.addAppender(appender, null, null);
        context.updateLoggers();

        logger.info("basicLogger");
    }

    @Test
    public void addAppenderWithStream() {
        LoggerContext context = (LoggerContext) LogManager.getContext(false);
        Configuration config = context.getConfiguration();
        LoggerConfig loggerConfig = config.getLoggerConfig(LogManager.ROOT_LOGGER_NAME);

        PatternLayout layout = PatternLayout.newBuilder().withPattern("%p %t %m%n").build();
        ConsoleAppender appender = ConsoleAppender.newBuilder()
                .setName("ConsoleAppender")
                .setLayout(layout)
                .setTarget(ConsoleAppender.Target.SYSTEM_OUT)
                .build();
        appender.start();
        loggerConfig.addAppender(appender, null, null);
        context.updateLoggers();

        logger.info("addAppenderWithStream");
    }

    @Test
    public void addAppenderWithoutStream() {
        LoggerContext context = (LoggerContext) LogManager.getContext(false);
        Configuration config = context.getConfiguration();
        LoggerConfig loggerConfig = config.getLoggerConfig(LogManager.ROOT_LOGGER_NAME);

        PatternLayout layout = PatternLayout.newBuilder().withPattern("%p %t %m%n").build();
        ConsoleAppender appender = ConsoleAppender.newBuilder()
                .setName("ConsoleAppender")
                .setLayout(layout)
                .build();
        appender.start();
        loggerConfig.addAppender(appender, null, null);
        context.updateLoggers();

        logger.info("addAppenderWithoutStream");
    }
}

๋˜๋„๋ก chatGPT์˜ ๋„์›€์„ ์•ˆ ๋ฐ›์œผ๋ ค ํ•˜๋Š”๋ฐ ์ด๋ฒˆ์€ ์–ด์ฉ” ์ˆ˜ ์—†์ด ๋ฐ›์•„ ์ฝ”๋“œ ์˜ฌ๋ฆฝ๋‹ˆ๋‹ค.

  • ์ฑ…์— ๋‚˜์˜จ ๋‚ด์šฉ์€ ์˜ˆ์ „ ๋ฒ„์ „์ด๋ผ ์ง€๊ธˆ์€ ๊ทธ๋Œ€๋กœ ์จ๋„ ์—†๋Š” ํ•จ์ˆ˜๋“ค์ด ๋งŽ์Šต๋‹ˆ๋‹ค.
  • Maven ๊ฐ™์€ ์•„ํ‚คํ…์ณ ํ”„๋กœ์ ํŠธ์—์„œ ํŽธํ•˜๊ฒŒ apach์˜ log4j๋ฅผ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
    ์ผ๋ฐ˜ ์ž๋ฐ” ํ”„๋กœ์ ํŠธ์—์„œ๋Š” ๋”ฐ๋กœ ๋‹ค์šด๋กœ๋“œ ๋ฐ›๊ณ  ํŒจ์Šค ์„ค์ •์„ ํ•ด์ค˜์•ผ ํ•˜๋Š” ๋“ฑ ์–ด๋ ค์›€์ด ์žˆ์Šต๋‹ˆ๋‹ค.
  • ์œ„์— ๋ณด๋ฉด ๋ฉ”์„œ๋“œ ์•ˆ์— ์ค‘๋ณต๋œ ์ฝ”๋“œ๋“ค์ด ๋ณด์ด๋Š”๋ฐ
    ์ดˆ๊ธฐํ™”๋ฅผ ์œ„ํ•ด์„œ ์ค‘๋ณต๋œ ๋‚ด์šฉ์ž…๋‹ˆ๋‹ค.

์•„์ง ์กด์žฌํ•˜์ง€ ์•Š๋Š” ์ฝ”๋“œ ์‚ฌ์šฉํ•˜๊ธฐ

 

 ์šฐ๋ฆฌ ํŒ€์—์„œ A๋ฅผ ๊ฐœ๋ฐœํ•˜๋Š”๋ฐ, B๋ฅผ ๋งŒ๋“œ๋Š” ํŒ€์—์„œ ๋ฐ›์•„์•ผํ•˜๋Š” ์ •๋ณด๊ฐ€ ๋‹ด๊ธด ์ธํ„ฐํŽ˜์ด์Šค๊ฐ€ ์ •์˜์กฐ์ฐจ ๋˜์ง€ ์•Š์€ ์ƒํƒœ๋ผ๋ฉด ์–ด๋–ป๊ฒŒ ํ•ด์•ผ ํ• ๊นŒ?

 

 

  1. ์ผ๋‹จ ์šฐ๋ฆฌ๊ฐ€ ๋ฐ›์•„์•ผ ํ•˜๋Š” ์ •๋ณด๋ฅผ ์ „๋‹ฌํ•˜๋Š” ์ธํ„ฐํŽ˜์ด์Šค ํด๋ž˜์Šค์™€ ๋ฉ”์„œ๋“œ๊ฐ€๊ฐ€ ์žˆ์„ ๊ฒƒ์ด๋‹ค.
    ์ด๊ฒƒ์„ ๊ฐ€์งœ๋กœ ๋งŒ๋“ค์–ด ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค.
  2. Interface๋ฅผ ํ˜ธ์ถœํ•  Controller๋ฅผ ๋†“๋Š”๋‹ค.
  3. ์ดํ›„์— ์™ธ๋ถ€ ํŒ€์—์„œ B๋ฅผ ๋งŒ๋“ค๋ฉด
    Fake Method์—์„œ API๋ฅผ ํ˜ธ์ถœํ•  Adapter๋ฅผ ์—ฐ๊ฒฐํ•˜๋ฉด ๋œ๋‹ค.

์‹ค์ œ๋กœ ์ด๋Ÿฐ ๊ฒฝ์šฐ๊ฐ€ ๊ต‰์žฅํžˆ ๋งŽ๊ตฌ์š”.
ํƒ€ ํŒ€์—์„œ ์ธํ„ฐํŽ˜์ด์Šค๊ฐ€ ์™„์„ฑ์ด ์•ˆ๋œ ๊ฒฝ์šฐ์— ์ž„์‹œ ๋ฐ์ดํ„ฐ๋ฅผ ์—ฐ๊ฒฐํ•ด ์‚ฌ์šฉํ•˜๊ณค ํ•ฉ๋‹ˆ๋‹ค.

ํด๋ฆฐ์ฝ”๋“œ ์ €์ž๊ฐ€ ๋งํ•˜๋Š” ๊ฒƒ์€ ์ด์ „ ๊ฒฝ๊ณ„1์—์„œ ๋งํ•˜๋Š” ๋ฐ”์™€ ์—ฐ๊ฒฐํ•ด์„œ ์ƒ๊ฐํ•ด๋ณด๋ฉด,
์šฐ๋ฆฌ๊ฐ€ ์™ธ๋ถ€ ์ธํ„ฐํŽ˜์ด์Šค ํ…Œ์ŠคํŠธ์— ๋Œ€ํ•œ ์ฑ…์ž„์€ ์žˆ์ง€๋งŒ ๊ทธ ๊ธฐ๋Šฅ์„ ์‚ฌ์šฉํ•  ์šฐ๋ฆฌ ์ฝ”๋“œ์— ๋Œ€ํ•œ ์ฑ…์ž„์„ ์ €์•ผ ํ•œ๋‹ค๋Š” ๊ฒƒ์ด๊ตฌ์š”. 

FakeMethod๊ฐ€ ๋‹ด๊ธด ํด๋ž˜์Šค(์•„๋งˆ ์ž„์‹œ ๋ฐ์ดํ„ฐ๋งŒ ๋‹ด๊ฒจ์žˆ๊ฒ ์ฃ )์—์„œ Adapter๋ฅผ ํ˜ธ์ถœํ•˜๋Š” ์—ญํ• ๋กœ ๋ฐ”๋€Œ๋ฉด
ex) return adapter.method1();
Adapter์—์„œ๋Š” ์™ธ๋ถ€ API๊ฐ€ ๋ณ€๊ฒฝ๋  ๋•Œ๋งˆ๋‹ค ์ˆ˜์ •ํ•˜๋Š” ์ž‘์—…์„ ํ•˜๋Š” ๊ณณ์ด ๋ฉ๋‹ˆ๋‹ค.

 

๋ฐ˜์‘ํ˜•