JavaÈÕÖ¾»ù´¡
JavaʹÓÃÁËÒ»ÖÖ×Ô¶¨ÒåµÄ¡¢¿ÉÀ©Õ¹µÄ·½·¨À´Êä³öÈÕÖ¾¡£ËäÈ»Javaͨ¹ýjava.util.logging°üÌṩÁËÒ»Ì×»ù±¾µÄÈÕÖ¾´¦ÀíAPI£¬µ«Äã¿ÉÒÔºÜÇáËɵÄʹÓÃÒ»ÖÖ»òÕß¶àÖÖÆäËüÈÕÖ¾½â¾ö·½°¸¡£ÕâЩ½â¾ö·½°¸¾¡¹ÜʹÓò»Í¬µÄ·½·¨À´´´½¨ÈÕÖ¾Êý¾Ý£¬µ«ËüÃǵÄ×îÖÕÄ¿±êÊÇÒ»ÑùµÄ£¬¼´½«ÈÕÖ¾´ÓÄãµÄÓ¦ÓóÌÐòÊä³öµ½Ä¿±êµØÖ·¡£
ÔÚÕâÒ»½ÚÖУ¬ÎÒÃÇ»á̽Ë÷JavaÈÕÖ¾±³ºóµÄÔÀí£¬²¢ËµÃ÷ÈçºÎͨ¹ýÈÕÖ¾À´ÈÃÄã³ÉΪһ¸ö¸üºÃµÄJava¿ª·¢ÈËÔ±¡£
JavaÈÕÖ¾×é¼þ
JavaÈÕÖ¾APIÓÉÒÔÏÂÈý¸öºËÐÄ×é¼þ×é³É£º
Loggers£ºLogger¸ºÔð²¶×½Ê¼þ²¢½«Æä·¢Ë͸øºÏÊʵÄAppender¡£
Appenders£ºÒ²±»³ÆÎªHandlers£¬¸ºÔð½«ÈÕ־ʼþ¼Ç¼µ½Ä¿±êλÖá£ÔÚ½«ÈÕ־ʼþÊä³ö֮ǰ£¬AppendersʹÓÃLayoutsÀ´¶Ôʼþ½øÐиñʽ»¯´¦Àí¡£
Layouts£ºÒ²±»³ÆÎªFormatters£¬Ëü¸ºÔð¶ÔÈÕ־ʼþÖеÄÊý¾Ý½øÐÐת»»ºÍ¸ñʽ»¯¡£Layouts¾ö¶¨ÁËÊý¾ÝÔÚÒ»ÌõÈÕÖ¾¼Ç¼ÖеÄ×îÖÕÐÎʽ¡£
µ±Logger¼Ç¼һ¸öʼþʱ£¬Ëü½«Ê¼þת·¢¸øÊʵ±µÄAppender¡£È»ºóAppenderʹÓÃLayoutÀ´¶ÔÈÕÖ¾¼Ç¼½øÐиñʽ»¯£¬²¢½«Æä·¢Ë͸ø¿ØÖÆÌ¨¡¢Îļþ»òÕ߯äËüÄ¿±êλÖá£ÁíÍ⣬Filters¿ÉÒÔÈÃÄã½øÒ»²½Ö¸¶¨Ò»¸öAppenderÊÇ·ñ¿ÉÒÔÓ¦ÓÃÔÚÒ»ÌõÌØ¶¨µÄÈÕÖ¾¼Ç¼ÉÏ¡£ÔÚÈÕÖ¾ÅäÖÃÖУ¬Filters²¢²»ÊDZØÐèµÄ£¬µ«¿ÉÒÔÈÃÄã¸üÁé»îµØ¿ØÖÆÈÕÖ¾ÏûÏ¢µÄÁ÷¶¯¡£

