1.Log4j Appenderä¹RollingFileAppender
Log4j Appenderä¹RollingFileAppender
æ¬æç¿»è¯èªlog4jå®æ¹ææ¡£ï¼æäºç»èå±äºä¸ªäººç解ï¼ï¼ https://logging.apache.org/log4j/2.x/manual/appenders.html#RollingFileAppender
RollingFileAppenderæ¯ä¸ç§OutputStreamAppenderï¼è¯¥appenderä¼å°æ¥å¿åå ¥å称为é 置项fileNameçæ件ä¸ï¼å¹¶ä¸è¿ä¼æ ¹æ®TriggeringPolicyåRolloverPolicy对æ¥å¿æ件è¿è¡åæ»æä½ï¼å®ä¾èµRollingFileManagerï¼ç»§æ¿èªOutputStreamManagerï¼æ¥å®é æ§è¡æ件读åï¼I/Oï¼ååæ»ã尽管éè¿ä¸åé ç½®åå§åçRollingFileAppenderä¸è½å ±äº«ï¼ä½æ¯RollingFileManagerså´æ¯å¯ä»¥å ±äº«çãæ¯å¦ï¼å¨åä¸ä¸ªservlet容å¨ä¸ç两个webåºç¨ï¼è½ç¶åèªæä»ä»¬èªå·±çé ç½®ï¼ä½æ¯å¦ælog4jæ¯éè¿å ¬ç¨çClassLoader å è½½çè¯ï¼å°±å¯ä»¥åæ¶åå ¥å°åä¸ä¸ªæ¥å¿æ件ã
RollingFileAppenderä¾èµäºä¸ä¸ª TriggeringPolicy åä¸ä¸ª RolloverStrategy ï¼triggering policyå³å®äºæ¯å¦æ§è¡åæ»ï¼èRolloverStrategyå³å®äºå¦ä½è¿è¡åæ»ãå¦æ没æé ç½®RolloverStrategyï¼å°±ä¼ä½¿ç¨ DefaultRolloverStrategy ãä»log4j 2.5çæ¬å¼å§ï¼å¯ä»¥å¨DefaultRolloverStrategyä¸é ç½®ä¸ä¸ª custom delete action ï¼ä»¥ä¾¿å¨åæ»æ¶å®æèªå®ä¹çå é¤æä½ãä»log4j 2.8çæ¬å¼å§ï¼å¦æ没æé ç½®file nameï¼åä¼é»è®¤ä½¿ç¨ DirectWriteRolloverStrategy ï¼èä¸æ¯DefaultRolloverStrategyãä»log4j 2.9çæ¬å¼å§ï¼å¯ä»¥å¨DefaultRolloverStrategyä¸é ç½®ä¸ä¸ª custom POSIX file attribute view action ï¼å¦æ没æå®ä¹ï¼åä¼ä½¿ç¨ç»§æ¿èªRollingFileAppenderçPOSIX fileã
RollingFileAppenderä¸æ¯ææ¥å¿æ件éå®ã
CompositeTriggeringPolicyç¨äºç»åå¤ä¸ªtriggering policiesï¼å¦ææä¸ä¸ªè¿åtrueï¼åCompositeTriggeringPolicyå°±è¿åtrueã
å¦ä¸çxmlç段ï¼ç»åäºä¸ä¸ªtriggering policiesï¼1.å½JVMå¯å¨æ¶ï¼2.å½æ¥å¿æ件达å°MBæ¶ï¼3.å½åæ¥æä¸æ¥å¿æ件çèµ·å§æ¥æä¸å¹é æ¶ï¼æ天åæ件ï¼ï¼
åºäºcron表达å¼è¿è¡åæ»ãè¿ä¸ªçç¥éè¿ä¸ä¸ªtimeræ¥æ§å¶ï¼å¹¶ä¸æ¯å¼æ¥å»å¤çlog eventçï¼å æ¤å¯è½ä¼åºç°æ¥èªä¸ä¸ä¸ªæä¸ä¸ä¸ªæ¶é´æ®µçæ¥å¿åºç°å¨ä¸ä¸ä¸ªæ¥å¿æ件çå¼å¤´æç»å°¾ãé 置项filePatternä¸éè¦å å«ä¸ä¸ªtimestampï¼å¦åå¨æ¯æ¬¡åæ»çæ¶åï¼ç®æ æ件é½ä¼è¢«è¦çã
å¦ææ件çå建æ¶é´å°äºJVMå¯å¨çæ¶é´ï¼å¹¶ä¸æ¥å¿æ件ç大å°å¤§çº¦çäºäºæå°æ件大å°ï¼åä¼å¨JVMå¯å¨æ¶è§¦ååæ»ã
注æï¼Google App Engine
å½åºç¨è¿è¡äºGoogle App Engineä¸æ¶,源码党校管理源码 该çç¥åªè½ä½¿ç¨Log4Jçåå§æ¶é´æ¥åå¤æä¾æ®ã (Google App Engineéå¶äºè®¿é®å ·ä½classï¼æ以Log4Jä¸è½éè¿æ¹æ³java.lang.management.ManagementFactory.getRuntimeMXBean().getStartTime() è·åå°JVMçå¯å¨æ¶é´ï¼åªè½é级为使ç¨Log4Jçåå§æ¶é´ã)
å½æ件大å°è¾¾å°æå®å¼æ¶ï¼SizeBasedTriggeringPolicyä¼è§¦ååæ»ã大å°å¯ä»¥æå®ä¸åçåä½ï¼å æ¬ï¼é»è®¤çbytesãå å«åç¼ç KB, MB or GBï¼æ¯å¦ï¼è¡¨ç¤ºä¸ªåèï¼MB表示å æ¯ãå½ä¸æ¶é´ç±»åççç¥ç»å使ç¨æ¶ï¼filePatternåæ°å¿ é¡»å å«%iï¼ä¸ç¶æ件å°ä¼è¢«è¦çï¼å 为SizeBased Triggering Policyä¸ä¼åæ´æ件åä¸çæ¶é´åæ°ãå½ä¸ä¸æ¶é´ç±»åçåæ»çç¥ç»å使ç¨æ¶ï¼SizeBased Triggering Policyä¼åæ´æ件åä¸çæ¶é´åæ°ã
å½å½åæ¶é´(date/time)è§åä¸åä¸å½åæ件åä¸çdate/timeä¸è´æ¶ï¼TimeBasedTriggeringPolicyä¼è§¦ååæ»ã该çç¥æ¥åä¸ä¸ªintervalå±æ§ï¼ç¨äºæå®åºäºtimeåmodulateçåæ»é¢çã
é»è®¤çåæ»çç¥æ¯æRollingFileAppenderçå±æ§filePatternä¸é ç½®çdate/timeè§å以åä¸ä¸ªæ°å计æ°å¨ãfilePatternä¸çdate/timeè§åä¼è¢«è®¾ç½®ä¸ºå½åæ¶é´ï¼è计æ°å¨ä¼å¨æ¯æ¬¡åæ»æ¶éå¢ãå¦æåæ¶å å«date/timeå计æ°å¨ï¼å计æ°å¨ä¼å¨date/timeè§åçå¼åçåæ´æ¶éæ°å¼å§è®¡æ°ãå¦æfilePattern以".gz", ".zip", ".bz2", ".deflate", ".pack", or ".xz" ç»å°¾ï¼åæ»åçæ件就ä¼ä»¥å¯¹åºçæ ¼å¼è¿è¡å缩ã使ç¨bzip2, Deflate, Pack and XZéè¦å¼å ¥ Apache Commons Compress åºï¼å¹¶ä¸XZè¿éè¦å¤å¼å ¥ XZ for Java ãThe pattern may also contain lookup references that can be resolved at runtime such as is shown in the example below.
é»è®¤çåæ»çç¥æ¯æä¸ç§å½¢å¼ç计æ°å¨ï¼ä¸ºäºè¡¨è¿°æ¸ æ¥å®æ¯æä¹å·¥ä½çï¼æ们å设计æ°å¨æå°å¼ä¸º1ï¼æ大å¼ä¸º3ï¼æ件å称为foo.logï¼filePattern为foo-%i.logã
为äºå¯¹ç §é 置项fileIndex设置为"min"æ¶çææï¼åºäºä¸è¡¨çå设ï¼ä¼åºç°å¦ä¸ç°è±¡ã
æåï¼å¨2.8çæ¬ä¸ï¼å¦æfileIndex 设置为äº"nomax"ï¼è¿æ¶å°±ä¼å¿½ç¥é 置项maxåminãæ以æ¯ä¸æ¬¡åæ»æ¶é½ä¼éå¢è®¡æ°å¨ï¼ä¸åææ大åæ»æ件æ°éçéå¶ã
DirectWriteRolloverStrategyä¼å°æ¥å¿ç´æ¥è¾åºå°filePatternæå®çæ件ï¼è¿ä¸ªè§åå¨åæ»æ¶ä¸ä¼éå½åæ件ãå¦æå¨æ¶é´åæ»çç¥æªçæçè¿ç¨ä¸ï¼åºäºå¤§å°ççç¥äº§çäºå¤æ¬¡åæ»ï¼åæ»æ件åä¼èªå¨æ·»å 计æ°å¨ï¼ä½æ件ä¸çæ¶é´ä¸åï¼ï¼å¹¶ä¸æå¢é¿ï¼ç´å°åºäºæ¶é´çåæ»çç¥çæï¼åä¼éæ°å¼å§æ°ä¸è½®ç计æ°å¨å¢é¿ã
注æï¼å¦æfilePatternä¸æå®éè¦å缩çè§åï¼æ¯å¦zipåç¼åï¼ï¼å½ååå ¥æ件ä¸ä¼è¢«å缩ï¼é¤éç¨åºå ³éãå¦å¤ï¼å¦ææç §æ¶é´è§åè¿è¡äºåæ»ï¼æ°çåå ¥æ件åæ ·ä¸ä¼è¢«å缩ã
ä¸æ¹æ¯ä¸ä¸ªå³ä½¿ç¨æ¶é´è§¦åçç¥å使ç¨å¤§å°è§¦åçç¥çäºä¾ï¼å®æ¯å¤©å°å建æå¤7个åæ»æ件ï¼åæ»æ件å°ä¼è¢«ä¿åå¨å称为年åæçç®å½ä¸ï¼æ¯ä¸ªåæ»æ件å°ä¼ä½¿ç¨gzipæ ¼å¼è¿è¡å缩ï¼
第äºä¸ªäºä¾ï¼æå®æ大çåæ»æ件æ°éï¼å½å¤§äºä¸ªåæ»æ件æ¶ï¼å°±ä¼è¢«ç§»é¤ï¼
ä¸é¢çäºä¾ï¼åæ ·æ¯ä¸ä¸ªå³ä½¿ç¨æ¶é´è§¦åçç¥å使ç¨å¤§å°è§¦åçç¥çäºä¾ãåæ ·ï¼æ¯å¤©æå¤åªä¼ä¿ç7个åæ»æ件ï¼åæ»æ件æ¾å¨ä»¥å½åå¹´åæ为å称çç®å½ä¸ï¼ä¹å°ä¼ä½¿ç¨gzipè¿è¡å缩ãç¹æ®çæ¯ï¼æ¬æ¬¡æ们就è¦æ±æ件6å°æ¶åæ»ä¸æ¬¡ï¼å¹¶ä¸åæ»éè¦åçå¨è½è¢«6æ´é¤çå°æ¶ã
ä¸æ¹çäºä¾ä½¿ç¨cronå大å°è§¦åçç¥ï¼åæ»æ件没ææ°ééå¶ãcronçç¥ä¼å¨æ¯ä¸ªå°æ¶0å触ååæ»ï¼åæ¶å¤§å°åæ»çç¥ä¼å¨æ件çäºMBæ¶è§¦ååæ»ï¼
ä¸æ¹çäºä¾ä¸ä¸ä¸ä¸ªç±»ä¼¼ï¼åªæ¯éå¶äºæ¯å°æ¶åæ»æ件çæ°é为个ï¼
Log4j-2.5为äºå¼¥è¡¥DefaultRolloverStrategyåªè½æ ¹æ®maxæ¥å é¤æ件ççç¥ï¼èå¼å ¥äºDelete actionï¼ç¨æ·å°±å¯ä»¥ææ´å¤çå¨åæ»æ¶å é¤æ件çæ段ãDelete action让ç¨æ·å¯ä»¥é ç½®ä¸ä¸ªæè å¤ä¸ªæ¡ä»¶æ¥éæ©éè¦å é¤çæ件ã
éè¦æ³¨æçæ¯ï¼Delete actionå¯è½ä¼å é¤ç¬¦åæ¡ä»¶çä»»ä½æ件ï¼ä¸ä» ä» æ¯åæ»æ件ï¼æ以使ç¨è¿ä¸ªç¹æ§æ¶éè¦å½å¿ãä½ å¯ä»¥ä½¿ç¨testModeåæ°æ¥æµè¯ä½ çé ç½®ï¼ä»¥é²æå¤çå éäºæ件ã
ä¸æ¹çäºä¾ï¼ä½¿ç¨ä¸ä¸ªcron触åçç¥ï¼æ¯å¤©åå¤è§¦ååæ»ãåæ»æ件åæ¾å¨å½åå¹´åæçç®å½ä¸ãææå¨base pathç®å½ä¸ç满足表达å¼" /app- .log.gz"çæ件ï¼ä¸æåä¿®æ¹æ¶é´å¤§äºçäºå¤©çæ件å°ä¼å¨åæ»æ¶è¢«å é¤ã
ä¸æ¹çäºä¾ï¼ä½¿ç¨æ¶é´å大å°ç触åçç¥ã该çç¥ä¼å¨ä»¥å½åå¹´åæçç®å½ä¸ä¿åä¸å¤©æå¤ä¸ªåæ»æ件ï¼æ¯ä¸ªåæ»æ件å°ä½¿ç¨gzipè¿è¡å缩ï¼æ¯ä¸ªå°æ¶ä¼åæ»ä¸æ¬¡ãå¨æ¯æ¬¡åæ»æ¶ï¼å é¤æ件çå¤ææ¡ä»¶ä¸ºï¼æ件å满足表达å¼" /app- .log.gz"ï¼å¹¶ä¸æåä¿®æ¹æ¶é´å¤§äºçäºå¤©ï¼ä½æ¯ä¸ä¼å é¤æè¿GBæ件(æ»å¤§å°å¤§äºGB)æè æè¿ç个ï¼æ件æ°å¤§äºï¼æ件ã
ä¸æ¹äºä¾ï¼ä½¿ç¨cron触åçç¥ï¼æ¯å¤©çåå¤å°ä¼è§¦ååæ»ãåæ»æ件åæ¾å¨ä»¥å½åå¹´åæ为å称çç®å½ä¸ã该èæ¬è¿åbase pathç®å½ä¸çæ¥æ为æ¥(ææäº)çåæ»æ件å表ãDelete actionå°ä¼å é¤è¯¥èæ¬è¿åçæææ件ã
Log4j-2.9å¼å ¥äºPosixViewAttribute actionï¼è¯¥actionç»äºç¨æ·æ´å¤æ§å¶åæ»æ件ç对äºownerågroupçæéã该actionåè²ç¨æ·é ç½®ä¸ä¸ªæè å¤ä¸ªæ¡ä»¶æ¥éæ©ç¬¦åæ¡ä»¶çbase pathä¸çæ件ã
ä¸é¢æ¯ä¸ä¸ªä½¿ç¨RollingFileAppenderç示ä¾é ç½®ï¼å®ä¸ºå½ååå·²æ»æ¥å¿æ件å®ä¹äºä¸åçPOSIXæ件å±æ§è§å¾ã