æ°ãããªãã¨ããªã話ã§ããå½ããåã¨æã人ã«ã¯æããå½ããåï¼æ度åãã¯ãã¾ãï¼ãåç §ï¼ãã§ãç¥ããªããã¨ã¯æªã§ã¯ãªããããããªãã ã¼ã£ã¦ãªãã°ããã ãã®è©±ã
ä¸é£ã®ãã°ã«åãå¤ãä»ä¸ãããæã MDC
(SLF4J) ã ThreadContext
(Log4j2) ã使ããã¨æãã¾ãã
ä¸èº«ã¯ java.lang.ThreadLocal
ã¨ãã ã£ããããã®ã§ããµã¼ãã¼ã¢ããªã±ã¼ã·ã§ã³ããã«ãã¹ã¬ããã§ã¯åãé¤ãã¦ãããªãã¨äºæ
ã®å
ã§ãã
// å¾®å¦ãªããæ¹ void method() { MDC.put("username", "hoge"); // ... ãªããå¦ç MDC.remove("username"); }
ãããªãã¨ãã¡ããã¨ä¾å¤ãçºçããå ´åã« remove
ãããªãã¦äºæ
ãã®ã§ãä¼çµ±çãªæ¹æ³ã§ã¯ finally
ã§ãããã¨æãã¾ãã
// ä¼çµ±çãªããæ¹ void method() { try { MDC.put("username", "hoge"); // ... ãªããå¦ç } finally { MDC.remove("username"); } }
ThreadContext
ãããã¾ã§ã¯ã»ã¼åãã
ãã¨ç´°ããããã©ãä¸è¨ã® "username"
ãäºç®æã«åºãã¨ããè¤æ°ã®é
ç®ãæ±ãã¨ãã¨ããããã¾å¬ãããªãã§ãã
æ¬é¡: å¾çä»ãã¯Javaã«ä»»ããã
try-with-resources
ã使ãã¾ãã
SLF4Jã ã¨ããã
void methodMDC() { try (var ignore = MDC.putCloseable("username", "hoge")) { // ... ãªããå¦ç } }
Log4j2ã 㨠ThreadContext
ã®ãããã«CloseableThreadContext
ã§ãã
void methodThreadContext() { try (var ignore = CloseableThreadContext.put("username", "hoge")) { // ... ãªããå¦ç } }
try-with-resources
㧠close
ãããã¨ã§ remove
æ¼ãããªããããã¼ãè¤æ°æ¸ããã«ãããªããåãéããç¡ããªãã¾ãã put
㨠remove
ã1ã¡ã½ããã«æ¸ããªãï¼äºåå¦çã¨äºå¾å¦çãå¥ã®ã¨ããã§æ¸ããªãããããªããªã©ï¼ã¨ãã§ãªããã°ã常ã«ãããã使ããã¨ããå§ããã¾ãã
SFL4J ã¯è£å®ã§ putCloseable
ã¡ã½ãããåºã¦ããã®ã§æ°ã¥ããããã®ã§ããã Log4j2㯠org.apache.logging.log4j.CloseableThreadContext
ã¨å¥ã¯ã©ã¹ã«ãªãããèªç¥çã¯ã¬ã¯ãã¨ä¸ããå°è±¡ã§ããLog4j2ãSFL4J以å¤ã使ã£ã¦ãã¦ããåæ§ã®ãã®ãããã¨æãã¾ãã
var
ã使ã£ã¦ããã®ã¯ç´ ç´ã«ãã㨠MDC.MDCCloseable
ã CloseableThreadContext.Instance
ã¨é·ããªãã«ãé¢ãããããã®å¤æ°ã®åã«èå³ããªãããã§ããããããã¨ã㯠var
ãè¼ãã
ãã¨å¤æ°åã ignore
ã¨ãã«ããã®ã¯ãæªä½¿ç¨å¤æ°ã§ãããã¯è¦åããªãã§ããã£ã¦IDEAã¸ã®ä¸»å¼µã§ãã
try-with-resources
ã使ç¨ããå ´åã«ã¯ãã使ãã¨æãã¾ããignore
ã§ãignored
ã§ãããããè¤æ°ããã¨ignore1
ã¨ãé£çªã«ãªã£ããã§ããµããªãã¾ããå人çã«ã¯ _
ã¨ãã«ããããã ãã©ãã
IDEã®è¦åã¯ã¼ãã«ãã¨ãã®ãããã§ãã
è±ç·: æ¿«ç¨ï¼
MDC
ã ThreadContext
ã¯ãã°ã©ã¤ãã©ãªã®ãã®ãªãã§ããã使ããããã¨è¨ãçç±ã§ãã¼ã¿åã渡ãã«ä¾¿å©ã«ä½¿ç¨ããã¦ããããã¾ãã
ãããªä½¿ãæ¹ãããããããããå¿
è¦ã§ããã°ãã®æ
å ±åã渡ãããã£ã¡ãè¨è¨ããããâ¦â¦ã¨æã£ããããããã§ããã
å¼æ°ã§å¼ãåã以å¤ã®æ¹æ³ã¨ãªãã¨ãDBãªã©ã®æ°¸ç¶æè¡ã使ãã ThreadLocal
ãªã©ã使ç¨ããå¿
è¦ãåºã¦ãã¾ãã
åã«åãã ããªãã©ããªæ¹æ³ã§ãç°¡åãªãã§ãããé©åã«è¨è¨ããã®ã¯é£ãããä¸æãªãã¨ããããããªããã£ã¡ä½¿ã£ã¦ããæ¹ããã·ãâ¦â¦ã¨æã£ãããã
ThreadLocal
ã使ãã¨éåæå¦çã使ãæã¨ãã«é©åã«åã渡ãå¿
è¦ããã£ããï¼ãã¨ãã° SpringBootã§Asyncã使ãæã«ç¥ã£ã¦ããããExecutorã®ã㨠(2023-08-24) ã§æ¸ãã¦ããããã«ï¼ãè¤æ°ãã®ãããªãã®ãåºã¦ããã¨æ¼ãããããã¡ã§ããã¨å¼ãã¾ããããã¹ã³ã¼ããä¼¼ããå¯ã£ããã«ãªãã®ã§ãã¾ããããããã£ã¦æã£ããããããã¯ãã¡ããã¨ãã¨ãªã£ããã
Observation
ã® Context
ãã ããä»åã®ä¾ã§ username
ã¨æ¸ãããã©èªè¨¼ã³ã³ããã¹ãããããªæãã
ãã¶ãåã
ã§é©åã«åãæ±ãä½ããä½ã£ã¦ãå¿
è¦ãªã¨ãã§ã¢ããã¿ãä½ãã¼ã¨ãããã°ç¶ºéºãªãã ããã©ãã³ã¹ããããã¨ãã§ããªãããªãâ¦â¦ã¨ãã¨ãããªãã¾ããã åãã°ããããåãã°
ãã¾ã
ãããããªãã¨ã㯠AutoCloseableã§ãªãã¦ãtry-with-resourcesãããã (2013-01-05) ã®æ¹æ³ã§è¶³æ»ããããããã¦ããããã¥ãããªããã¼ã£ã¦ãªã£ã¦ãæ®éã« finally
ã§ãã£ãããã¦ã¾ããã
// ä»ã¯ä¸è¦ void method() throws Exception { MDC.put("username", "hoge"); try (AutoCloseable ignore = () -> MDC.remove("username")) { // ... ãªããå¦ç } }
putCloseable
ããã®ã«ãããã£ã¦ããè²ã
èãç´ããæ¹ãããã§ãã
ãã¾ãï¼
ã¯ã¦ãªããã°ã«ãã¤ã®ã¾ã«ãAIã§ã®ã¿ã¤ãã«çææ©è½ãã¤ãã¦ãã®ã§è©¦ãã¦ã¿ãã
ã©ããã³ã¬ã¸ã£ãã¤æã ã ãã©ãAIãã¤ããã¿ã¤ãã«ã¨èªåãã¤ããã¿ã¤ãã«ãè¿ãæç« ã®ã»ããããæç« ãªã®ãããããªããªããã¨ãæã£ãããããã
ãã¾ãï¼
åé ã§ãæãããã£ã¦æ¸ããã®ã§ãã©ããããããªã¼ã¨èª¿ã¹ã¦ã¿ãã
- SLF4J 㯠1.7.8 ã§å ¥ã£ã¦ããã®ã§ã 2014-12-14ã
- Log4j2 㯠2.6 ã§å ¥ã£ã¦ããã®ã§ã2016-05-25ã
ã¸ã¼ãæ¥ä»ã¯ CentralRepository ã®ã¿ã¤ã ã¹ã¿ã³ããã