直接上代码:

/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/ package org.apache.log4j; import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InterruptedIOException;
import java.io.Writer; import org.apache.log4j.helpers.LogLog;
import org.apache.log4j.helpers.QuietWriter;
import org.apache.log4j.spi.ErrorCode; import reyo.sdk.utils.DateTimeUtils;
import reyo.sdk.utils.StringUtils; // Contibutors: Jens Uwe Pipka <jens.pipka@gmx.de>
// Ben Sandee /**
* FileAppender appends log events to a file.
*
* <p>Support for <code>java.io.Writer</code> and console appending
* has been deprecated and then removed. See the replacement
* solutions: {@link WriterAppender} and {@link ConsoleAppender}.
*
* @author Ceki Gülcü
* */
public class FileAppender extends WriterAppender { /** Controls file truncatation. The default value for this variable
* is <code>true</code>, meaning that by default a
* <code>FileAppender</code> will append to an existing file and not
* truncate it.
*
* <p>This option is meaningful only if the FileAppender opens the
* file.
*/
protected boolean fileAppend = true; /**
The name of the log file. */
protected String fileName = null; /**
Do we do bufferedIO? */
protected boolean bufferedIO = false; /**
* Determines the size of IO buffer be. Default is 8K.
*/
protected int bufferSize = 8 * 1024; /**
The default constructor does not do anything.
*/
public FileAppender() {
} /**
Instantiate a <code>FileAppender</code> and open the file
designated by <code>filename</code>. The opened filename will
become the output destination for this appender. <p>If the <code>append</code> parameter is true, the file will be
appended to. Otherwise, the file designated by
<code>filename</code> will be truncated before being opened. <p>If the <code>bufferedIO</code> parameter is <code>true</code>,
then buffered IO will be used to write to the output file. */
public FileAppender(Layout layout, String filename, boolean append, boolean bufferedIO, int bufferSize)
throws IOException {
this.layout = layout;
this.setFile(filename, append, bufferedIO, bufferSize);
} /**
Instantiate a FileAppender and open the file designated by
<code>filename</code>. The opened filename will become the output
destination for this appender. <p>If the <code>append</code> parameter is true, the file will be
appended to. Otherwise, the file designated by
<code>filename</code> will be truncated before being opened.
*/
public FileAppender(Layout layout, String filename, boolean append) throws IOException {
this.layout = layout;
this.setFile(filename, append, false, bufferSize);
} /**
Instantiate a FileAppender and open the file designated by
<code>filename</code>. The opened filename will become the output
destination for this appender. <p>The file will be appended to. */
public FileAppender(Layout layout, String filename) throws IOException {
this(layout, filename, true);
} /**
The <b>File</b> property takes a string value which should be the
name of the file to append to. <p><font color="#DD0044"><b>Note that the special values
"System.out" or "System.err" are no longer honored.</b></font> <p>Note: Actual opening of the file is made when {@link
#activateOptions} is called, not when the options are set. */
public void setFile(String file) {
// Trim spaces from both ends. The users probably does not want
// trailing spaces in file names.
String val = file.trim();
fileName = val;
} /**
Returns the value of the <b>Append</b> option.
*/
public boolean getAppend() {
return fileAppend;
} /** Returns the value of the <b>File</b> option. */
public String getFile() {
return fileName;
} /**
If the value of <b>File</b> is not <code>null</code>, then {@link
#setFile} is called with the values of <b>File</b> and
<b>Append</b> properties. @since 0.8.1 */
@Override
public void activateOptions() { String filetype = fileName.substring(fileName.lastIndexOf(".") + 1, fileName.length());
if (StringUtils.isEmpty(filetype)) {
fileName = fileName + DateTimeUtils.getCurrentDate() + ".log";
} if (fileName != null) {
try {
setFile(fileName, fileAppend, bufferedIO, bufferSize);
} catch (java.io.IOException e) {
errorHandler.error("setFile(" + fileName + "," + fileAppend + ") call failed.", e,
ErrorCode.FILE_OPEN_FAILURE);
}
} else {
//LogLog.error("File option not set for appender ["+name+"].");
LogLog.warn("File option not set for appender [" + name + "].");
LogLog.warn("Are you using FileAppender instead of ConsoleAppender?");
}
} /**
Closes the previously opened file.
*/
protected void closeFile() {
if (this.qw != null) {
try {
this.qw.close();
} catch (java.io.IOException e) {
if (e instanceof InterruptedIOException) {
Thread.currentThread().interrupt();
}
// Exceptionally, it does not make sense to delegate to an
// ErrorHandler. Since a closed appender is basically dead.
LogLog.error("Could not close " + qw, e);
}
}
} /**
Get the value of the <b>BufferedIO</b> option. <p>BufferedIO will significatnly increase performance on heavily
loaded systems. */
public boolean getBufferedIO() {
return this.bufferedIO;
} /**
Get the size of the IO buffer.
*/
public int getBufferSize() {
return this.bufferSize;
} /**
The <b>Append</b> option takes a boolean value. It is set to
<code>true</code> by default. If true, then <code>File</code>
will be opened in append mode by {@link #setFile setFile} (see
above). Otherwise, {@link #setFile setFile} will open
<code>File</code> in truncate mode. <p>Note: Actual opening of the file is made when {@link
#activateOptions} is called, not when the options are set.
*/
public void setAppend(boolean flag) {
fileAppend = flag;
} /**
The <b>BufferedIO</b> option takes a boolean value. It is set to
<code>false</code> by default. If true, then <code>File</code>
will be opened and the resulting {@link java.io.Writer} wrapped
around a {@link BufferedWriter}. BufferedIO will significatnly increase performance on heavily
loaded systems. */
public void setBufferedIO(boolean bufferedIO) {
this.bufferedIO = bufferedIO;
if (bufferedIO) {
immediateFlush = false;
}
} /**
Set the size of the IO buffer.
*/
public void setBufferSize(int bufferSize) {
this.bufferSize = bufferSize;
} /**
<p>Sets and <i>opens</i> the file where the log output will
go. The specified file must be writable. <p>If there was already an opened file, then the previous file
is closed first. <p><b>Do not use this method directly. To configure a FileAppender
or one of its subclasses, set its properties one by one and then
call activateOptions.</b> @param fileName The path to the log file.
@param append If true will append to fileName. Otherwise will
truncate fileName. */
public synchronized void setFile(String fileName, boolean append, boolean bufferedIO, int bufferSize)
throws IOException {
LogLog.debug("setFile called: " + fileName + ", " + append); // It does not make sense to have immediate flush and bufferedIO.
if (bufferedIO) {
setImmediateFlush(false);
} reset();
FileOutputStream ostream = null;
try {
//
// attempt to create file
//
ostream = new FileOutputStream(fileName, append);
} catch (FileNotFoundException ex) {
//
// if parent directory does not exist then
// attempt to create it and try to create file
// see bug 9150
//
String parentName = new File(fileName).getParent();
if (parentName != null) {
File parentDir = new File(parentName);
if (!parentDir.exists() && parentDir.mkdirs()) {
ostream = new FileOutputStream(fileName, append);
} else {
throw ex;
}
} else {
throw ex;
}
}
Writer fw = createWriter(ostream);
if (bufferedIO) {
fw = new BufferedWriter(fw, bufferSize);
}
this.setQWForFiles(fw);
this.fileName = fileName;
this.fileAppend = append;
this.bufferedIO = bufferedIO;
this.bufferSize = bufferSize;
writeHeader();
LogLog.debug("setFile ended");
} /**
Sets the quiet writer being used. This method is overriden by {@link RollingFileAppender}.
*/
protected void setQWForFiles(Writer writer) {
this.qw = new QuietWriter(writer, errorHandler);
} /**
Close any previously opened file and call the parent's
<code>reset</code>. */
@Override
protected void reset() {
closeFile();
this.fileName = null;
super.reset();
}
}

