1919 *
2020 */
2121public class Printer {
22-
22+
2323 private void writeInFile (FileWriter fw , Map <String , String > log , File logFile , double limitSize , Formater formater ) throws IOException {
2424
2525 if (limitSize == 0 || logFile .length () <= limitSize ) {
@@ -35,7 +35,7 @@ private void writeInFile(FileWriter fw, Map<String, String> log, File logFile, d
3535 if (log .get ("LEVEL" ) != null ) {
3636 fw .write (String .format (" %-5s %1s" , log .get ("LEVEL" ), formater .getSeparator ()));
3737 }
38-
38+
3939 if (log .get ("MESSAGE" ) != null ) {
4040 fw .write (String .format (" %-6s%n" , log .get ("MESSAGE" )));
4141 }
@@ -44,10 +44,37 @@ private void writeInFile(FileWriter fw, Map<String, String> log, File logFile, d
4444 //System.out.println("Log file size: " + logFile.length() + "Limit Size: " + limitSize);
4545 }
4646
47- else
48- //TODO Adeline verifie ICI la taille fileSize() puis fais une rotation
49- System .out .println ("Adeline verifie la taille fileSize() puisfais une rotation" );
50-
47+ else {
48+ File folder = logFile .getParentFile ();
49+ int counter = 0 ;
50+ for (final File fileEntry : folder .listFiles ()) {
51+ if (fileEntry .getName ().contains (logFile .getName ().split ("\\ ." )[0 ])) {
52+ if (fileEntry .getName ().contains ("_" )) {
53+ int max ;
54+ String [] tokens = fileEntry .getName ().split ("_" );
55+ String firstPart = tokens [tokens .length - 1 ];
56+ if (firstPart != null && !firstPart .isEmpty ()) {
57+ if (firstPart .split ("\\ ." )[0 ].matches ("\\ d+(\\ .\\ d+)?" )) {
58+ max = Integer .parseInt (firstPart .split ("\\ ." )[0 ]);
59+ if (counter < max ) {
60+ counter = max ;
61+ }
62+ }
63+ }
64+ }
65+ }
66+ }
67+ counter += 1 ;
68+ StringBuffer newName = new StringBuffer ();
69+ newName .append (logFile .getName ().split ("\\ ." )[0 ] + "_" + counter );
70+ if (logFile .getName ().split ("\\ ." ).length > 1 ) {
71+ newName .append ("." + logFile .getName ().split ("\\ ." )[1 ]);
72+ }
73+ //System.out.println(newName.toString());
74+ File newlogFile = new File (logFile .getParent () + "/" + newName .toString ());
75+ fw .close ();
76+ logFile .renameTo (newlogFile );
77+ }
5178 }
5279
5380 public void write (Level level , String message , String fqcn , String handler , Formater formater , String logFilePath , int logFileLimitSize ) {
@@ -65,7 +92,7 @@ public void write(Level level, String message, String fqcn, String handler, Form
6592
6693 if (log .get ("LEVEL" ) != null )
6794 System .out .printf (" %-5s %1s" , log .get ("LEVEL" ), formater .getSeparator ());
68-
95+
6996 if (log .get ("MESSAGE" ) != null )
7097 System .out .printf (" %-6s%n" , log .get ("MESSAGE" ));
7198 }
@@ -77,11 +104,11 @@ else if (handler.equals(FileHandler.class.getName())) {
77104
78105 private void writeToFile (Level level , String message , String fqcn , String name ,
79106 Formater formater , String logFilePath , double logFileLimitSize ) {
80-
107+
81108 Map <String , String > log = new HashMap <String , String >();
82109
83110 log = this .initMap (log , level , fqcn , message , formater );
84-
111+
85112 FileWriter fw = null ;
86113 final File logFile = new File (logFilePath );
87114 final File parent_directory = logFile .getParentFile ();
@@ -110,7 +137,7 @@ private void writeToFile(Level level, String message, String fqcn, String name,
110137 }
111138 }
112139 }
113-
140+
114141 private Map <String , String > initMap (Map <String , String > log , Level level , String fqcn , String message ,
115142 Formater formater ) {
116143 Date date = new Date ();
@@ -128,7 +155,7 @@ private Map<String, String> initMap(Map<String, String> log, Level level, String
128155 if (formater .isLevelEnabled ()) {
129156 log .put ("LEVEL" , level .getName ());
130157 }
131-
158+
132159 if (formater .isMessageEnabled ()) {
133160 log .put ("MESSAGE" , message );
134161 }
0 commit comments