- 浏览: 563452 次
- 性别:
- 来自: 长沙
文章分类
- 全部博客 (145)
- apache-struts (3)
- apache-shiro (4)
- apache-wicket (1)
- spring (34)
- spring-data-jpa (2)
- spring-mvc (20)
- spring-security (1)
- spring-webflow (1)
- hibernate (2)
- mongodb (1)
- ibatis (5)
- mysql (4)
- 开源组件 (18)
- java (3)
- maven (7)
- jBPM (1)
- EJB (1)
- JMS (2)
- servlet / jsp (9)
- javascript / jquery (10)
- 工作技巧 (12)
- ubuntu (6)
- bootstrap (10)
- javaee (1)
- 他山石 (7)
- jetbrick (1)
最新评论
-
yubao2008:
[size=x-small]为什么我也这样试了, 就是没有生效 ...
javax.servlet.http.HttpServletResponse 得到 status_code -
chenrl:
...
SpringMVC杂记(十五) spring-mvc controller 的切面 -
LONGTENGLONG:
你好,这样配置的,得到的集合为空,是什么原因?
apache-shiro杂记(一) 统计在线用户数目 -
xiafengfeiwu:
[flash=200,200][url]引用[/url][/f ...
apache-shiro 学习笔记 -
3108493554:
你好 ,有些问题想请教下,加下我qq310849354,你这上 ...
SpringMVC杂记(十二) 自定义Interceptor从Active Directory得到域信息
package org.apache.log4j; import java.io.File; import java.io.FilenameFilter; import java.io.IOException; import java.io.Writer; import java.text.SimpleDateFormat; import java.util.ArrayList; import java.util.Date; import java.util.List; import java.util.Locale; import java.util.TimeZone; import org.apache.log4j.helpers.CountingQuietWriter; import org.apache.log4j.helpers.LogLog; import org.apache.log4j.helpers.OptionConverter; import org.apache.log4j.spi.LoggingEvent; /** * 整合<code>RollingFileAppender</code>与<code>DailyRollingFileAppender</code> * * <p> * 可追加属性如下 * </p> * * <table border="1"> * <tr> * <th><b>属性名称</b></th> * <th><b>说明</b></th> * <th><b>默认值</b></th> * </tr> * <tr> * <td nowrap="true"><b>maxFileSize</b></td> * <td>日志文件大小</td> * <td nowrap="true">java.lang.Long#MAX_VALUE</td> * </tr> * <tr> * <td nowrap="true"><b>maxBackupIndex</b></td> * <td>每日产生日志文件的数量(个数)</td> * <td nowrap="true">java.lang.Integer#MAX_VALUE</td> * </tr> * <tr> * <td nowrap="true"><b>datePattern</b></td> * <td>日期格式</td> * <td nowrap="true">'.'yyyy-MM-dd</td> * </tr> * </table> * * <p> * <b>maxBackupIndex</b> 设置为一个过小的数值时,有可能最老的日志文件被删除。 <br/> * <b>maxFileSize</b> 设置为0时,日志是不会输出到文件中的。这一点需要特别注意! * </p> * * </ul> * * <h4>配置列子(log4j.xml)</h4> * * <pre> * <?xml version="1.0" encoding="UTF-8" ?> * <!DOCTYPE log4j:configuration SYSTEM "log4j.dtd"> * * <log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/"> * * <appender name="FILE" class="org.apache.log4j.CompositeRollingFileAppender"> * <param name="File" value="log/sample.log" /> * <param name="maxFileSize" value="15MB" /> * <param name="maxBackupIndex" value="12" /> * * <layout class="org.apache.log4j.PatternLayout"> * <param name="ConversionPattern" value="%d{ABSOLUTE} %5p %c:%L - %m%n"/> * </layout> * </appender> * * </log4j:configuration> * </pre> * * <h4>配置列子(log4j.properties)</h4> * * <pre> * log4j.rootCategory=DEBUG, file * * log4j.appender.file=org.apache.log4j.CompositeRollingFileAppender * log4j.appender.file.File=${webapp.root}/WEB-INF/log/sample.log * log4j.appender.file.maxBackupIndex=12 * log4j.appender.file.maxFileSize=15MB * log4j.appender.file.layout=org.apache.log4j.PatternLayout * log4j.appender.file.layout.ConversionPattern=%d{ABSOLUTE} %5p %c:%L - %m%n * </pre> */ public class CompositeRollingFileAppender extends FileAppender { private static final int TOP_OF_TROUBLE = -1; private static final int TOP_OF_MINUTE = 0; private static final int TOP_OF_HOUR = 1; private static final int HALF_DAY = 2; private static final int TOP_OF_DAY = 3; private static final int TOP_OF_WEEK = 4; private static final int TOP_OF_MONTH = 5; private static final TimeZone gmtTimeZone = TimeZone.getTimeZone("GMT"); private static final int BY_SIZE = 0; private static final int BY_DATE = 1; protected long maxFileSize = Long.MAX_VALUE; protected int maxBackupIndex = Integer.MAX_VALUE; protected String datePattern = "'.'yyyy-MM-dd"; protected String scheduledFilename; protected long nextCheck = System.currentTimeMillis() - 1; private Date now = new Date(); private SimpleDateFormat sdf; private RollingCalendar rc = new RollingCalendar(); /*----------------------------------------------------------------------------*/ public CompositeRollingFileAppender() { super(); } public CompositeRollingFileAppender(Layout layout, String filename, boolean append) throws IOException { super(layout, filename, append); } public CompositeRollingFileAppender(Layout layout, String filename) throws IOException { super(layout, filename); } public CompositeRollingFileAppender(Layout layout, String filename, String datePattern) throws IOException { super(layout, filename, true); this.datePattern = datePattern; activateOptions(); } public CompositeRollingFileAppender(Layout layout, String filename, String datePattern, boolean append) throws IOException { super(layout, filename, append); this.datePattern = datePattern; activateOptions(); } /*----------------------------------------------------------------------------*/ public synchronized void setFile(String fileName, boolean append, boolean bufferedIO, int bufferSize) throws IOException { super.setFile(fileName, append, this.bufferedIO, this.bufferSize); if (append) { File f = new File(fileName); ((CountingQuietWriter) qw).setCount(f.length()); } } public void activateOptions() { super.activateOptions(); if (datePattern != null && fileName != null) { now.setTime(System.currentTimeMillis()); sdf = new SimpleDateFormat(datePattern); int type = computeCheckPeriod(); printPeriodicity(type); rc.setType(type); File file = new File(fileName); scheduledFilename = fileName + sdf.format(new Date(file.lastModified())); } else { LogLog.error("Either File or DatePattern options are not set for appender [" + name + "]."); } } void printPeriodicity(int type) { switch (type) { case TOP_OF_MINUTE: LogLog.debug("Appender [" + name + "] to be rolled every minute."); break; case TOP_OF_HOUR: LogLog.debug("Appender [" + name + "] to be rolled on top of every hour."); break; case HALF_DAY: LogLog.debug("Appender [" + name + "] to be rolled at midday and midnight."); break; case TOP_OF_DAY: LogLog.debug("Appender [" + name + "] to be rolled at midnight."); break; case TOP_OF_WEEK: LogLog.debug("Appender [" + name + "] to be rolled at start of week."); break; case TOP_OF_MONTH: LogLog.debug("Appender [" + name + "] to be rolled at start of every month."); break; default: LogLog.warn("Unknown periodicity for appender [" + name + "]."); } } int computeCheckPeriod() { RollingCalendar rollingCalendar = new RollingCalendar(gmtTimeZone, Locale.ENGLISH); // set sate to 1970-01-01 00:00:00 GMT Date epoch = new Date(0); if (datePattern != null) { for (int i = TOP_OF_MINUTE; i <= TOP_OF_MONTH; i++) { SimpleDateFormat simpleDateFormat = new SimpleDateFormat( datePattern); simpleDateFormat.setTimeZone(gmtTimeZone); // do all date // formatting in GMT String r0 = simpleDateFormat.format(epoch); rollingCalendar.setType(i); Date next = new Date(rollingCalendar.getNextCheckMillis(epoch)); String r1 = simpleDateFormat.format(next); // System.out.println("Type = "+i+", r0 = "+r0+", r1 = "+r1); if (r0 != null && r1 != null && !r0.equals(r1)) { return i; } } } return TOP_OF_TROUBLE; // Deliberately head for trouble... } public void rollOverSize() { LogLog.debug("rolling over count=" + ((CountingQuietWriter) qw).getCount()); LogLog.debug("maxBackupIndex=" + maxBackupIndex); if (maxBackupIndex > 0) { rotateLogFilesBy(BY_SIZE); } try { this.setFile(fileName, false, bufferedIO, bufferSize); } catch (IOException e) { LogLog.error("setFile(" + fileName + ", false) call failed.", e); } } public void rollOverTime() throws IOException { if (datePattern == null) { errorHandler.error("Missing DatePattern option in rollOver()."); return; } String datedFilename = fileName + sdf.format(now); if (scheduledFilename.equals(datedFilename)) { return; } rotateLogFilesBy(BY_DATE); try { this.setFile(fileName, false, this.bufferedIO, this.bufferSize); } catch (IOException e) { errorHandler.error("setFile(" + fileName + ", false) call failed."); } scheduledFilename = datedFilename; } protected void subAppend(LoggingEvent event) { long n = System.currentTimeMillis(); if (n >= nextCheck) { now.setTime(n); nextCheck = rc.getNextCheckMillis(now); try { rollOverTime(); } catch (IOException ioe) { LogLog.error("rollOver() failed.", ioe); } } if ((fileName != null) && ((CountingQuietWriter) qw).getCount() >= maxFileSize) { rollOverSize(); } super.subAppend(event); } private int getBackupLogFileNum() { File[] logs = getAllLogFiles(); return logs.length - 1; } private File[] getAllLogFiles() { File outputPath = new File(fileName); String name = outputPath.getName(); File logDir = outputPath.getParentFile(); if (logDir == null) { logDir = new File("."); } return logDir.listFiles(new LogFileNameFilter(name)); } private void deleteOldestFile() { File[] logs = getAllLogFiles(); File oldest = null; long maxLastModified = 0; for (int i = 0; i < logs.length; i++) { long lastModified = logs[i].lastModified(); if (oldest == null) { oldest = logs[i]; maxLastModified = lastModified; } else { if (maxLastModified > lastModified) { oldest = logs[i]; maxLastModified = lastModified; } } } if (oldest != null && oldest.exists()) { oldest.delete(); } } private void rotateLogFilesBy(int mode) { List<File> notDailyLogs = new ArrayList<File>(); File[] allLogs = getAllLogFiles(); for (int i = 0; i < allLogs.length; i++) { if (!isDailyRotatedLog(allLogs[i])) { notDailyLogs.add(allLogs[i]); } } int notDailyLogNum = notDailyLogs.size(); if (mode == BY_SIZE) { File file = null; File target = null; if (getBackupLogFileNum() >= maxBackupIndex) { deleteOldestFile(); } for (int i = notDailyLogNum - 1; i >= 1; i--) { file = new File(fileName + "." + i); if (file.exists()) { target = new File(fileName + '.' + (i + 1)); LogLog.debug("Renaming file " + file + " to " + target); file.renameTo(target); } } target = new File(fileName + "." + 1); this.closeFile(); file = new File(fileName); LogLog.debug("Renaming file " + file + " to " + target); file.renameTo(target); } else if (mode == BY_DATE) { this.closeFile(); if (getBackupLogFileNum() >= maxBackupIndex) { deleteOldestFile(); } for (int i = 1; i <= notDailyLogNum - 1; i++) { String from = fileName + '.' + i; String to = scheduledFilename + '.' + i; renameFile(from, to); } renameFile(fileName, scheduledFilename); } else { LogLog.warn("invalid mode:" + mode); } } private boolean isDailyRotatedLog(File deletedFile) { String deletedName = deletedFile.getName(); File outputPath = new File(fileName); String name = outputPath.getName(); if (deletedName.equals(name)) { return false; } else { String ext = deletedName.substring(name.length() + 1); try { Integer.parseInt(ext); } catch (NumberFormatException ex) { return true; } return false; } } private void renameFile(String from, String to) { File toFile = new File(to); if (toFile.exists()) { toFile.delete(); } File fromFile = new File(from); fromFile.renameTo(toFile); } /*----------------------------------------------------------------------------*/ public int getMaxBackupIndex() { return maxBackupIndex; } public long getMaximumFileSize() { return maxFileSize; } public void setMaxBackupIndex(int maxBackups) { this.maxBackupIndex = maxBackups; } public void setMaximumFileSize(long maxFileSize) { this.maxFileSize = maxFileSize; } public void setMaxFileSize(String value) { maxFileSize = OptionConverter.toFileSize(value, maxFileSize + 1); } protected void setQWForFiles(Writer writer) { this.qw = new CountingQuietWriter(writer, errorHandler); } public void setDatePattern(String pattern) { datePattern = pattern; } public String getDatePattern() { return datePattern; } /*----------------------------------------------------------------------------*/ static class LogFileNameFilter implements FilenameFilter { String fileName; LogFileNameFilter(String name) { this.fileName = name; } public boolean accept(File dir, String name) { return name.startsWith(fileName); } } }
配置列子:
#------------------------------------------------------------------------------------ # Root-Categroy #------------------------------------------------------------------------------------ log4j.rootCategory=DEBUG, stdout, file #------------------------------------------------------------------------------------ # stdout #------------------------------------------------------------------------------------ log4j.appender.stdout=org.apache.log4j.ConsoleAppender log4j.appender.stdout.layout=org.apache.log4j.PatternLayout log4j.appender.stdout.layout.ConversionPattern=%d{ABSOLUTE} %5p %c:%L - %m%n #------------------------------------------------------------------------------------ # file (log) #------------------------------------------------------------------------------------ log4j.appender.file=org.apache.log4j.CompositeRollingFileAppender log4j.appender.file.File=D:/log4j-test/sample.log log4j.appender.file.maxBackupIndex=12 log4j.appender.file.maxFileSize=15MB log4j.appender.file.layout=org.apache.log4j.PatternLayout log4j.appender.file.layout.ConversionPattern=%d{ABSOLUTE} %5p %c:%L - %m%n
发表评论
-
扩展logback DBAppender
2013-07-30 15:07 10355一) logback已经提供了一个DBAppender(ch. ... -
Java生成缩略图之Thumbnailator(转载)
2013-07-15 21:19 2765Java生成缩略图之Thumbnailator(转载) 原文 ... -
pinyin4j 的应用
2013-05-12 08:59 1283<!-- pinyin4j --> < ... -
二维条形码的生成与解析
2013-05-02 11:58 2651二维条形码的生成与解 ... -
memcached学习笔记(二) 使用memcached java客户端
2012-07-11 11:51 14441) 了解了一下 memcached 客户端常见的有两种 sp ... -
memcached学习笔记(一) instanll memcached on windows machine
2012-07-11 10:44 16141) 下载 http://code.jellycan.com/ ... -
Zip 压缩和解压缩
2011-12-31 14:17 1697要依赖 ant.1.7.0.jar <depend ... -
BoneCP
2011-11-07 10:39 27361) BoneCP加入工程 <dependency& ... -
Quartz 任务调度器
2011-05-04 15:33 1854字段允许值允许的特殊 ... -
commons-email笔记
2011-04-15 11:59 1674使用Apache Commons-email发送电子邮件 所 ... -
commons-digest笔记
2011-04-12 11:09 1435本文为转载且为截取的片段,如有需要请访问原帖 待解析的xml ... -
json-taglib
2011-03-29 14:53 1794JSON-taglib是一套JSP标签库用于在JSP代码中输出 ... -
JSR-303
2011-03-21 15:45 12063JSR-303 是JAVA EE 6 中的一项子规范,叫做Be ... -
JUnit如何使用
2011-02-17 13:34 4811一,Junit4是Junit框架有 ... -
Apache-dom4j的使用(二)
2011-02-11 15:57 2055本小节讲如何使用dom4j解析一个xml文件 以读取book ... -
Apache-dom4j的使用(一)
2011-02-11 14:15 1363本节主要介绍如何用dom4j生成一个xml文档 <? ... -
log4j的xml配置方式
2011-01-15 07:33 1621一,原来一贯用的properties配置文件 log4j.pr ...
相关推荐
log4j.appender.A1=org.apache.log4j.DailyRollingFileAppender log4j.appender.A1.File=c\:\\SampleMessages.html log4j.appender.A1.DatePattern=yyyyMMdd-HH log4j.appender.A1.layout=org.apache.log4j....
log4j.appender.file = org.apache.log4j.RollingFileAppender log4j.appender.file.File=log/tibet.log log4j.appender.file.MaxFileSize=10mb log4j.appender.file.Threshold=ERROR log4j.appender.file.layout=...
log4j.appender.A1=org.apache.log4j.DailyRollingFileAppender log4j.appender.A1.File=SampleMessages.log4j log4j.appender.A1.DatePattern=yyyyMMdd-HH'.log4j' log4j.appender.A1.layout=org.apache.log4j.xml....
本文将深入解析log4j的配置与加载机制,帮助开发者更好地理解和应用log4j。 #### Log4j配置文件格式 Log4j支持多种配置文件格式,包括`.properties`和`.xml`。两种格式在功能上并无本质区别,选择哪种取决于个人或...
Log4j 中将要输出的 Log 信息定义了 5 种级别,依次为 DEBUG、INFO、WARN、ERROR 和 FATAL。当输出时,只有级别高过配置中规定的级别的信息才能真正的输出。 Log4j 的 Appender Log4j 提供了多种 Appender,包括:...
这些级别可以自定义,但 Log4j 建议只使用四个级别:ERROR、WARN、INFO 和 DEBUG。 四、Appenders Appenders 是 Log4j 的输出目的地,用于将日志信息输出到不同的目的地。Log4j 提供了多种 Appenders,包括: 1. ...
org.apache.log4j.DailyRollingFileAppender(每天产生一个日志文件) org.apache.log4j.RollingFileAppender(文件大小到达指定尺寸的时候产生新文件) 使用 Log4j 可以帮助开发人员调试和分析程序,了解程序的...
log4j.appender.ROLLING_FILE=org.apache.log4j.RollingFileAppender log4j.appender.ROLLING_FILE.Threshold=ERROR log4j.appender.ROLLING_FILE.File=rolling.log log4j.appender.ROLLING_FILE.Append=true log4j....
- `org.apache.log4j.DailyRollingFileAppender`:根据日期滚动日志文件,每天生成一个新的日志文件。 - `org.apache.log4j.RollingFileAppender`:当文件大小达到指定值时,自动滚动日志文件,创建新文件。 - `...
log4j.appender.DailyRollingFileAppender=org.apache.log4j.DailyRollingFileAppender log4j.appender.DailyRollingFileAppender.File=mylog.txt log4j.appender.DailyRollingFileAppender.DatePattern='.'yyyy-...
四、Log4j-1.2.16的优化与维护 1. **日志级别管理**:在生产环境中,通常会将日志级别设置为WARN或ERROR,以减少不必要的日志输出,提高性能。 2. **日志分割**:为了便于管理和分析,可以配置Log4j按日期分割日志...
接下来,`log4j配置说明.txt`文件通常会提供更详细的解释和示例,包括如何配置不同的appender(如FileAppender、RollingFileAppender等),如何定义不同logger的级别,以及如何使用自定义的error handler和filter。...
- `log4j.appender.ROLLING_FILE=org.apache.log4j.RollingFileAppender`:指定了名为`ROLLING_FILE`的appender用于回滚文件输出。 - `log4j.appender.ROLLING_FILE.Threshold=ERROR`:设置此appender只记录错误级别...
log4j.appender.RFA=org.apache.log4j.DailyRollingFileAppender log4j.appender.RFA.File=${catalina.base}/logs/catalina.out log4j.appender.RFA.Append=true log4j.appender.RFA.Threshold=DEBUG log4j.appender...
Log4j 1.2.17中包括ConsoleAppender(控制台)、FileAppender(文件)、RollingFileAppender(滚动文件)等多种类型的Appender。 3. **Layout**: 格式化器,定义日志信息的输出格式。例如,SimpleLayout只显示消息...
配置Log4j主要通过log4j.properties或log4j.xml文件进行,定义Logger的级别、Appender的目标和Layout的格式。例如: ```properties log4j.rootLogger=DEBUG, FILE log4j.appender.FILE=org.apache.log4j....
Log4j 提供了多种类型的 Appender,例如 ConsoleAppender、FileAppender、RollingFileAppender 等。 log4j.appender.stdout=org.apache.log4j.ConsoleAppender 这个语句定义了一个名为 stdout 的 ConsoleAppender...
* org.apache.log4j.RollingFileAppender:文件大小到达指定尺寸的时候产生一个新的文件 * org.apache.log4j.WriterAppender:将日志信息以流格式发送到任意指定的地方 例如:log4j.appender.stdout=org.apache.log...
在“log4j按功能保存日志”的场景中,我们通常会利用Log4j的配置灵活性,将不同功能的日志分别写入不同的文件,以便于后期分析、排查问题和监控系统状态。 1. **Log4j基本概念** - **Logger**: 日志记录器,是Log4...
- `org.apache.log4j.RollingFileAppender`:根据文件大小自动滚动输出文件。 - `org.apache.log4j.WriteAppender`:将日志写入到其他输出流。 - `org.apache.log4j.jdbc.JDBCAppender`:通过 JDBC 将日志记录到...