运行效果:

log4j1 修改FileAppender解决当天的文件没有日期后缀的更多相关文章

  1. 【Python】批量修改指定目录下所有文件的文件名/后缀

    [删除.txt文件的后缀] import os, shutil #rootdir = input("请输入文件路径(结尾加上/):") #fileList = os.listdir ...

  2. 修改Android中strings.xml文件, 动态改变数据

    有些朋友可能会动态的修改Android中strings.xml文件中的值,在这里给大家推荐一种简单的方法.strings.xml中节点是支持占位符的,如下所示: <string name=&qu ...

  3. 【原创】基于部署映像服务和管理(DISM)修改映象解决WIN7 USB3.0安装时报错

    本文作者为博客园阿梓喵http://www.cnblogs.com/c4isr/,转载请注明作者. 本文源地址:http://www.cnblogs.com/c4isr/p/3532362.html ...

  4. 【IntelliJ IDEA】使用idea解决新建jsp文件而找不到jsp文件模版的新建选项

    使用idea解决新建jsp文件而找不到jsp文件模版的新建选项,这样每次创建一个新的jsp文件岂不是很耗时间? 解决办法: 就是要让idea知道你需要在这个目录下创建jsp文件 左上角,file中点击 ...

  5. 解决双击excel文件打开多个excel.exe进程的问题

    解决双击excel文件打开多个excel.exe进程的问题有些时候,双击两个excel文件,会打开多个excel进程,不同进程之间不能复制粘贴公式,只能粘贴数值,很不方便.怎么样双击多个excel文件 ...

  6. Idea解决打开大文件消耗CPU问题

    dea打开大文件的时候,会导致cpu利用率变得特别高,我这边八核i7的配置下,cpu依然飙到了600%~700%,这个时候就需要修改idea的配置(下面以Ubuntu为例). 1.进入到idea安装目 ...

  7. Java代码加密与反编译(二):用加密算法DES修改classLoader实现对.class文件加密

    Java代码加密与反编译(二):用加密算法DES修改classLoader实现对.class文件加密 二.利用加密算法DES实现java代码加密 传统的C/C++自动带有保护机制,但java不同,只要 ...

  8. 40.【IntelliJ IDEA】使用idea解决新建jsp文件而找不到jsp文件模版的新建选项

    转自:https://www.cnblogs.com/sxdcgaq8080/p/7676294.html 使用idea解决新建jsp文件而找不到jsp文件模版的新建选项,这样每次创建一个新的jsp文 ...

  9. eclipse彻底去除validation(彻底解决编辑js文件的卡顿问题)

    Eclipse中默认的JS编辑器非常慢,尤其在拷贝粘贴代码时,CPU总是占用很高甚至到100%,也就导致了编辑起来很卡. 这是因为Eclipse中带的Validate功能导致的,这个鸡肋的功能简直让人 ...