ÈÕÖ¾¿ò¼Ü
ÔÚJavaÖУ¬Êä³öÈÕÖ¾ÐèҪʹÓÃÒ»¸ö»òÕß¶à¸öÈÕÖ¾¿ò¼Ü£¬ÕâЩ¿ò¼ÜÌṩÁ˱ØÒªµÄ¶ÔÏó¡¢·½·¨ºÍÅäÖÃÀ´´«ÊäÏûÏ¢¡£JavaÔÚjava.util.logging°üÖÐÌṩÁËÒ»¸öĬÈϵĿò¼Ü¡£³ý´ËÖ®Í⣬»¹ÓкܶàÆäËüµÚÈý·½¿ò¼Ü£¬°üÀ¨Log4j¡¢LogbackÒÔ¼°tinylog¡£»¹ÓÐÆäËüһЩ¿ª·¢°ü£¬ÀýÈçSLF4JºÍApache
Commons Logging£¬ËüÃÇÌṩÁËһЩ³éÏó²ã£¬¶ÔÄãµÄ´úÂëºÍÈÕÖ¾¿ò¼Ü½øÐнâñ´Ó¶øÔÊÐíÄãÔÚ²»Í¬µÄÈÕÖ¾¿ò¼ÜÖнøÐÐÇл»¡£
ÈçºÎÑ¡ÔñÒ»¸öÈÕÖ¾½â¾ö·½°¸£¬ÕâÈ¡¾öÓÚÄãµÄÈÕÖ¾ÐèÇóµÄ¸´ÔÓ¶È¡¢ºÍÆäËüÈÕÖ¾½â¾ö·½°¸µÄ¼æÈÝÐÔ¡¢Ò×ÓÃÐÔÒÔ¼°¸öÈËϲºÃ¡£Logback»ùÓÚlog4j֮ǰµÄ°æ±¾¿ª·¢£¨°æ±¾1£©£¬Òò´ËËüÃǵŦÄܼ¯ºÏ¶¼·Ç³£ÀàËÆ¡£È»¶ø£¬Log4jÔÚ×îа汾£¨°æ±¾2£©ÖÐÒýÓÃÁËһЩ¸Ä½ø£¬ÀýÈçÖ§³Ö¶àAPI£¬²¢ÌáÉýÁËÔÚÓÃDisruptor¿âµÄÐÔÄÜ¡£¶øtinylog£¬ÓÉÓÚȱÉÙÁËһЩ¹¦ÄÜ£¬ÔËÐÐÌØ±ð¿ì£¬·Ç³£ÊʺÏСÏîÄ¿¡£
ÁíÍâÒ»¸ö¿¼ÂÇÒòËØÊÇ¿ò¼ÜÔÚ»ùÓÚJavaµÄ¸÷ÖÖ²»Í¬ÏîÄ¿ÉϵÄÖ§³Ö³Ì¶È¡£ÀýÈçAndroid³ÌÐòÖ»ÄÜʹÓÃLog4j¡¢Logback»òÕßµÚÈý·½°üÀ´¼Ç¼ÈÕÖ¾£¬
Apache Tomcat¿ÉÒÔʹÓÃLog4jÀ´¼Ç¼ÄÚ²¿ÏûÏ¢£¬µ«Ö»ÄÜʹÓð汾1µÄLog4j¡£
³éÏó²ã
ÖîÈçSLF4JÕâÑùµÄ³éÏó²ã£¬»á½«ÄãµÄÓ¦ÓóÌÐò´ÓÈÕÖ¾¿ò¼ÜÖнâñî¡£Ó¦ÓóÌÐò¿ÉÒÔÔÚÔËÐÐʱѡÔñ°ó¶¨µ½Ò»¸öÌØ¶¨µÄÈÕÖ¾¿ò¼Ü£¨ÀýÈçjava.util.logging¡¢Log4j»òÕßLogback£©£¬Õâͨ¹ýÔÚÓ¦ÓóÌÐòµÄÀà·¾¶ÖÐÌí¼Ó¶ÔÓ¦µÄÈÕÖ¾¿ò¼ÜÀ´ÊµÏÖ¡£Èç¹ûÔÚÀà·¾¶ÖÐÅäÖõÄÈÕÖ¾¿ò¼Ü²»¿ÉÓ㬳éÏó²ã¾Í»áÁ¢¿ÌÈ¡Ïûµ÷ÓÃÈÕÖ¾µÄÏàÓ¦Âß¼¡£³éÏó²ã¿ÉÒÔÈÃÎÒÃǸü¼ÓÈÝÒ׵ظıäÏîÄ¿ÏÖÓеÄÈÕÖ¾¿ò¼Ü£¬»òÕß¼¯³ÉÄÇЩʹÓÃÁ˲»Í¬ÈÕÖ¾¿ò¼ÜµÄÏîÄ¿¡£
ÅäÖÃ
¾¡¹ÜËùÓеÄJavaÈÕÖ¾¿ò¼Ü¶¼¿ÉÒÔͨ¹ý´úÂë½øÐÐÅäÖ㬵«ÊǴ󲿷ÖÅäÖû¹ÊÇͨ¹ýÍⲿÅäÖÃÎļþÍê³ÉµÄ¡£ÕâЩÎļþ¾ö¶¨ÁËÈÕÖ¾ÏûÏ¢ÔÚºÎʱͨ¹ýʲô·½Ê½½øÐд¦Àí£¬ÈÕÖ¾¿ò¼Ü¿ÉÒÔÔÚÔËÐÐʱ¼ÓÔØÕâЩÎļþ¡£ÔÚÕâÒ»½ÚÖÐÌṩµÄ´ó²¿·ÖÅäÖÃʾÀý¶¼Ê¹ÓÃÁËÅäÖÃÎļþ¡£
java.util.logging
ĬÈϵÄJavaÈÕÖ¾¿ò¼Ü½«ÆäÅäÖô洢µ½Ò»¸öÃûΪ logging.properties µÄÎļþÖС£ÔÚÕâ¸öÎļþÖУ¬Ã¿ÐÐÊÇÒ»¸öÅäÖÃÏÅäÖÃÏîʹÓõã±ê¼Ç£¨dot
notation£©µÄÐÎʽ¡£JavaÔÚÆä°²×°Ä¿Â¼µÄlibÎļþ¼ÐÏÂÃæ°²×°ÁËÒ»¸öÈ«¾ÖÅäÖÃÎļþ£¬µ«ÔÚÆô¶¯Ò»¸öJava³ÌÐòʱ£¬Äã¿ÉÒÔͨ¹ýÖ¸¶¨
java.util.logging.config.file ÊôÐԵķ½Ê½À´Ê¹ÓÃÒ»¸öµ¥¶ÀµÄÈÕÖ¾ÅäÖÃÎļþ£¬Í¬ÑùÒ²¿ÉÒÔÔÚ¸öÈËÏîÄ¿Öд´½¨ºÍ´æ´¢
logging.properties Îļþ¡£
ÏÂÃæµÄʾÀýÃèÊöÁËÈçºÎÔÚÈ«¾ÖµÄlogging.propertiesÎļþÖж¨ÒåÒ»¸öAppender£º
# default file
output is in user's home directory.
java.util.logging.FileHandler.pattern = %h/java%u.log
java.util.logging.FileHandler.limit = 50000
java.util.logging.FileHandler.count = 1
java.util.logging.FileHandler.formatter = java.util.logging.XmlFormatter |
Log4j
Log4j°æ±¾1ʹÓõÄÓï·¨ºÍ java.util.logging µÄÓï·¨ºÜÀàËÆ¡£Ê¹ÓÃÁËLog4jµÄ³ÌÐò»áÔÚÏîĿĿ¼ÖÐѰÕÒÒ»¸öÃûΪ
log4j.properties µÄÎļþ¡£Ä¬ÈÏÇé¿öÏ£¬Log4jÅäÖûὫËùÓÐÈÕÖ¾ÏûÏ¢Êä³öµ½¿ØÖÆÌ¨ÉÏ¡£Log4jͬÑùÒ²Ö§³ÖXML¸ñʽµÄÅäÖÃÎļþ£¬¶ÔÓ¦µÄÅäÖÃÐÅÏ¢»á´æ´¢µ½
log4j.xml ÎļþÖС£
Log4j°æ±¾2Ö§³ÖXML¡¢JSONºÍYAML¸ñʽµÄÅäÖã¬ÕâЩÅäÖûá·Ö±ð´æ´¢µ½ log4j2.xml¡¢log4j2.json
ºÍ log4j2.yaml ÎļþÖС£ºÍ°æ±¾1ÀàËÆ£¬°æ±¾2Ò²»áÔÚ¹¤³ÌĿ¼ÖÐѰÕÒÕâЩÎļþ¡£Äã¿ÉÒÔÔÚÿ¸ö°æ±¾µÄÎĵµÖÐÕÒµ½ÏàÓ¦µÄÅäÖÃÎļþʾÀý¡£
Logback
¶ÔÓÚLogbackÀ´Ëµ£¬´ó²¿·ÖÅäÖö¼ÊÇÔÚ logback.xml ÎļþÖÐÍê³ÉµÄ£¬Õâ¸öÎļþʹÓÃÁ˺ÍLog4jÀàËÆµÄXMLÓï·¨¡£LogbackͬʱҲ֧³Öͨ¹ýGroovyÓïÑԵķ½Ê½À´½øÐÐÅäÖã¬ÅäÖÃÐÅÏ¢»á´æ´¢µ½
logback.groovy ÎļþÖС£Äã¿ÉÒÔͨ¹ýÿÖÖÀàÐÍÅäÖÃÎļþµÄÁ´½ÓÕÒµ½¶ÔÓ¦µÄÅäÖÃÎļþʾÀý¡£
Loggers
LoggersÊÇÓÃÀ´´¥·¢ÈÕ־ʼþµÄ¶ÔÏó£¬ÔÚÎÒÃǵÄJavaÓ¦ÓóÌÐòÖб»´´½¨ºÍµ÷Óã¬È»ºóLoggers²Å»á½«Ê¼þ´«µÝ¸øAppender¡£Ò»¸öÀàÖпÉÒÔ°üº¬Õë¶Ô²»Í¬Ê¼þµÄ¶à¸ö¶ÀÁ¢µÄLoggers£¬ÄãÒ²¿ÉÒÔÔÚÒ»¸öLoggersÀïÃæÄÚǶһ¸öLoggers£¬´Ó¶ø´´½¨Ò»ÖÖLoggers²ã´Î½á¹¹¡£
´´½¨ÐÂLogger
ÔÚ²»Í¬µÄÈÕÖ¾¿ò¼ÜÏÂÃæ´´½¨ÐÂLogger¹ý³Ì´óͬСÒ죬¾¡¹Üµ÷ÓõľßÌå·½·¨Ãû³Æ¿ÉÄܲ»Í¬¡£ÔÚʹÓà java.util.logging
ʱ£¬Äã¿ÉÒÔͨ¹ý Logger.getLogger().getLogger() ·½·¨´´½¨ÐÂLogger£¬Õâ¸ö·½·¨½ÓÊÕÒ»¸östring²ÎÊý£¬ÓÃÓÚÖ¸¶¨LoggerµÄÃû×Ö¡£Èç¹ûÖ¸¶¨Ãû×ÖµÄLoggerÒѾ´æÔÚ£¬ÄÇôֻÐèÒª·µ»ØÒѾ´æÔÚµÄLogger£»·ñÔò£¬³ÌÐò»á´´½¨Ò»¸öÐÂLogger¡£Í¨³£Çé¿öÏ£¬Ò»ÖֺõÄ×ö·¨ÊÇ£¬ÎÒÃÇÔÚµ±Ç°ÀàÏÂʹÓÃ
class.getName() ×÷ΪÐÂLoggerµÄÃû×Ö¡£
Logger logger
= Logger.getLogger(MyClass.class.getName()); |
¼Ç¼ÈÕ־ʼþ
LoggerÌṩÁ˼¸ÖÖ·½·¨À´´¥·¢ÈÕ־ʼþ¡£È»¶ø£¬ÔÚÄã¼Ç¼һ¸öʼþ֮ǰ£¬Ä㻹ÐèÒªÉèÖü¶±ð¡£ÈÕÖ¾¼¶±ðÓÃÀ´È·¶¨ÈÕÖ¾µÄÑÏÖØ³Ì¶È£¬Ëü¿ÉÒÔÓÃÀ´¹ýÂËÈÕ־ʼþ»òÕß½«Æä·¢Ë͸ø²»Í¬µÄAppender£¨ÏëÁ˽â¸ü¶àÐÅÏ¢£¬Çë²Î¿¼¡°ÈÕÖ¾¼¶±ð¡±Ò»½Ú£©£¬Logger.log()
·½·¨³ýÁËÈÕÖ¾ÏûÏ¢ÒÔÍ⣬»¹ÐèÒªÒ»¸öÈÕÖ¾¼¶±ð×÷Ϊ²ÎÊý£º
logger.log(Level.WARNING,
¡°This is a warning!¡±); |
´ó²¿·ÖÈÕÖ¾¿ò¼Ü¶¼Õë¶ÔÊä³öÌØ¶¨¼¶±ðÈÕÖ¾ÌṩÁË¿ì½Ý·½Ê½¡£ÀýÈ磬ÏÂÃæÓï¾äµÄ×÷ÓúÍÉÏÃæÓï¾äµÄ×÷ÓÃÊÇÒ»ÑùµÄ£º
logger.warning(¡°This
is a warning!¡±); |
Ä㻹¿ÉÒÔ×èÖ¹LoggerÊä³öµÍÓÚÖ¸¶¨ÈÕÖ¾¼¶±ðµÄÏûÏ¢¡£ÔÚÏÂÃæµÄʾÀýÖУ¬LoggerÖ»ÄÜÊä³ö¸ßÓÚWARNING¼¶±ðµÄÈÕÖ¾ÏûÏ¢£¬²¢¶ªÆúÈÕÖ¾¼¶±ðµÍÓÚWARNINGµÄÏûÏ¢£º
logger.setLevel(Level.WARNING); |
ÎÒÃÇ»¹ÓÐÁíÍâһЩ·½·¨¿ÉÒÔÓÃÀ´¼Ç¼¶îÍâµÄÐÅÏ¢¡£logp()£¨¾«È·ÈÕÖ¾£©¿ÉÒÔÈÃÄãÖ¸¶¨Ã¿ÌõÈÕÖ¾¼Ç¼µÄÔ´Àࣨsource
class£©ºÍ·½·¨£¬¶ø logrb()£¨Ê¹ÓÃ×ÊÔ´°ó¶¨µÄÈÕÖ¾£©¿ÉÒÔÈÃÄãÖ¸¶¨ÓÃÓÚÌáÈ¡ÈÕÖ¾ÏûÏ¢µÄ×ÊÔ´¡£entering()
ºÍ exiting() ·½·¨¿ÉÒÔÈÃÄã¼Ç¼·½·¨µ÷ÓÃÐÅÏ¢£¬´Ó¶ø×·×Ù³ÌÐòµÄÖ´Ðйý³Ì¡£
Appenders
Appenders½«ÈÕÖ¾ÏûϢת·¢¸øÆÚÍûµÄÊä³ö¡£Ëü¸ºÔð½ÓÊÕÈÕ־ʼþ£¬Ê¹ÓÃLayout¸ñʽ»¯Ê¼þ£¬È»ºó½«Æä·¢Ë͸ø¶ÔÓ¦µÄÄ¿±ê¡£¶ÔÓÚÒ»¸öÈÕ־ʼþ£¬ÎÒÃÇ¿ÉÒÔʹÓöà¸öAppendersÀ´½«Ê¼þ·¢Ë͵½²»Í¬µÄÄ¿±êλÖá£ÀýÈ磬ÎÒÃÇ¿ÉÒÔÔÚ¿ØÖÆÌ¨ÉÏÏÔʾһ¸ö¼òµ¥µÄÈÕ־ʼþµÄͬʱ£¬½«Æäͨ¹ýÓʼþµÄ·½Ê½·¢Ë͸øÖ¸¶¨µÄ½ÓÊÕÕß¡£
Çë×¢Ò⣬ÔÚjava.util.loggingÖУ¬Appenders±»³Æ×÷Handlers¡£
Ôö¼ÓAppender
´ó²¿·ÖÈÕÖ¾¿ò¼ÜµÄAppender¶¼»áÖ´ÐÐÀàËÆµÄ¹¦ÄÜ£¬µ«ÔÚʵÏÖ·½Ãæ´óÏྶͥ¡£Èç¹ûʹÓà java.util.logging£¬Äã¿ÉÒÔʹÓÃ
Logger.addHandler() ·½·¨½«AppenderÌí¼Óµ½LoggerÖС£ÀýÈ磬ÏÂÃæµÄ´úÂëÌí¼ÓÁËÒ»¸öеÄConsoleHandler£¬Ëü»á½«ÈÕÖ¾Êä³öµ½¿ØÖÆÌ¨£º
logger.addHandler(new
ConsoleHandler()); |
Ò»ÖÖ¸ü³£ÓõÄÌí¼ÓAppenderµÄ·½Ê½ÊÇʹÓÃÅäÖÃÎļþ¡£Èç¹ûʹÓà java.util.logging£¬Appenders»á¶¨ÒåÒ»¸öÒÔ¶ººÅ¸ô¿ªµÄÁÐ±í£¬ÏÂÃæµÄʾÀý½«ÈÕ־ʼþÊä³öµ½¿ØÖÆÌ¨ºÍÎļþ£º
handlers=java.util.logging.ConsoleHandler,
java.util.logging.FileHandler |
Èç¹ûʹÓûùÓÚXMLµÄÅäÖÃÎļþ£¬Appenders»á±»Ìí¼Óµ½<Appenders>ÔªËØÏÂÃæ£¬Èç¹ûʹÓÃLog4j£¬ÎÒÃÇ¿ÉÒÔºÜÈÝÒ×µØÌí¼ÓÒ»¸öÐÂConsoleAppenderÀ´½«ÈÕÖ¾ÏûÏ¢·¢Ë͵½System.out£º
<Console
name="console" target="SYSTEM_OUT">
<PatternLayout pattern="[%p] %t: %m%n"
/>
</Console> |
AppendersÀàÐÍ
ÕâÒ»½ÚÃèÊöÁËһЩ¸üͨÓõÄAppenders£¬ÒÔ¼°ËüÃÇÔÚ¸÷ÖÖÈÕÖ¾¿ò¼ÜÖÐÊÇÈçºÎʵÏֵġ£
ConsoleAppender
ConsoleAppenderÊÇ×î³£ÓõÄAppendersÖ®Ò»£¬ËüÖ»Êǽ«ÈÕÖ¾ÏûÏ¢ÏÔʾµ½¿ØÖÆÌ¨ÉÏ¡£Ðí¶àÈÕÖ¾¿ò¼Ü¶¼½«Æä×÷ΪĬÈϵÄAppender£¬²¢ÇÒÔÚ»ù±¾µÄÅäÖÃÖнøÐÐÔ¤ÅäÖá£ÀýÈ磬ÔÚLog4jÖÐConsoleAppenderµÄÅäÖòÎÊýÈçÏÂËùʾ¡£

