log4j1 修改FileAppender解决当天的文件没有日期后缀
直接上代码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 | /* * 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(); } } |
运行效果:
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
· 一个奇形怪状的面试题:Bean中的CHM要不要加volatile?
· Obsidian + DeepSeek:免费 AI 助力你的知识管理,让你的笔记飞起来!
· 分享4款.NET开源、免费、实用的商城系统
· 解决跨域问题的这6种方案,真香!
· 5. Nginx 负载均衡配置案例(附有详细截图说明++)
· Windows 提权-UAC 绕过
2014-10-26 Spring4+quartz2集群借助邮箱或是短信实现生日的农历提醒(Quartz实现农历、阴历、公历生日提醒)
2014-10-26 jquery 带农历天干地支的日期选择控件
2009-10-26 不会提示是否关闭浏览器 IE6、IE7、FF通用代码