随机推荐

  1. SURF 特征匹配

    参考:http://www.cnblogs.com/ronny/p/4045979.html,博主对源码进行了分析,不过很多没看明白. 分为几个部分.积分图:借助积分图像,图像与高斯二阶微分模板的滤波 ...

  2. 为什么尽量别用 setInterval

    为什么尽量别用setInterval   在开发一个在线聊天工具时,经常会有过多少毫秒就重复执行一次某操作的需求.“没问题”,大家都说,“用setInterval好了.”我觉得这个点子很糟糕. 原因之 ...

  3. .NetCore 使用 Linq 动态拼接Expression表达式条件来实现 对EF、EF Core 扩展查询排序操作

    相信在使用EF的时候对查询条件或者排序上的处理令人心烦,下面我们就来动态拼接表达式解决这一问题 当我们在查询中使用Where的时候可以看到如下参数 下面我们就来扩展 Expression<Fun ...

  4. dojo/domReady! 中感叹号的作用

    废话 其实不算个技术问题,但实在是花了我不少时间,不记下来都对不起我这浪费掉的几十分钟. 问题 在dojo官网上看教程,跟着做点练习,看到Dojo DOM Functions那节,有一个练习是改变页面 ...

  5. Web安全之跨站脚本攻击(XSS)

    XSS 简介 跨站脚本攻击,英文全称是 Cross Site Script,本来缩写是CSS,但是为了和层叠样式表(Cascading Style Sheet,CSS)有所区别,所以在安全领域叫做&q ...

  6. P1417 烹调方案 背包DP

    题目背景 由于你的帮助,火星只遭受了最小的损失.但gw懒得重建家园了,就造了一艘飞船飞向遥远的earth星.不过飞船飞到一半,gw发现了一个很严重的问题:肚子饿了~ gw还是会做饭的,于是拿出了储藏的 ...

  7. plsql oracle 使用教程

    课程 一 PL/SQL 基本查询与排序 本课重点: 1.写SELECT语句进行数据库查询 2.进行数学运算 3.处理空值 4.使用别名ALIASES 5.连接列 6.在SQL PLUS中编辑缓冲,修改 ...

  8. AFO 我的oi生涯 大结局

    今儿个哥几个一屋子退役了,这两天也许会写一个生涯大结局留作纪念吧. 今天就写了吧. 由于在机房的原因比一般同学获得的知识更多一些.进来总是看新闻,感慨颇多.自从两会开的第一天起,我就对我们政府采取的一 ...

  9. Android任务和返回栈完全解析(转)

    转载请注明出处:http://blog.csdn.net/guolin_blog/article/details/41087993 本篇文章主要内容来自于Android Doc,我翻译之后又做了些加工 ...

  10. 2018-2019-20172329 《Java软件结构与数据结构》第五周学习总结

    2018-2019-20172329 <Java软件结构与数据结构>第五周学习总结 教材学习内容总结 <Java软件结构与数据结构>第九章-排序与查找 一.查找 1.查找概念简 ...