Ò»¸öÍêÕûµÄLog4j2µÄÅäÖÃÎļþÈçÏÂËùʾ£º
<?xml version="1.0"
encoding="UTF-8"?> <Configuration
status="warn" name="MyApp">
<Appenders> <Console name="MyAppender"
target="SYSTEM_OUT"> <PatternLayout
pattern="%m%n"/> </Console>
</Appenders> <Loggers> <Root
level="error"> <AppenderRef
ref="MyAppender"/> </Root>
</Loggers> </Configuration> |
Õâ¸öÅäÖÃÎļþ´´½¨ÁËÒ»¸öÃûΪMyAppenderµÄConsoleAppender£¬ËüʹÓÃPatternLayoutÀ´¶ÔÈÕ־ʼþ½øÐиñʽ»¯£¬È»ºóÔÙ½«ÆäÊä³öµ½System.out¡£<Loggers>ÔªËØ¶Ô¶¨ÒåÔÚ³ÌÐò´úÂëÖеÄLoggers½øÐÐÁËÅäÖá£ÔÚÕâÀÎÒÃÇÖ»ÅäÖÃÁËÒ»¸öLoggerConfig£¬¼´ÃûΪRootµÄLogger£¬Ëü»á½ÓÊÕÄÄЩÈÕÖ¾¼¶±ðÔÚERRORÒÔÉϵÄÈÕÖ¾ÏûÏ¢¡£Èç¹ûÎÒÃÇʹÓÃlogger.error()À´¼Ç¼һ¸öÏûÏ¢£¬ÄÇôËü¾Í»á³öÏÖÔÚ¿ØÖÆÌ¨ÉÏ£¬¾ÍÏñÕâÑù£º
An unexpected
error occurred. |
ÄãÒ²¿ÉÒÔʹÓÃLogbackʵÏÖÍêȫһÑùµÄЧ¹û£º
<configuration>
<appender name="MyAppender" class="ch.qos.Logback.core.ConsoleAppender">
<encoder> <pattern>%m%n</pattern>
</encoder> </appender> <root
level="error"> <appender-ref
ref="MyAppender" /> </root>
</configuration> |
FileAppenders
FileAppenders½«ÈÕÖ¾¼Ç¼дÈëµ½ÎļþÖУ¬Ëü¸ºÔð´ò¿ª¡¢¹Ø±ÕÎļþ£¬ÏòÎļþÖÐ×·¼ÓÈÕÖ¾¼Ç¼£¬²¢¶ÔÎļþ½øÐмÓËø£¬ÒÔÃâÊý¾Ý±»ÆÆ»µ»òÕ߸²¸Ç¡£
ÔÚLog4jÖУ¬Èç¹ûÏë´´½¨Ò»¸öFileAppender£¬ÐèÒªÖ¸¶¨Ä¿±êÎļþµÄÃû×Ö£¬Ð´È뷽ʽÊÇ×·¼Ó»¹ÊǸ²¸Ç£¬ÒÔ¼°ÊÇ·ñÐèÒªÔÚдÈëÈÕ־ʱ¶ÔÎļþ½øÐмÓËø£º
...
...
<Appenders> <File name="MyFileAppender"
fileName="myLog.log" append="true"
locking="true"> <PatternLayout
pattern="%m%n"/> </File>
</Appenders>
... |
ÕâÑùÎÒÃÇ´´½¨ÁËÒ»¸öÃûΪMyFileAppenderµÄFileAppender£¬²¢ÇÒÔÚÏòÎļþÖÐ×·¼ÓÈÕ־ʱ»á¶ÔÎļþ½øÐмÓËø²Ù×÷¡£
Èç¹ûʹÓÃLogback£¬Äã¿ÉÒÔͬʱÆôÓÃprudentģʽÀ´±£Ö¤ÎļþµÄÍêÕûÐÔ¡£ËäÈ»PrudentģʽÔö¼ÓÁËдÈëÎļþËù»¨·ÑµÄʱ¼ä£¬µ«Ëü¿ÉÒÔ±£Ö¤ÔÚ¶à¸öFileAppenderÉõÖÁ¶à¸öJava³ÌÐòÏòͬһ¸öÎļþдÈëÈÕ־ʱ£¬ÎļþµÄÍêÕûÐÔ¡£
...
<appender name="FileAppender" class="ch.qos.Logback.core.FileAppender">
<file>myLog.log</file> <append>true</append>
<prudent>true</prudent> <encoder>
<pattern>%m%n</pattern> </encoder>
</appender>
... |
SyslogAppender
SyslogAppenders½«ÈÕÖ¾¼Ç¼·¢Ë͸ø±¾µØ»òÕßÔ¶³ÌϵͳµÄÈÕÖ¾·þÎñ¡£syslogÊÇÒ»¸ö½ÓÊÕÈÕ־ʼþ·þÎñ£¬ÕâЩÈÕ־ʼþÀ´×Ô²Ù×÷ϵͳ¡¢½ø³Ì¡¢ÆäËü·þÎñ»òÕ߯äËüÉ豸¡£Ê¼þµÄ·¶Î§¿ÉÒÔ´ÓÕï¶ÏÐÅÏ¢µ½Óû§µÇ¼Ӳ¼þʧ°ÜµÈ¡£syslogµÄʼþ°´ÕÕÉ豸½øÐзÖÀ࣬ËüÖ¸¶¨ÁËÕýÔڼǼµÄʼþµÄÀàÐÍ¡£ÀýÈ磬auth
facility±íÃ÷Õâ¸öʼþÊǺͰ²È«ÒÔ¼°ÈÏÖ¤Óйء£
Log4jºÍLogback¶¼ÄÚÖÃÖ§³ÖSyslogAppenders¡£ÔÚLog4jÖУ¬ÎÒÃÇ´´½¨SyslogAppenderʱ£¬ÐèÒªÖ¸¶¨syslog·þÎñ¼àÌýµÄÖ÷»úºÅ¡¢¶Ë¿ÚºÅÒÔ¼°ÐÒé¡£ÏÂÃæµÄʾÀýÑÝʾÁËÈçºÎÉ趨װÖãº
...
<Appenders> <Syslog name="SyslogAppender"
host="localhost" port="514"
protocol="UDP" facility="Auth"
/>
</Appenders>
... |
ÔÚLogbackÖУ¬ÎÒÃÇ¿ÉÒÔʵÏÖͬÑùµÄЧ¹û£º
...
<appender name="SyslogAppender" class="ch.qos.Logback.classic.net.SyslogAppender">
<syslogHost>localhost</syslogHost>
<port>514</port> <facility>Auth</facility>
</appender>
... |
ÆäËüAppender
ÎÒÃÇÒѾ½éÉÜÁËһЩ¾³£Óõ½µÄAppenders£¬»¹ÓкܶàÆäËüAppender¡£ËüÃÇÌí¼ÓÁËй¦ÄÜ»òÕßÔÚÆäËüµÄһЩAppender»ù´¡ÉÏʵÏÖÁËй¦ÄÜ¡£ÀýÈ磬Log4jÖеÄRollingFileAppenderÀ©Õ¹ÁËFileAppender£¬Ëü¿ÉÒÔÔÚÂú×ãÌØ¶¨Ìõ¼þʱ×Ô¶¯´´½¨ÐµÄÈÕÖ¾Îļþ£»SMTPAppender»á½«ÈÕÖ¾ÄÚÈÝÒÔÓʼþµÄÐÎʽ·¢ËͳöÈ¥£»FailoverAppender»áÔÚ´¦ÀíÈÕÖ¾µÄ¹ý³ÌÖУ¬Èç¹ûÒ»¸ö»òÕß¶à¸öAppenderʧ°Ü£¬×Ô¶¯Çл»µ½ÆäËûAppenderÉÏ¡£
Èç¹ûÏëÁ˽â¸ü¶à¹ØÓÚÆäËûAppenderµÄÐÅÏ¢£¬¿ÉÒԲ鿴Log4j Appender²Î¿¼ÒÔ¼°Logback
Appender²Î¿¼¡£
Layouts
Layouts½«ÈÕÖ¾¼Ç¼µÄÄÚÈÝ´ÓÒ»ÖÖÊý¾ÝÐÎʽת»»³ÉÁíÍâÒ»ÖÖ¡£ÈÕÖ¾¿ò¼ÜΪ´¿Îı¾¡¢HTML¡¢syslog¡¢XML¡¢JSON¡¢ÐòÁл¯ÒÔ¼°ÆäËüÈÕÖ¾ÌṩÁËLayouts¡£
Çë×¢Ò⣺ÔÚjava.util.loggingÖÐLayoutsÒ²±»³ÆÎªFormatters¡£
ÀýÈ磬java.util.loggingÌṩÁËÁ½ÖÖLayouts£ºSimpleFormatterºÍXMLFormatter¡£Ä¬ÈÏÇé¿öÏ£¬ConsoleHandlersʹÓÃSimpleFormatter£¬ËüÊä³öµÄ´¿Îı¾ÈÕÖ¾¼Ç¼¾ÍÏñÕâÑù£º
Mar 31, 2015
10:47:51 AM MyClass main
SEVERE: An exception occurred. |
¶øÄ¬ÈÏÇé¿öÏ£¬FileHandlersʹÓÃXMLFormatter£¬ËüµÄÊä³ö¾ÍÏñÕâÑù£º
<?xml version="1.0"
encoding="UTF-8" standalone="no"?>
<!DOCTYPE log SYSTEM "logger.dtd">
<log>
<record> <date>2015-03-31T10:47:51</date>
<millis>1427903275893</millis>
<sequence>0</sequence> <logger>MyClass</logger>
<level>SEVERE</level> <class>MyClass</class>
<method>main</method> <thread>1</thread>
<message>An exception occurred.</message>
</record>
</log> |
ÅäÖÃLayout
ÎÒÃÇͨ³£Ê¹ÓÃÅäÖÃÎļþ¶ÔLayouts½øÐÐÅäÖᣴÓJava 7¿ªÊ¼£¬ÎÒÃÇÒ²¿ÉÒÔʹÓÃsystem propertyÀ´ÅäÖÃSimpleFormatter¡£
ÀýÈ磬ÔÚLog4jºÍLogbackÖÐ×î³£ÓõÄLayoutsÊÇPatternLayout¡£Ëü¿ÉÒÔÈÃÄã¾ö¶¨ÈÕ־ʼþÖеÄÄÄЩ²¿·ÖÐèÒªÊä³ö£¬ÕâÊÇͨ¹ýת»»Ä£Ê½£¨Conversion
Pattern£©Íê³ÉµÄ£¬×ª»»Ä£Ê½ÔÚÿһÌõÈÕ־ʼþµÄÊý¾ÝÖаçÑÝÁË¡°Õ¼Î»·û¡±µÄ½ÇÉ«¡£ÀýÈ磬Log4jĬÈϵÄPatternLayoutʹÓÃÁËÈçÏÂת»»Ä£Ê½£º
<PatternLayout
pattern="%d{HH:mm:ss.SSS} [%t] %-5level %logger{36}
- %msg%n"/> |
%d{HH:mm:ss.SSS} ½«ÈÕÆÚת»»³Éʱ¡¢·Ö¡¢ÃëºÍºÁÃëµÄÐÎʽ£¬%levelÏÔʾÈÕ־ʼþµÄÑÏÖØ³Ì¶È£¬%CÏÔʾÉú³ÉÈÕ־ʼþµÄÀàµÄÃû×Ö£¬%tÏÔʾLoggerµÄµ±Ç°Ị̈߳¬%mÏÔʾʱ¼äµÄÏûÏ¢£¬×îºó£¬%nΪÏÂÒ»¸öÈÕ־ʼþ½øÐÐÁË»»ÐС£
¸Ä±äLayouts
Èç¹ûÔÚjava.util.loggingÖÐʹÓÃÒ»¸ö²»Í¬µÄLayout£¬ÐèÒª½«AppenderµÄformatterÊôÐÔÉèÖóÉÄãÏëÒªµÄLayout¡£ÔÚ´úÂëÖУ¬Äã¿ÉÒÔ´´½¨Ò»¸öеÄHandler£¬µ÷ÓÃsetFormatter·½·¨£¬È»ºóͨ¹ýlogger.AddHandler()·½·¨½«Handler·Åµ½LoggerÉÏÃæ¡£ÏÂÃæµÄʾÀý´´½¨ÁËÒ»¸öConsoleAppender£¬ËüʹÓÃXMLFormatterÀ´¶ÔÈÕÖ¾½øÐиñʽ»¯£¬¶ø²»ÊÇʹÓÃĬÈϵÄSimpleFormatter£º
Handler ch =
new ConsoleHandler();
ch.setFormatter(new XMLFormatter());
logger.addHandler(ch); |
ÕâÑùLogger»á½«ÏÂÃæµÄÐÅÏ¢Êä³öµ½¿ØÖÆÌ¨ÉÏ£º
<?xml version="1.0"
encoding="UTF-8" standalone="no"?>
<!DOCTYPE log SYSTEM "logger.dtd">
<log>
<record> <date>2015-03-31T10:47:51</date>
<millis>1427813271000</millis>
<sequence>0</sequence> <logger>MyClass</logger>
<level>SEVERE</level> <class>MyClass</class>
<method>main</method> <thread>1</thread>
<message>An exception occurred.</message>
</record> |
Èç¹ûÏëÁ˽â¸ü¶àÐÅÏ¢£¬Äã¿ÉÒԲ鿴Log4j Layouts²Î¿¼ÒÔ¼°Logback Layouts²Î¿¼¡£
ʹÓÃ×Ô¶¨ÒåLayouts
×Ô¶¨ÒåLayouts¿ÉÒÔÈÃÄãÖ¸¶¨AppenderÓ¦¸ÃÈçºÎÊä³öÈÕÖ¾¼Ç¼¡£´ÓJava SE 7¿ªÊ¼£¬¾¡¹ÜÄã¿ÉÒÔµ÷ÕûSimpleLoggerµÄÊä³ö£¬µ«ÓÐÒ»¸öÏÞÖÆ£¬¼´Ö»Äܹ»µ÷Õû¼òµ¥µÄ´¿Îı¾ÏûÏ¢¡£¶ÔÓÚ¸ü¸ß¼¶µÄ¸ñʽ£¬ÀýÈçHTML»òÕßJSON£¬ÄãÐèÒªÒ»¸ö×Ô¶¨ÒåLayout»òÕßÒ»¸öµ¥¶ÀµÄ¿ò¼Ü¡£
Èç¹ûÏëÁ˽â¸ü¶àʹÓÃjava.util.logging´´½¨×Ô¶¨ÒåLayoutsµÄÐÅÏ¢£¬Äã¿ÉÒԲ鿴Jakob
JenkovµÄJavaÈÕÖ¾Ö¸ÄÏÖеÄJava Logging: FormattersÕ½ڡ£
ÈÕÖ¾¼¶±ð
ÈÕÖ¾¼¶±ðÌṩÁËÒ»ÖÖ·½Ê½£¬ÎÒÃÇ¿ÉÒÔÓÃËüÀ´¸ù¾ÝÑÏÖØ³Ì¶È¶ÔÈÕÖ¾½øÐзÖÀàºÍʶ±ð¡£java.util.logging
°´ÕÕÑÏÖØ³Ì¶È´ÓÖØµ½ÇᣬÌṩÁËÒÔϼ¶±ð£º
SEVERE£¨×î¸ß¼¶±ð£©
WARNING
INFO
CONFIG
FINE
FINER
FINEST£¨×îµÍ¼¶±ð£©
ÁíÍ⣬ »¹ÓÐÁ½¸öÈÕÖ¾¼¶±ð£ºALLºÍOFF¡£ALL»áÈÃLoggerÊä³öËùÓÐÏûÏ¢£¬¶øOFFÔò»á¹Ø±ÕÈÕÖ¾¹¦ÄÜ¡£
ÉèÖÃÈÕÖ¾¼¶±ð
ÔÚÉ趨ÈÕÖ¾¼¶±ðºó£¬Logger»á×Ô¶¯ºöÂÔÄÇЩµÍÓÚÉ趨¼¶±ðµÄÈÕÖ¾ÏûÏ¢¡£ÀýÈ磬ÏÂÃæµÄÓï¾ä»áÈÃLoggerºöÂÔÄÇЩµÍÓÚWARNING¼¶±ðµÄÈÕÖ¾ÏûÏ¢£º
logger.setLevel(Level.WARNING); |
È»ºó£¬Logger»á¼Ç¼ÈκÎWARNING»òÕ߸ü¸ß¼¶±ðµÄÈÕÖ¾ÏûÏ¢¡£ÎÒÃÇÒ²¿ÉÒÔÔÚÅäÖÃÎļþÖÐÉèÖÃLoggerµÄÈÕÖ¾¼¶±ð£º
...
<Loggers> <Logger name="MyLogger"
level="warning">
... |
ת»»Ä£Ê½
Log4jºÍLogbackÖеÄPatternLayoutÀà¶¼Ö§³Öת»»Ä£Ê½£¬Ëü¾ö¶¨ÁËÎÒÃÇÈçºÎ´ÓÿһÌõÈÕ־ʼþÖÐÌáÈ¡ÐÅÏ¢ÒÔ¼°ÈçºÎ¶ÔÐÅÏ¢½øÐиñʽ»¯¡£ÏÂÃæÏÔʾÁËÕâЩģʽµÄÒ»¸ö×Ó¼¯£¬¶ÔÓÚLog4jºÍLogbackÀ´Ëµ£¬ËäÈ»ÕâÐ©ÌØ¶¨µÄ×ֶζ¼ÊÇÒ»ÑùµÄ£¬µ«ÊDz¢²»ÊÇËùÓеÄ×ֶζ¼»áʹÓÃÏàͬµÄģʽ¡£ÏëÒªÁ˽â¸ü¶àÐÅÏ¢£¬¿ÉÒԲ鿴Log4jºÍLogbackµÄPatternLayoutÎĵµ¡£

