Skip to content

Commit 8ff372b

Browse files
committed
Logger supports the maximum file size.
1 parent df353a3 commit 8ff372b

1 file changed

Lines changed: 103 additions & 69 deletions

File tree

src/main/java/com/scudata/common/ScudataLogger.java

Lines changed: 103 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -216,11 +216,12 @@ private static Handler getHandler(String name, Properties p)
216216
String file = tmp;
217217
tmp = p.getProperty(name + ".Encoding");
218218
String buf = p.getProperty(name + ".isFixedFileName");
219+
String maxSize =p.getProperty(name + ".maxSize");
219220
boolean isFixedFileName = false;
220221
if (StringUtils.isValidString(buf)) {
221222
isFixedFileName = Boolean.parseBoolean(buf);
222223
}
223-
h = logger.new FileHandler(file, tmp, isFixedFileName);
224+
h = logger.new FileHandler(file, tmp, isFixedFileName,maxSize);
224225
}
225226
tmp = p.getProperty(name + ".Level");
226227
if (StringUtils.isValidString(tmp)) {
@@ -422,66 +423,6 @@ private synchronized String getDateMark() {
422423
return formatter.format(new Date());
423424
}
424425

425-
private ArrayList<String> bufFiles = new ArrayList<String>();
426-
private Object[] getLogFile(String fileName, boolean isFixedFileName) {
427-
Object[] files = new Object[2];//第0返回工作File,第1返回绝对路径
428-
File f = new File(fileName);
429-
if (!f.isAbsolute()) {
430-
String home = System.getProperty("start.home");
431-
if (home != null) {
432-
f = new File(home, fileName);
433-
} else {
434-
ServletContext sc = Env.getApplication();
435-
if (sc != null) {
436-
home = sc.getRealPath("/");
437-
}
438-
if (home != null) {
439-
f = new File(home, fileName);// 相对于web应用的根目录
440-
} else {
441-
// 这种情况为war包发布时,home仍然会为null,此时相对于当前的工作路径,也即web
442-
// server的启动exe的路径。
443-
f = new File(f.getAbsolutePath());// 在当前工作路径下文件
444-
}
445-
}
446-
files[1] = f.getAbsolutePath();
447-
}
448-
String filePath;
449-
if (isFixedFileName) {
450-
filePath = f.getAbsolutePath();
451-
} else {
452-
String parentPath = f.getParent();
453-
String file = f.getName();
454-
if (!parentPath.endsWith(File.separator)) {
455-
parentPath += File.separator;
456-
}
457-
String pattern = parentPath;// + File.separator;
458-
if (file.endsWith(".log")) {
459-
pattern += file.substring(0, file.length() - 4);
460-
} else {
461-
pattern += file;
462-
}
463-
currentMark = getDateMark();
464-
filePath = pattern + "_" + currentMark + ".log";
465-
}
466-
if(!bufFiles.contains(filePath)) {
467-
if(!Logger.isUseSLF4J()) {//使用框架时,设置的日志配置便用不上了
468-
System.err.println("Raqsoft is using log file:\r\n" + filePath + "\r\n");
469-
}
470-
471-
bufFiles.add(filePath);
472-
if(bufFiles.size()>1024) {
473-
bufFiles.clear();
474-
}
475-
}
476-
477-
f = new File(filePath);
478-
File p = f.getParentFile();
479-
if (!p.exists()) {
480-
p.mkdirs();
481-
}
482-
files[0] = f;
483-
return files;
484-
}
485426

486427
abstract class Handler {
487428
int logLevel = iDEBUG;// iINFO;
@@ -520,24 +461,27 @@ void close() {
520461
public class FileHandler extends Handler {
521462
String fileName, encoding = "UTF-8";
522463
boolean isFixedFileName = false;
464+
int maxFileSize = 10*1024*1024;
523465
String absolutePath = null;//由于构造相对路径的文件时,构造条件的不同可能造成绝对路径不一致,加上绝对路径,用于一旦文件生成绝对路径后,以后所有路径都用该绝对路径
466+
File currentFile = null;
524467
BufferedWriter br = null;
525468
FileOutputStream fos = null;
526469

527470
public FileHandler(String file) throws Exception {
528-
this(file, null,false);
471+
this(file, null,false,null);
529472
}
530-
531-
public FileHandler(String file, String encode,boolean isFixedFileName) throws Exception {
473+
// maxSize: 单位为M, 可以写10, 或者10M
474+
public FileHandler(String file, String encode,boolean isFixedFileName,String maxSize) throws Exception {
532475
this.fileName = file;
533476
this.isFixedFileName = isFixedFileName;
477+
setMaxFileSize(maxSize);
534478
if (encode != null && !encode.isEmpty()) {
535479
this.encoding = encode;
536480
}
537481
Object[] files = getLogFile( getBaseFile(), isFixedFileName);
538-
File f = (File)files[0];
482+
currentFile = (File)files[0];
539483
absolutePath = (String)files[1];
540-
fos = new FileOutputStream(f, true);
484+
fos = new FileOutputStream(currentFile, true);
541485
br = new BufferedWriter(new OutputStreamWriter(fos, encoding));
542486
}
543487

@@ -553,16 +497,36 @@ public void setFixedFileName(boolean fix) {
553497
isFixedFileName = fix;
554498
}
555499

500+
/**
501+
* 设置最大文件尺寸,单位M
502+
* @param maxSize
503+
*/
504+
public void setMaxFileSize(String maxSize) {
505+
if(StringUtils.isValidString(maxSize)) {
506+
try {
507+
if(maxSize.toLowerCase().endsWith("m")) {
508+
int len = maxSize.length();
509+
maxSize = maxSize.substring(0,len-1);
510+
}
511+
maxFileSize = Integer.parseInt(maxSize)*1024*1024;
512+
}catch(Exception x) {
513+
514+
}
515+
}
516+
}
517+
556518
void doLog(int level, String msg) {
557519
if (level > logLevel)
558520
return;
559521
if (!isFixedFileName) {
560522
String mark = getDateMark();
561-
if (!currentMark.equals(mark)) {
523+
524+
if (!currentMark.equals(mark) ||
525+
(currentFile!=null && currentFile.length()>maxFileSize)) {
562526
try {
563527
br.close();
564-
File f = (File)getLogFile(getBaseFile(),isFixedFileName)[0];
565-
fos = new FileOutputStream(f, true);
528+
currentFile = (File)getLogFile(getBaseFile(),isFixedFileName)[0];
529+
fos = new FileOutputStream(currentFile, true);
566530
br = new BufferedWriter(new OutputStreamWriter(fos,
567531
encoding));
568532
} catch (Exception e1) {
@@ -586,6 +550,76 @@ void close() {
586550
} catch (Exception e) {
587551
}
588552
}
553+
554+
private ArrayList<String> bufFiles = new ArrayList<String>();
555+
private Object[] getLogFile(String fileName, boolean isFixedFileName) {
556+
Object[] files = new Object[2];//第0返回工作File,第1返回绝对路径
557+
File f = new File(fileName);
558+
if (!f.isAbsolute()) {
559+
String home = System.getProperty("start.home");
560+
if (home != null) {
561+
f = new File(home, fileName);
562+
} else {
563+
ServletContext sc = Env.getApplication();
564+
if (sc != null) {
565+
home = sc.getRealPath("/");
566+
}
567+
if (home != null) {
568+
f = new File(home, fileName);// 相对于web应用的根目录
569+
} else {
570+
// 这种情况为war包发布时,home仍然会为null,此时相对于当前的工作路径,也即web
571+
// server的启动exe的路径。
572+
f = new File(f.getAbsolutePath());// 在当前工作路径下文件
573+
}
574+
}
575+
files[1] = f.getAbsolutePath();
576+
}
577+
String filePath;
578+
if (isFixedFileName) {
579+
filePath = f.getAbsolutePath();
580+
} else {
581+
String parentPath = f.getParent();
582+
String file = f.getName();
583+
if (!parentPath.endsWith(File.separator)) {
584+
parentPath += File.separator;
585+
}
586+
String pattern = parentPath;// + File.separator;
587+
if (file.endsWith(".log")) {
588+
pattern += file.substring(0, file.length() - 4);
589+
} else {
590+
pattern += file;
591+
}
592+
currentMark = getDateMark();
593+
int count=0;
594+
filePath = pattern + "_" + currentMark+ count + ".log";
595+
File tmp = new File(filePath);
596+
while(tmp.length()>maxFileSize) {
597+
count = count + 1;
598+
filePath = pattern + "_" + currentMark+ count + ".log";
599+
tmp = new File(filePath);
600+
}
601+
}
602+
if(!bufFiles.contains(filePath)) {
603+
if(!Logger.isUseSLF4J()) {//使用框架时,设置的日志配置便用不上了
604+
System.err.println("Raqsoft is using log file:\r\n" + filePath + "\r\n");
605+
}
606+
607+
bufFiles.add(filePath);
608+
if(bufFiles.size()>1024) {
609+
bufFiles.clear();
610+
}
611+
}
612+
613+
f = new File(filePath);
614+
615+
File p = f.getParentFile();
616+
if (!p.exists()) {
617+
p.mkdirs();
618+
}
619+
files[0] = f;
620+
return files;
621+
}
622+
589623
}
590624

591625
public static void main(String[] args) throws Exception {

0 commit comments

Comments
 (0)