ÀýÈ磬ÏÂÃæµÄPatternLayout»áÔÚÖÐÀ¨ºÅÄÚxÏÔʾÈÕÖ¾¼¶±ð£¬ºóÃæÊÇÏß³ÌÃû×ÖºÍÈÕ־ʼþµÄÏûÏ¢£º
ÏÂÃæÊÇʹÓÃÁËÉÏÊöת»»Ä£Ê½ºóµÄÈÕÖ¾Êä³öʾÀý£º
[INFO] main: initializing
worker threads
[DEBUG] worker: listening on port 12222[INFO]
worker: received request from 192.168.1.200[ERROR]
worker: unknown request ID from 192.168.1.200 |
¼Ç¼ջ¸ú×ÙÐÅÏ¢
Èç¹ûÄãÔÚJava³ÌÐòÖÐʹÓùýÒì³££¬ÄÇôºÜÓпÉÄÜÒѾ¿´µ½¹ýÕ»¸ú×ÙÐÅÏ¢¡£ËüÌṩÁËÒ»¸ö³ÌÐòÖз½·¨µ÷ÓõĿìÕÕ£¬ÈÃÄã׼ȷ¶¨Î»³ÌÐòÖ´ÐеÄλÖá£ÀýÈ磬ÏÂÃæµÄÕ»¸ú×ÙÐÅÏ¢ÊdzÌÐòÊÔͼ´ò¿ªÒ»¸ö²»´æÔÚµÄÎļþºóÉú³ÉµÄ£º
[ERROR] main: Unable to
open file! java.io.FileNotFoundException: foo.file
(No such file or directory)
at java.io.FileInputStream.open(Native Method)
~[?:1.7.0_79]
at java.io.FileInputStream.<init>(FileInputStream.java:146)
~[?:1.7.0_79]
at java.io.FileInputStream.<init>(FileInputStream.java:101)
~[?:1.7.0_79]
at java.io.FileReader.<init>(FileReader.java:58)
~[?:1.7.0_79]
at FooClass.main(FooClass.java:47) |
Õâ¸öʾÀýʹÓÃÁËÒ»¸öÃûΪFooClassµÄÀ࣬Ëü°üº¬Ò»¸ömain·½·¨¡£ÔÚ³ÌÐòµÚ47ÐУ¬FileReader¶ÀÏíÊÔͼ´ò¿ªÒ»¸öÃûΪfoo.fileµÄÎļþ£¬ÓÉÓÚÔÚ³ÌÐòĿ¼ÏÂûÓÐÃû×ÖÊÇfoo.fileµÄÎļþ£¬Òò´ËJavaÐéÄâ»úÅ׳öÁËÒ»¸öFileNotFoundException¡£ÒòΪÕâ¸ö·½·¨µ÷Óñ»·Åµ½ÁËtry-catchÓï¿éÖУ¬ËùÒÔÎÒÃÇÄܹ»²¶»ñÕâ¸öÒì³£²¢¼Ç¼Ëü£¬»òÕßÖÁÉÙ¿ÉÒÔ×èÖ¹³ÌÐò±ÀÀ£¡£
ʹÓÃPatternLayout¼Ç¼ջ¸ú×ÙÐÅÏ¢
ÔÚд±¾ÆªÎÄÕÂʱ×îа汾µÄLog4jºÍLogbackÖУ¬Èç¹ûÔÚLayoutÖÐûÓкͿÉÅ×Òì³£Ïà¹ØµÄÐÅÏ¢£¬ÄÇô¶¼»á×Ô¶¯½«%xEx£¨ÕâÖÖÕ»¸ú×ÙÐÅÏ¢°üº¬ÁËÿ´Î·½·¨µ÷ÓõİüÐÅÏ¢£©Ìí¼Óµ½PatternLayoutÖС£Èç¹û¶ÔÓÚÆÕͨµÄÈÕÖ¾ÐÅÏ¢µÄģʽÈçÏ£º
Ëü»á±äΪ£º
ÕâÑù²»½ö½ö´íÎóÐÅÏ¢»á±»¼Ç¼ÏÂÀ´£¬ÍêÕûµÄÕ»¸ú×ÙÐÅÏ¢Ò²»á±»¼Ç¼£º
[ERROR] main:
Unable to open file! java.io.FileNotFoundException:
foo.file (No such file or directory)
at java.io.FileInputStream.open(Native Method)
~[?:1.7.0_79]
at java.io.FileInputStream.<init>(FileInputStream.java:146)
~[?:1.7.0_79]
at java.io.FileInputStream.<init>(FileInputStream.java:101)
~[?:1.7.0_79]
at java.io.FileReader.<init>(FileReader.java:58)
~[?:1.7.0_79]
at FooClass.main(FooClass.java:47) |
%xExÖеİü²éѯÊÇÒ»¸ö´ú¼Û°º¹óµÄ²Ù×÷£¬Èç¹ûÄãÆµ·±µÄ¼Ç¼Òì³£ÐÅÏ¢£¬ÄÇô¿ÉÄÜ»áÅöµ½ÐÔÄÜÎÊÌ⣬ÀýÈ磺
// ...
} catch (FileNotFoundException ex) {
logger.error(¡°Unable to open file!¡±, ex);
} |
Ò»ÖÖ½â¾ö·½·¨ÊÇÔÚģʽÖÐÏÔʽµÄ°üº¬%ex£¬ÕâÑù¾ÍÖ»»áÇëÇóÒì³£µÄÕ»¸ú×ÙÐÅÏ¢£º
ÁíÍâÒ»ÖÖ·½·¨ÊÇͨ¹ý×·¼Ó%xEx(none)µÄ·½·¨Åųý£¨ÔÚLog4j£©ÖÐËùÓеÄÒì³£ÐÅÏ¢£º
»òÕßÔÚLogbackÖÐʹÓÃ%nopex£º
ʹÓýṹ»¯²¼¾ÖÊä³öÕ»¸ú×ÙÐÅÏ¢
ÈçÄãÔÚ¡°½âÎö¶àÐÐÕ»¸ú×ÙÐÅÏ¢¡±Ò»½ÚÖÐËù¼û£¬¶ÔÓÚÕ¾¸ú×ÙÐÅÏ¢À´Ëµ£¬Ê¹Óýṹ»¯²¼¾ÖÀ´¼Ç¼ÊÇ×îºÏÊʵķ½Ê½£¬ÀýÈçJSONºÍXML¡£
ÕâЩ²¼¾Ö»á×Ô¶¯½«Õ»¸ú×ÙÐÅÏ¢°´ÕÕºËÐÄ×é¼þ½øÐзֽ⣬ÕâÑùÎÒÃÇ¿ÉÒÔºÜÈÝÒ×½«Æäµ¼³öµ½ÆäËû³ÌÐò»òÕßÈÕÖ¾·þÎñÖС£¶ÔÓÚÉÏÊöÕ¾¸ú×ÙÐÅÏ¢£¬Èç¹ûʹÓÃJSON¸ñʽ£¬²¿·ÖÐÅÏ¢ÏÔʾÈçÏ£º
...
"loggerName" : "FooClass",
"message" : "Foo, oh no! ",
"thrown" : { "commonElementCount"
: 0, "localizedMessage" : "foo.file
(No such file or directory)", "message"
: "foo.file (No such file or directory)",
"name" : "java.io.FileNotFoundException",
"extendedStackTrace" : [ { "class"
: "java.io.FileInputStream", "method"
: "open", "file" : "FileInputStream.java",
... |
¼Ç¼δ²¶»ñÒì³£
ͨ³£Çé¿öÏ£¬ÎÒÃÇͨ¹ý²¶»ñµÄ·½Ê½À´´¦ÀíÒì³£¡£Èç¹ûÒ»¸öÒ쳣ûÓб»²¶»ñ£¬ÄÇôËü¿ÉÄܻᵼÖ³ÌÐòÖÕÖ¹¡£Èç¹ûÄܹ»Áô´æÈκÎÈÕÖ¾£¬ÄÇôÕâÊÇÒ»¸ö¿ÉÒÔ°ïÖúÎÒÃǵ÷ÊÔΪʲô»á·¢ÉúÒì³£µÄºÃ°ì·¨£¬ÕâÑùÄã¾Í¿ÉÒÔÕÒµ½·¢ÉúÒì³£µÄ¸ù±¾ÔÒò²¢½â¾öËü¡£ÏÂÃæÀ´ËµÃ÷ÎÒÃÇÈçºÎ½¨Á¢Ò»¸öĬÈϵÄÒì³£´¦ÀíÆ÷À´¼Ç¼ÕâЩ´íÎó¡£
ThreadÀàÖÐÓÐÁ½¸ö·½·¨£¬ÎÒÃÇ¿ÉÒÔÓÃËüÀ´ÎªÎ´²¶»ñµÄÒì³£Ö¸¶¨Ò»¸öExceptionHandler£º
setDefaultUncaughtExceptionHandler ¿ÉÒÔÈÃÄãÔÚÈκÎÏß³ÌÉÏ´¦ÀíÈκÎÒì³£¡£setUncaughtExceptionHandler¿ÉÒÔÈÃÄãÕë¶ÔÒ»¸öÖ¸¶¨µÄÏß³ÌÉ趨һ¸ö²»Í¬µÄ´¦Àí·½·¨¡£¶øThreadGroupÔòÔÊÐíÄãÉ趨һ¸ö´¦Àí·½·¨¡£´ó²¿·ÖÈË»áʹÓÃĬÈϵÄÒì³£´¦Àí·½·¨¡£
ÏÂÃæÊÇÒ»¸öʾÀý£¬ËüÉ趨ÁËÒ»¸öĬÈϵÄÒì³£´¦Àí·½·¨£¬À´´´½¨Ò»¸öÈÕ־ʼþ¡£ËüÒªÇóÄã´«ÈëÒ»¸öUncaughtExceptionHandler£º
import java.util.logging.*;
public class ExceptionDemo {
private static final Logger logger = Logger.getLogger(ExceptionDemo.class);
public static void main(String[] args) {
Thread.setDefaultUncaughtExceptionHandler(new
Thread.UncaughtExceptionHandler() {
public void uncaughtException(Thread t, Throwable
e) {
logger.log(Level.SEVERE, t + " ExceptionDemo
threw an exception: ", e);
};
});
class adminThread implements Runnable {
public void run() {
throw new RuntimeException();
}
}
Thread t = new Thread(new adminThread());
t.start();
}
} |
ÏÂÃæÊÇÒ»¸öδ´¦ÀíÒì³£µÄÊä³öʾÀý£º
May 29, 2015
2:21:15 PM ExceptionDemo$1 uncaughtException
SEVERE: Thread[Thread-1,5,main] ExceptionDemo
threw an exception:
java.lang.RuntimeException
at ExceptionDemo$1adminThread.run(ExceptionDemo.java:15)
at java.lang.Thread.run(Thread.java:745) |
JSON
JSON£¨JavaScript Object Notation£©ÊÇÒ»ÖÖÓÃÀ´´æ´¢½á¹¹»¯Êý¾ÝµÄ¸ñʽ£¬Ëü½«Êý¾Ý´æ´¢³É¼üÖµ¶ÔµÄ¼¯ºÏ£¬ÀàËÆÓÚHashMap»òÕßHashtable¡£JSON¾ßÓеĿÉÒÆÖ²ÐÔºÍͨÓÃÐÔ£¬´ó²¿·ÖÏÖ´úÓïÑÔ¶¼ÄÚÖÃÖ§³ÖËü»òÕßͨ¹ýÒѾ׼±¸ºÃµÄµÚÈý·½Àà¿âÀ´Ö§³ÖËü¡£
JSONÖ§³ÖÐí¶à»ù±¾Êý¾ÝÀàÐÍ£¬°üÀ¨×Ö·û´®¡¢Êý×Ö¡¢²¼¶û¡¢Êý×éºÍnull¡£ÀýÈ磬Äã¿ÉÒÔʹÓÃÏÂÃæµÄJSON¸ñʽÀ´±íʾһ¸öµçÄÔ£º
{ "manufacturer":
"Dell", "model": "Inspiron",
"hardware": { "cpu": "Intel
Core i7", "ram": 16384,
¡°cdrom¡±: null
}, "peripherals": [
{ "type": "monitor",
"manufacturer": "Acer",
"model": "S231HL"
}
]
} |
JSONµÄ¿ÉÒÆÖ²ÐÔʹµÃËü·Ç³£Êʺϴ洢ÈÕÖ¾¼Ç¼£¬Ê¹ÓÃJSONºó£¬JavaÈÕÖ¾¿ÉÒÔ±»ÈκÎÊýÄ¿µÄJSON½âÊÍÆ÷Ëù¶ÁÈ¡¡£ÒòΪÊý¾ÝÒѾÊǽṹ»¯µÄ£¬ËùÒÔ½âÎöJSONÈÕÖ¾ÒªÔ¶±È½âÎö´¿Îı¾ÈÕÖ¾ÈÝÒס£
JavaÖеÄJSON
¶ÔÓÚJavaÀ´Ëµ£¬ÓдóÁ¿µÄJSONʵÏÖ£¬ÆäÖÐÒ»¸öÊÇJSON.simple¡£JSON.simpleÊÇÇáÁ¿¼¶µÄ¡¢Ò×ÓÚʹÓ㬲¢ÇÒÈ«²¿·ûºÏJSON±ê×¼¡£
Èç¹ûÏ뽫ÉÏÃæµÄcomputer¶ÔÏóת»»³É¿ÉÓõÄJava¶ÔÏó£¬ÎÒÃÇ¿ÉÒÔ´ÓÎļþÖжÁÈ¡JSONÄÚÈÝ£¬½«Æä´«µÝ¸øJSON.simple£¬È»ºó·µ»ØÒ»¸öObject£¬½Ó×ÅÎÒÃÇ¿ÉÒÔ½«Objectת»»³ÉJSONObject£º
Object computer
= JSONValue.parse(new FileReader("computer.json"));
JSONObject computerJSON = (JSONObject)computer; |
ÁíÍ⣬ΪÁËÈ¡µÃ¼üÖµ¶ÔµÄÐÅÏ¢£¬Äã¿ÉÒÔʹÓÃÈκÎÈÕÖ¾¿ò¼ÜÀ´¼Ç¼һ¸öJSONObject£¬JSONObject¶ÔÏó°üº¬Ò»¸ötoString()·½·¨£¬
Ëü¿ÉÒÔ½«JSONת»»³ÉÎı¾£º
2015-05-06
14:54:32,878 INFO JSONTest main {"peripherals"
:[{"model":"S231HL","manufacturer":"Acer", "type":"monitor"}],"
model":"Inspiron","hardware": {"cdrom":null,"ram":16384,"cpu" :"Intel
Core i7"} ,"manufacturer":"Dell"} |
ËäÈ»ÕâÑù×ö¿ÉÒÔºÜÈÝÒ׵ĴòÓ¡JSONObject£¬µ«Èç¹ûÄãʹÓýṹ»¯µÄLayouts£¬ÀýÈçJSONLayout»òÕßXMLLayout£¬¿ÉÄܻᵼÖÂÒâÏë²»µ½µÄ½á¹û£º
...
"message" : "{"peripherals":
[{"model":"S231HL","manufacturer":"Acer",
"type":"monitor"}], "model"
:"Inspiron","hardware": {"cdrom":null,"ram":16384,
"cpu":"Intel Core i7"}, "manufacturer":"Dell"}",
... |
Log4jÖеÄJSONLayout²¢Ã»ÓÐÄÚÖÃÖ§³ÖÄÚǶJSON¶ÔÏ󣬵«Äã¿ÉÒÔͨ¹ý´´½¨×Ô¶¨ÒåLayoutµÄ·½Ê½À´Ìí¼ÓÒ»¸öJSONObject×ֶΣ¬Õâ¸öLayout»á¼Ì³Ð»òÕßÌæ»»JSONLayout¡£È»¶ø£¬Èç¹ûÄãʹÓÃÒ»¸öÈÕÖ¾¹ÜÀíϵͳ£¬ÐèÒª¼ÇסÐí¶àÈÕÖ¾¹ÜÀíϵͳ»áÕë¶ÔijЩ×Ö¶ÎʹÓÃÔ¤¶¨ÒåµÄÊý¾ÝÀàÐÍ¡£Èç¹ûÄã´´½¨Ò»¸öLayout²¢½«JSONObject´æ´¢µ½message×Ö¶ÎÖУ¬ÄÇôËü¿ÉÄÜ»áºÍÈÕ־ϵͳÖÐʹÓõÄStringÊý¾ÝÀàÐÍÏà³åÍ»¡£Ò»ÖÖ½â¾ö°ì·¨Êǽ«JSONÊý¾Ý´æ´¢µ½Ò»¸ö×Ö¶ÎÖУ¬È»ºó½«×Ö·û´®ÀàÐ͵ÄÈÕÖ¾ÏûÏ¢´æ´¢µ½ÁíÍâÒ»¸ö×Ö¶ÎÖС£
ÆäËüJSON¿â
³ýÁËJSON.simple£¬JavaÖл¹ÓкܶàÆäËüJSON¿â¡£JSON-javaÊÇÓÉJSON´´½¨Õß¿ª·¢µÄÒ»¸ö²Î¿¼ÊµÏÖ£¬Ëü°üº¬Á˶îÍâµÄһЩ¹¦ÄÜ£¬¿ÉÒÔת»»ÆäËüÊý¾ÝÀàÐÍ£¬°üÀ¨webÔªËØ¡£µ«ÊÇĿǰJSON-javaÒѾûÓÐÈËÀ´Î¬»¤ºÍÌṩ֧³ÖÁË¡£
Èç¹ûÏ뽫JSON¶ÔÏóת»»³ÉJava¶ÔÏó»òÕßÄæÏòת»»£¬GoogleÌṩÁËÒ»¸öGson¿â¡£Ê¹ÓÃGsonʱ£¬¿ÉÒԺܼòµ¥Ê¹ÓÃ
toJson() ºÍ fromJson() ·½·¨À´½âÎöJSON£¬ÕâÁ½¸ö·½·¨·Ö±ðÓÃÀ´½«Java¶ÔÏóת»»³ÉJSON×Ö·û´®ÒÔ¼°½«JSON×Ö·û´®×ª»»³ÉJava¶ÔÏó¡£GsonÉõÖÁ¿ÉÒÔÓ¦ÓÃÔÚÄÚ´æ¶ÔÏóÖУ¬ÔÊÐíÄãÓ³Éäµ½ÄÇЩûÓÐÔ´´úÂëµÄ¶ÔÏóÉÏ¡£
Jackson
JacksonÊÇÒ»¸öÇ¿´óµÄ¡¢Á÷Ðеġ¢¹¦ÄܷḻµÄ¿â£¬Ëü¿ÉÒÔÔÚJavaÖйÜÀíJSON¶ÔÏó¡£ÓÐһЩ¿ò¼ÜÉõÖÁʹÓÃJackson×÷ΪËüÃǵÄJSONLayouts¡£¾¡¹ÜËüºÜ´ó²¢ÇÒ¸´ÔÓ£¬µ«Jackson¶ÔÓÚ³õѧÕߺ͸߼¶Óû§À´Ëµ£¬ÊǺÜÈÝÒ×ʹÓõġ£
Logbackͨ¹ýlogback-jacksonºÍlogback-json-classic¿â¼Ì³ÐÁËJackson£¬ÕâÁ½¸ö¿âÒ²ÊÇlogback-contribÏîÄ¿µÄÒ»²¿·Ö¡£ÔÚ¼¯³ÉÁËJacksonºó£¬Äã¿ÉÒÔ½«ÈÕÖ¾ÒÔJSONµÄ¸ñʽµ¼³öµ½ÈκÎAppenderÖС£
Logback WikiÏêϸ½âÊÍÁËÈçºÎ½«JSONÌí¼Óµ½logbackÖУ¬ÔÚWikiÒ³ÃæÖеÄʾÀýʹÓÃÁËLogglyAppender£¬ÕâÀïµÄÅäÖÃÒ²¿ÉÒÔÓ¦Óõ½ÆäËûAppenderÉÏ¡£ÏÂÃæµÄʾÀý˵Ã÷ÁËÈçºÎ½«JSON¸ñʽ»¯µÄÈÕÖ¾¼Ç¼дÈëµ½ÃûΪmyLog.jsonµÄÎļþÖУº
...
<appender name="file" class="ch.qos.Logback.core.FileAppender">
<file>myLog.json</file> <encoder
class= "ch.qos.Logback.core.encoder.LayoutWrappingEncoder">
<layout class= "ch.qos.Logback.contrib.json.classic.JsonLayout">
<jsonFormatter class= "ch.qos.Logback.contrib.jackson.JacksonJsonFormatter"/>
</layout> </encoder>
</appender>
... |
ÄãÒ²¿ÉÒÔͨ¹ýFasterXML WikiÕÒµ½¸ü¶à¹ØÓÚJacksonµÄÉî¶È½éÉÜ¡£
Á˽â¸ü¶àJSONÏà¹ØÐÅÏ¢
Äã¿ÉÒÔͨ¹ýJSONÖ÷ҳѧϰ¸ü¶àJSONÏà¹ØÐÅÏ¢£¬»òÕßͨ¹ýCodeAcademyÀ´Í¨¹ýѧϰһ¸ö½»»¥Ê½µÄ¿ìËÙÉÏÊֽ̳̣¨Çë×¢ÒâÕâ¸ö¿Î³ÌÊÇ»ùÓÚJavaScriptµÄ£¬¶ø²»ÊÇJava£©¡£ÓÐһЩÔÚÏß¹¤¾ßÀýÈçJSONLintºÍJSONÔÚÏß±à¼Æ÷¿ÉÒÔ°ïÖúÄã½âÎö¡¢ÑéÖ¤ÒÔ¼°¸ñʽ»¯JSON´úÂë¡£
NDC¡¢MDCÒÔ¼°ThreadContext
µ±´¦Àí¶àÏß³ÌÓ¦ÓóÌÐò£¬ÌرðÊÇweb·þÎñʱ£¬¸ú×Ùʼþ¿ÉÄÜ»á±äµÃÀ§ÄÑ¡£µ±Õë¶Ô¶à¸öͬʱ´æÔڵĶà¸öÓû§Éú³ÉÈÕÖ¾¼Ç¼ʱ£¬ÄãÈçºÎÇø·ÖÄĸöÐÐΪºÍÄĸöÈÕ־ʼþÓйØÄØ£¿ÈçºÎÁ½¸öÓû§Ã»Óгɹ¦´ò¿ªÒ»¸öÏàͬµÄÎļþ£¬»òÕßÔÚͬһʱ¼äûÓгɹ¦µÇ½£¬ÄÇôÔõô´¦ÀíÈÕÖ¾¼Ç¼£¿Äã¿ÉÄÜÐèÒªÒ»ÖÖ·½Ê½À´½«ÈÕÖ¾¼Ç¼ºÍ³ÌÐòÖеÄΨһ±êʾ·û¹ØÁªÆðÀ´£¬ÕâЩ±êʶ·û¿ÉÄÜÊÇÓû§ID£¬»á»°ID»òÕßÉ豸ID¡£¶øÕâ¾ÍÊÇNDC¡¢MDCÒÔ¼°ThreadContextµÄÓÃÎäÖ®µØ¡£
NDC¡¢MDCºÍThreadContextͨ¹ýÏòµ¥¶ÀµÄÈÕÖ¾¼Ç¼ÖÐÌí¼Ó¶ÀÒ»ÎÞ¶þµÄÊý¾Ý´Á£¬À´´´½¨ÈÕÖ¾×ã¼££¨log
trails£©¡£ÕâЩÊý¾Ý´ÁÒ²±»³ÆÎªÓã±ê¼Ç£¨fish tagging£©£¬ÎÒÃÇ¿ÉÒÔͨ¹ýÒ»¸ö»òÕß¶à¸ö¶ÀÒ»ÎÞ¶þµÄÖµÀ´Çø·ÖÈÕÖ¾¡£ÕâЩÊý¾Ý´ÁÔÚÿ¸öÏ̼߳¶±ðÉϽøÐйÜÀí£¬²¢ÇÒÒ»Ö±³ÖÐøµ½Ï߳̽áÊø£¬»òÕßÖ±µ½Êý¾Ý´Á±»É¾µô¡£ÀýÈ磬Èç¹ûÄãµÄWebÓ¦ÓóÌÐòΪÿ¸öÓû§Éú³ÉÒ»¸öеÄỊ̈߳¬ÄÇôÄã¿ÉÒÔʹÓÃÕâ¸öÓû§µÄIDÀ´±ê¼ÇÈÕÖ¾¼Ç¼¡£µ±ÄãÏëÔÚÒ»¸ö¸´ÔÓµÄϵͳÖиú×ÙÌØ¶¨µÄÇëÇó¡¢ÊÂÎñ»òÕßÓû§£¬ÕâÊÇÒ»Öַdz£ÓÐÓõķ½·¨¡£
ǶÌ×Õï¶ÏÉÏÏÂÎÄ£¨NDC£©
NDC»òÕßǶÌ×Õï¶ÏÉÏÏÂÎÄ£¨Nested Diagnostic Context£©ÊÇ»ùÓÚÕ»µÄ˼Ï룬ÐÅÏ¢¿ÉÒÔ±»·Åµ½Õ»ÉÏ»òÕß´ÓÕ»ÖÐÒÆ³ý¡£¶øÕ»ÖеÄÖµ¿ÉÒÔ±»Logger·ÃÎÊ£¬²¢ÇÒLoggerÎÞÐèÏÔʾÏëÈÕÖ¾·½·¨Öд«ÈëÈκÎÖµ¡£
ÏÂÃæµÄ´úÂëʾÀýʹÓÃNDCºÍLog4jÀ´½«Óû§ÐÕÃûºÍÒ»ÌõÈÕÖ¾¼Ç¼¹ØÁªÆðÀ´¡£NDCÊÇÒ»¸ö¾²Ì¬À࣬Òò´ËÎÒÃÇ¿ÉÒÔÖ±½Ó·ÃÎÊËüµÄ·½·¨£¬¶øÎÞÐèʵÀý»¯Ò»¸öNDC¶ÔÏó¡£ÔÚÕâ¸öʾÀýÖУ¬
NDC.oush(username) ºÍ NDC.push(sessionID) ·½·¨ÔÚÕ»Öд洢Á˵±Ç°µÄÓû§Ãû£¨admin£©ºÍ»á»°ID£¨1234£©£¬¶øNDC.pop()·½·¨½«Ò»Ð©Ïî´ÓÕ»ÖÐÒÆ³ý£¬NDC.remove()·½·¨ÈÃJava»ØÊÕÄڴ棬ÒÔÃâÔì³ÉÄÚ´æÒç³ö¡£
JavaÈÕÖ¾ÖÕ¼«Ö¸ÄÏ
import java.io.FileReader;
import org.apache.Log4j.Logger;
import org.apache.Log4j.NDC;
...
String username = "admin";
String sessionID = "1234";
NDC.push(username);
NDC.push(sessionID);
try {
// tmpFile doesn't exist, causing an exception.
FileReader fr = new FileReader("tmpFile");
}
catch (Exception ex) {
logger.error("Unable to open file.");
}
finally {
NDC.pop();
NDC.pop();
NDC.remove();
} |
Log4jµÄPatternLayoutÀàͨ¹ý%xת»»×Ö·û´ÓNDCÖÐÌáȡֵ¡£Èç¹ûÒ»¸öÈÕ־ʼþ±»´¥·¢£¬ÄÇôÍêÕûµÄNDCÕ»¾Í±»´«µ½Log4j£º
<PatternLayout
pattern="%x %-5p - %m%n" /> |
ÔËÐгÌÐòºó£¬ÎÒÃÇ¿ÉÒԵóöÏÂÃæµÄÊä³ö£º
"admin
1234 ERROR ¨C Unable to open file." |
Ó³ÉäÕï¶ÏÉÏÏÂÎÄ£¨MDC£©
MDC»òÕßÓ³ÉäÕï¶ÏÉÏÏÂÎĺÍNDCºÜÏàËÆ£¬²»Í¬Ö®´¦ÔÚÓÚMDC½«Öµ´æ´¢ÔÚ¼üÖµ¶ÔÖУ¬¶ø²»ÊÇÕ»ÖС£ÕâÑùÄã¿ÉÒÔºÜÈÝÒ×µÄÔÚLayoutÖÐÒýÓÃÒ»¸öµ¥¶ÀµÄ¼ü¡£MDC.put(key,value)
·½·¨½«Ò»¸öеļüÖµ¶ÔÌí¼Óµ½ÉÏÏÂÎÄÖУ¬¶ø MDC.remove(key) ·½·¨»áÒÆ³ýÖ¸¶¨µÄ¼üÖµ¶Ô¡£
Èç¹ûÏëÔÚÈÕÖ¾ÖÐͬÑùÏÔʾÓû§ÃûºÍ»á»°ID£¬ÎÒÃÇÐèҪʹÓà MDC.put() ·½·¨½«ÕâÁ½¸ö±äÁ¿´æ´¢³É¼üÖµ¶Ô£º

import java.io.FileReader;
import org.apache.Log4j.Logger;
import org.apache.Log4j.MDC;
...
MDC.put("username", "admin");
MDC.put("sessionID", "1234");
try {
// tmpFile doesn't exist, causing an exception.
FileReader fr = new FileReader("tmpFile");
}
catch (Exception ex) {
logger.error("Unable to open file!");
}
finally {
MDC.clear();
} |
ÕâÀïÔÙÒ»´ÎÇ¿µ÷£¬ÔÚ²»ÐèҪʹÓÃContextºó£¬ÎÒÃÇÐèҪʹÓà MDC.clear() ·½·¨½«ËùÓеļüÖµ¶Ô´ÓMDCÖÐÒÆ³ý£¬ÕâÑù»á½µµÍÄÚ´æµÄʹÓÃÁ¿£¬²¢×èÖ¹MDCÔÚºóÃæÊÔͼµ÷ÓÃÄÇЩÒѾ¹ýÆÚµÄÊý¾Ý¡£
ÔÚÈÕÖ¾¿ò¼ÜÖзÃÎÊMDCµÄֵʱ£¬Ò²ÉÔ΢ÓÐÐ©Çø±ð¡£¶ÔÓÚ´æ´¢ÔÚÉÏÏÂÎÄÖеÄÈκμü£¬ÎÒÃÇ¿ÉÒÔʹÓÃ%X(¼ü)µÄ·½Ê½À´·ÃÎʶÔÓ¦µÄÖµ¡£ÕâÑù£¬ÎÒÃÇ¿ÉÒÔʹÓÃ
%X(username) ºÍ %X(sessionID) À´»ñÈ¡¶ÔÓ¦µÄÓû§ÃûºÍ»á»°ID£º
<PatternLayout
pattern="%X{username} %X{sessionID} %-5p
- %m%n" />
"admin 1234 ERROR ¨C Unable to open file!" |
Èç¹ûÎÒÃÇûÓÐÖ¸¶¨Èκμü£¬ÄÇôMDCÉÏÏÂÎľͻᱻÒÔ {(key, value),(key, value)}
µÄ·½Ê½´«µÝ¸øAppender¡£
LogbackÖеÄNDCºÍMDC
ºÍLog4j²»Í¬£¬LogbackÄÚÖÃûÓÐʵÏÖNDC¡£µ«ÊÇslf4j-ext°üÌṩÁËÒ»¸öNDCʵÏÖ£¬ËüʹÓÃMDC×÷Ϊ»ù´¡¡£ÔÚLogbackÄÚ²¿£¬Äã¿ÉÒÔʹÓÃ
MDC.put()¡¢MDC.remove() ºÍ MDC.clear() ·½·¨À´·ÃÎʺ͹ÜÀíMDC£º
import org.slf4j.MDC;
...
Logger logger = LoggerFactory.getLogger(MDCLogback.class);
...
MDC.put("username", "admin");
MDC.put("sessionID", "1234");
try {
FileReader fr = new FileReader("tmpFile");
}
catch (Exception ex) {
logger.error("Unable to open file.");
}
finally {
MDC.clear();
} |
ÔÚLogbackÖУ¬Äã¿ÉÒÔÔÚLogback.xmlÖн«ÈçÏÂģʽӦÓõ½AppenderÉÏ£¬Ëü¿ÉÒÔÊä³öºÍÉÏÃæLog4jÏàͬµÄ½á¹û£º
<Pattern>[%X{username}]
%X{sessionID} %-5p - %m%n</Pattern>
"[admin] 1234 ERROR - Unable to open file." |
Õë¶ÔMDCµÄ·ÃÎʲ¢²»½ö½öÏÞÖÆÔÚPatternLayoutÉÏ£¬ÀýÈ磬µ±Ê¹ÓÃJSONFormatterʱ£¬MDCÖеÄËùÓÐÖµ¶¼»á±»µ¼³ö£º
ThreadContext
Version 2 of Log4j merged MDC and NDC into a single
concept known as the Thread Context. The Thread Context
is an evolution of MDC and NDC, presenting them respectively
as the Thread Context Map and Thread Context Stack.
The Thread Context is managed through the static ThreadContext
class, which is implemented similar to Log4j 1¡¯s MDC
and NDC classes.
Log4j°æ±¾2Öн«MDCºÍNDCºÏ²¢µ½Ò»¸öµ¥¶ÀµÄ×é¼þÖУ¬Õâ¸ö×é¼þ±»³ÆÎªÏß³ÌÉÏÏÂÎÄ¡£Ïß³ÌÉÏÏÂÎÄÊÇÕë¶ÔMDCºÍNDCµÄ½ø»¯£¬Ëü·Ö±ðÓÃÏß³ÌÉÏÏÂÎÄMapÓ³ÉäÏß³ÌÉÏÏÂÎÄÕ»À´±íʾMDCºÍNDC¡£ÎÒÃÇ¿ÉÒÔͨ¹ýThreadContext¾²Ì¬ÀàÀ´¹ÜÀíÏß³ÌÉÏÏÂÎÄ£¬Õâ¸öÀàÔÚʵÏÖÉÏÀàËÆÓÚLog4j°æ±¾1ÖеÄMDCºÍNDC¡£
When using the Thread Context Stack, data is pushed
to and popped from a stack just like with NDC:
µ±Ê¹ÓÃÏß³ÌÉÏÏÂÎÄջʱ£¬ÎÒÃÇ¿ÉÒÔÏòNDCÄÇÑùÏòÕ»ÖÐÌí¼Ó»òÕßɾ³ýÊý¾Ý£º
import org.apache.logging.Log4j.ThreadContext;
...
ThreadContext.push(username);
ThreadContext.push(sessionID);
// Logging methods go here
ThreadContext.pop();
... |
µ±Ê¹ÓÃÏß³ÌÉÏÏÂÎÄÓ³Éäʱ£¬ÎÒÃÇ¿ÉÒÔÏñMDCÄÇÑù½«ÖµºÍ¼ü½áºÏÔÚÒ»Æð£º
import org.apache.logging.Log4j.ThreadContext;
...
ThreadContext.put(¡°username¡±,"admin");
ThreadContext.put("sessionID", "1234");
// Logging methods go here
ThreadContext.clearMap();
... |
ThreadContextÀàÌṩÁËһЩ·½·¨£¬ÓÃÓÚÇå³ýÕ»¡¢Çå³ýMDC¡¢Çå³ý´æ´¢ÔÚÉÏÏÂÎÄÖеÄËùÓÐÖµ£¬¶ÔÓ¦µÄ·½·¨ÊÇThreadContext.clearAll()¡¢ThreadContext.clearMap()ºÍThreadContext.clearStack()¡£
ºÍÔÚMDCÒÔ¼°NDCÖÐÒ»Ñù£¬ÎÒÃÇ¿ÉÒÔʹÓÃLayoutsÔÚÏß³ÌÉÏÏÂÎÄÖзÃÎÊÕâЩֵ¡£Ê¹ÓÃPatternLayoutʱ£¬%xת»»Ä£Ê½»á´ÓÕ»Öлñȡֵ£¬%XºÍ%X(¼ü)»á´ÓͼÖлñȡֵ¡£
ThreadContext¹ýÂË
һЩ¿ò¼ÜÔÊÐíÄã»ùÓÚijЩÊôÐÔ¶ÔÈÕÖ¾½øÐйýÂË¡£ÀýÈ磬Log4jµÄDynamicThresholdFilter
»áÔÚ¼üÂú×ãÌØ¶¨Ìõ¼þµÄÇé¿öÏ£¬×Ô¶¯µ÷ÕûÈÕÖ¾¼¶±ð¡£ÔÙ±ÈÈ磬Èç¹ûÎÒÃÇÏëÒª´¥·¢TRACE¼¶±ðµÄÈÕÖ¾ÏûÏ¢£¬ÎÒÃÇ¿ÉÒÔ´´½¨Ò»¸öÃûΪtrace-logging-enabledµÄ¼ü£¬²¢Ïòlog4jÅäÖÃÎļþÖÐÌí¼ÓÒ»¸ö¹ýÂËÆ÷£º
<Configuration
name="MyApp">
<DynamicThresholdFilter key="trace-logging-enabled"
onMatch="ACCEPT" onMismatch="NEUTRAL">
<KeyValuePair key="true" value="TRACE"
/>
</DynamicThresholdFilter>
... |
Èç¹ûThreadContext°üº¬Ò»¸öÃûΪtrace-logging-enabledµÄ¼ü£¬onMatch
ºÍ onMismatch »á¾ö¶¨ÈçºÎ´¦ÀíËü¡£¹ØÓÚ onMatch ºÍ onMismatch£¬ÎÒÃÇÓÐÈý¸ö¿ÉÑ¡ÏACCEPT£¬Ëü»á´¦Àí¹ýÂËÆ÷µÄ¹æÔò£»DENY£¬Ëü»áºöÂÔ¹ýÂËÆ÷µÄ¹æÔò£»NEUTRAL£¬Ëü»áÍÆ³Ùµ½ÏÂÒ»¸ö¹ýÂËÆ÷¡£³ýÁËÕâЩ£¬ÎÒÃÇ»¹¶¨ÒåÒ»¸ö¼üÖµ¶Ô£¬µ±ÖµÎªtrueʱ£¬ÎÒÃÇÆôÓÃTRACE¼¶±ðµÄÈÕÖ¾¡£
ÏÖÔÚ£¬µ±trace-logging-enabled±»ÉèÖóÉtrueʱ£¬¼´Ê¹¸ùLoggerÉèÖõÄÈÕÖ¾¼¶±ð¸ßÓÚTRACE£¬AppenderÒ²»á¼Ç¼TRACE¼¶±ðµÄÏûÏ¢¡£
Äã¿ÉÄÜ»¹Ïë¹ýÂËÒ»Ð©ÌØ¶¨µÄÈÕÖ¾µ½Ìض¨µÄAppenderÖУ¬Log4jÖÐÌṩÁËThreadContextMapFilterÀ´ÊµÏÖÕâÒ»µã¡£Èç¹ûÎÒÃÇÏëÒªÏÞ֯ij¸öÌØ¶¨µÄAppender£¬Ö»¼Ç¼Õë¶Ôij¸öÓû§µÄTRACE¼¶±ðµÄÏûÏ¢£¬ÎÒÃÇ¿ÉÒÔ»ùÓÚusername¼üÌí¼ÓÒ»¸öThreadContextMapFilter£º
<Console
name="ConsoleAppender" target="SYSTEM_OUT">
<ThreadContextMapFilter onMatch="ACCEPT"
onMismatch="DENY">
<KeyValuePair key="username" value="admin"
/>
</ThreadContextMapFilter>
... |
Èç¹ûÏëÁ˽â¸ü¶àÐÅÏ¢£¬Äã¿ÉÒԲ鿴Log4jºÍLogbackÎĵµÖйØÓÚDynamicThresholdFilter²¿·Ö¡£
Markers
MarkersÔÊÐíÄã¶Ôµ¥¶ÀµÄÈÕÖ¾¼Ç¼Ìí¼ÓһЩ¶ÀÒ»ÎÞ¶þµÄÊý¾Ý¡£Ëü¿ÉÒÔÓÃÀ´¶ÔÈÕÖ¾¼Ç¼½øÐзÖ×飬´¥·¢Ò»Ð©ÐÐΪ£¬»òÕß¶ÔÈÕÖ¾¼Ç¼½øÐйýÂË£¬²¢½«¹ýÂ˽á¹ûÊä³öµ½Ö¸¶¨µÄAppenderÖС£ÄãÉõÖÁ¿ÉÒÔ½«MarkersºÍThreadContext½áºÏÔÚÒ»ÆðʹÓã¬ÒÔÌá¸ßËÑË÷ºÍ¹ýÂËÈÕÖ¾Êý¾ÝµÄÄÜÁ¦¡£
ÀýÈ磬¼ÙÉèÎÒÃÇÓÐÒ»¸ö¿ÉÒÔÁ¬½Óµ½Êý¾Ý¿âµÄÀ࣬Èç¹ûÔÚ´ò¿ªÊý¾Ý¿âµÄʱºò·¢ÉúÁËÒì³££¬ÎÒÃÇÐèÒª°ÑÒì³£¼Ç¼³Éfatal´íÎó¡£ÎÒÃÇ¿ÉÒÔ´´½¨Ò»¸öÃûΪDB_ERRORµÄMarker£¬È»ºó½«ÆäÓ¦Óõ½ÈÕ־ʼþÖУº
import org.apache.logging.Log4j.Marker;
import org.apache.logging.Log4j.MarkerManager;
...
final static Marker DB_ERROR = MarkerManager.getMarker("DATABASE_ERROR");
...
logger.fatal(DB_ERROR, "An exception occurred."); |
ΪÁËÔÚÈÕÖ¾Êä³öÖÐÏÔʾMarkerÐÅÏ¢£¬ÎÒÃÇÐèÒªÔÚPatternLayoutÖÐÌí¼Ó%markerת»»Ä£Ê½£º
<PatternLayout
pattern="%p %marker: %m%n" />
[FATAL] DATABASE_ERROR: An exception occurred. |
»òÕß¶ÔÓÚJSONºÍXML¸ñʽµÄLayouts£¬»á×Ô¶¯ÔÚÊä³öÖаüº¬MarkerÐÅÏ¢£º
...
"thread" : "main",
"level" : "FATAL",
"loggerName" : "DBClass",
"marker" : { "name" :
"DATABASE_ERROR"
},
"message" : "An exception occurred.",
... |
ͨ¹ý¶ÔMarkerÊý¾Ý½øÐÐ×Ô¶¯½âÎöºÍÅÅÐò£¬¼¯ÖÐʽµÄÈÕÖ¾·þÎñ¿ÉÒÔºÜÈÝÒ×¶ÔÈÕÖ¾½øÐÐËÑË÷´¦Àí¡£
Markers¹ýÂË
Marker¹ýÂËÆ÷¿ÉÒÔÈÃÄã¾ö¶¨ÄÄЩMarkerÓÉÄÄЩLoggerÀ´´¦Àí¡£marker×ֶλá±È½ÏÔÚÈÕ־ʼþÀïÃæµÄMarkerÃû×Ö£¬Èç¹ûÃû×ÖÆ¥Å䣬ÄÇôLogger»áÖ´ÐкóÐøµÄÐÐΪ¡£ÀýÈ磬ÔÚLog4jÖУ¬ÎÒÃÇ¿ÉÒÔÅäÖÃÒ»¸öAppenderÀ´Ö»ÏÔʾÄÄЩʹÓÃÁËDB_ERROR
MarkerµÄÏûÏ¢£¬Õâ¿ÉÒÔͨ¹ýlog4j2.xmlÖеÄAppenderÌí¼ÓÈçÏÂÐÅÏ¢À´ÊµÏÖ£º
<MarkerFilter
marker="DATABASE_ERROR" onMatch="ACCEPT"
onMismatch="DENY" /> |
Èç¹ûÈÕÖ¾¼Ç¼ÖÐijһÌõµÄMarker¿ÉÒÔÆ¥ÅäÕâÀïµÄmarker×ֶΣ¬ÄÇôonMatch»á¾ö¶¨ÈçºÎ´¦ÀíÕâÌõ¼Ç¼¡£Èç¹û²»Äܹ»Æ¥Å䣬»òÕßÈÕÖ¾¼Ç¼ÖÐûÓÐMarkerÐÅÏ¢£¬ÄÇôonMismatch¾Í»á¾ö¶¨ÈçºÎ´¦ÀíÕâÌõ¼Ç¼¡£¶ÔÓÚonMatchºÍonMismatchÀ´Ëµ£¬ÓÐ3¸ö¿ÉÑ¡ÏACCEPT£¬ËüÔÊÐí¼Ç¼Ê¼þ£»DENY£¬Ëü»á×èÈûʼþ£»NEUTRAL£¬Ëü²»»á¶Ôʼþ½øÐÐÈκδ¦Àí¡£
ÔÚLogbackÖУ¬ÎÒÃÇÐèÒª¸ü¶àһЩÉèÖá£Ê×ÏÈ£¬ÏëAppenderÖÐÌí¼ÓÒ»¸öеÄEvaluatorFilter£¬²¢ÈçÉÏËùÊöÖ¸¶¨onMatchºÍonMismatchÐÐΪ¡£È»ºó£¬Ìí¼ÓÒ»¸öOnMarkerEvaluator²¢½«MarkerµÄÃû×Ö´«µÝ¸øËü£º
<filter
class="ch.qos.Logback.core.filter.EvaluatorFilter">
<evaluator class="ch.qos.Logback.classic.boolex.OnMarkerEvaluator">
<marker>DATABASE_ERROR</marker>
</evaluator> <onMatch>ACCEPT</onMatch>
<onMismatch>DENY</onMismatch>
</filter> |
½«MarkersºÍNDC¡¢MDCÒÔ¼°ThreadContext½áºÏʹÓÃ
MarkerµÄ¹¦ÄܺÍThreadContextÀàËÆ£¬ËüÃǶ¼ÊÇÏòÈÕÖ¾¼Ç¼ÖÐÌí¼Ó¶ÀÒ»ÎÞ¶þµÄÊý¾Ý£¬ÕâЩÊý¾Ý¿ÉÒÔ±»Appender·ÃÎÊ¡£Èç¹û°ÑÕâÁ½Õß½áºÏʹÓ㬿ÉÒÔÈÃÄã¸üÈÝÒ׵ĶÔÈÕÖ¾Êý¾Ý½øÐÐË÷ÒýºÍËÑË÷¡£Èç¹ûÄܹ»ÖªµÀºÎʱʹÓÃÄÄÒ»ÖÖ¼¼Êõ£¬»á¶ÔÎÒÃÇÓÐËù°ïÖú¡£
NDC¡¢MDCºÍThreadContext±»ÓÃÓÚ½«Ïà¹ØÈÕÖ¾¼Ç¼½áºÏÔÚÒ»Æð¡£Èç¹ûÄãµÄÓ¦ÓóÌÐò»á´¦Àí¶à¸öͬʱ´æÔÚµÄÓû§£¬ThreadContext¿ÉÒÔÈÃÄ㽫Õë¶Ôij¸öÌØ¶¨Óû§µÄÒ»×éÈÕÖ¾¼Ç¼×éºÏÔÚÒ»Æð¡£ÒòΪThreadContextÕë¶Ôÿ¸öÏ̶߳¼ÊDz»Ò»ÑùµÄ£¬ËùÒÔÄã¿ÉÒÔʹÓÃͬÑùµÄ·½·¨À´¶ÔÏà¹ØµÄÈÕÖ¾¼Ç¼½øÐÐ×Ô¶¯·Ö×é¡£
ÁíÒ»·½Ã棬Markerͨ³£ÓÃÓÚ±ê¼Ç»òÕ߸ßÁÁÏÔÊ¾Ä³Ð©ÌØÊâʼþ¡£ÔÚÉÏÊöʾÀýÖУ¬ÎÒÃÇʹÓÃDB_ERROR
MarkerÀ´±êÃ÷ÔÚ·½·¨Öз¢ÉúµÄSQLÏà¹ØÒì³£¡£ÎÒÃÇ¿ÉÒÔʹÓÃDB_ERROR MarkerÀ´½«ÕâЩʼþµÄ´¦Àí¹ý³ÌºÍÆäËûʼþÇø·Ö¿ªÀ´£¬ÀýÈçÎÒÃÇ¿ÉÒÔʹÓÃSMTP
AppenderÀ´½«ÕâЩʼþͨ¹ýÓʼþ·¢Ë͸øÊý¾Ý¿â¹ÜÀíÔ±¡£
¶îÍâ×ÊÔ´
Ö¸ÄϺͽ̳Ì
Java Logging£¨Jakob Jenkov£©¡ª¡ªÊ¹ÓÃJava Logging API½øÐÐÈÕÖ¾¿ª·¢½Ì³Ì
Java Logging Overview£¨Oracle£©¡ª¡ª OracleÌṩµÄÔÚJavaÖнøÐÐÈÕÖ¾¿ª·¢µÄÖ¸ÄÏ
Log4J Tutorial£¨Tutorials Point£©¡ª¡ªÊ¹ÓÃlog4j °æ±¾1½øÐÐÈÕÖ¾¿ª·¢µÄÖ¸ÄÏ
ÈÕÖ¾³éÏó²ã
Apache Commons Logging£¨Apache£©¡ª¡ªÕë¶ÔLog4j¡¢Avalon LogKitºÍjava.util.loggingµÄ³éÏó²ã
SLF4J£¨QOS.ch£©¡ª¡ªÒ»¸öÁ÷³ÌµÄ³éÏó²ã£¬Ó¦ÓÃÔÚ¶à¸öÈÕÖ¾¿ò¼ÜÉÏ£¬°üÀ¨Log4j¡¢LogbackÒÔ¼°java.util.logging
ÈÕÖ¾¿ò¼Ü
Java Logging API£¨Oracle£©¡ª¡ª JavaĬÈϵÄÈÕÖ¾¿ò¼Ü
Log4j£¨Apache£©¡ª¡ª¿ªÔ´ÈÕÖ¾¿ò¼Ü
Logback£¨Logback Project£©¡ª¡ª¿ªÔ´ÏîÄ¿£¬±»Éè¼Æ³ÉLog4j°æ±¾1µÄºóÐø°æ±¾
tinylog£¨tinylog£©¡ª¡ªÇáÁ¿¼¶¿ªÔ´logger
|