1
1
package com .optilog .log .console ;
2
2
3
+ import com .optilog .log .LogInit ;
3
4
import com .optilog .log .Optilog ;
4
- import com .optilog .util .Debugging ;
5
5
import com .optilog .util .Util ;
6
6
7
7
import java .io .File ;
13
13
import java .util .zip .ZipEntry ;
14
14
import java .util .zip .ZipOutputStream ;
15
15
16
- @ Debugging // TODO
17
16
public class ZipLog {
18
17
public static void zipAllLog (boolean delete , Optilog instance ) {
18
+ if (!instance .alreadyInit ) {
19
+ LogInit .initLog (instance .settingFilePath , instance );
20
+ }
19
21
String s = DateTimeFormatter .ofPattern ("yyyy-MM-dd HH-mm-ss(SS)" ).format (LocalDateTime .now ());
20
- File info = new File (instance .info + s + "LogPackage.zip" );
21
- File error = new File (instance .error + s + "LogPackage.zip" );
22
- File warn = new File (instance .warn + s + "LogPackage.zip" );
23
- File debug = new File (instance .debug + s + "LogPackage.zip" );
24
- File fatal = new File (instance .fatal + s + "LogPackage.zip" );
22
+ File info = new File (instance .info . substring ( 0 , instance . info . length () - ( instance . allSetting . fileName . length () + 1 )) + "//" + s + "LogPackage.zip" );
23
+ File error = new File (instance .error . substring ( 0 , instance . error . length () - ( instance . allSetting . fileName . length () + 1 )) + "//" + s + "LogPackage.zip" );
24
+ File warn = new File (instance .warn . substring ( 0 , instance . warn . length () - ( instance . allSetting . fileName . length () + 1 )) + "//" + s + "LogPackage.zip" );
25
+ File debug = new File (instance .debug . substring ( 0 , instance . debug . length () - ( instance . allSetting . fileName . length () + 1 )) + "//" + s + "LogPackage.zip" );
26
+ File fatal = new File (instance .fatal . substring ( 0 , instance . fatal . length () - ( instance . allSetting . fileName . length () + 1 )) + "//" + s + "LogPackage.zip" );
25
27
try {
26
- if (!info .createNewFile ()) {
27
- Util .getOutput ().println ("Optilog Note:Failed to create zip file!" );
28
- return ;
28
+ if (!info .isFile ()) {
29
+ if (!info .createNewFile ()) {
30
+ Util .getOutput ().println ("Optilog Note:Failed to create zip file!" );
31
+ return ;
32
+ }
29
33
}
30
- if (!error .createNewFile ()) {
31
- Util .getOutput ().println ("Optilog Note:Failed to create zip file!" );
32
- return ;
34
+ if (!error .isFile ()) {
35
+ if (!error .createNewFile ()) {
36
+ Util .getOutput ().println ("Optilog Note:Failed to create zip file!" );
37
+ return ;
38
+ }
33
39
}
34
- if (!warn .createNewFile ()) {
35
- Util .getOutput ().println ("Optilog Note:Failed to create zip file!" );
36
- return ;
40
+ if (!warn .isFile ()) {
41
+ if (!warn .createNewFile ()) {
42
+ Util .getOutput ().println ("Optilog Note:Failed to create zip file!" );
43
+ return ;
44
+ }
37
45
}
38
- if (!debug .createNewFile ()) {
39
- Util .getOutput ().println ("Optilog Note:Failed to create zip file!" );
40
- return ;
46
+ if (!debug .isFile ()) {
47
+ if (!debug .createNewFile ()) {
48
+ Util .getOutput ().println ("Optilog Note:Failed to create zip file!" );
49
+ return ;
50
+ }
41
51
}
42
- if (!fatal .createNewFile ()) {
43
- Util .getOutput ().println ("Optilog Note:Failed to create zip file!" );
44
- return ;
52
+ if (!fatal .isFile ()) {
53
+ if (!fatal .createNewFile ()) {
54
+ Util .getOutput ().println ("Optilog Note:Failed to create zip file!" );
55
+ return ;
56
+ }
45
57
}
46
58
} catch (IOException e ) {
47
59
Util .getOutput ().println ("Optilog Note:Failed to create zip file!" );
48
60
e .printStackTrace ();
49
61
return ;
50
62
}
51
-
52
63
try (ZipOutputStream output = new ZipOutputStream (new FileOutputStream (info ))) {
53
- File [] file = new File (instance .info ).listFiles ();
64
+ File [] file = new File (instance .info . substring ( 0 , instance . info . length () - ( instance . allSetting . fileName . length () + 1 )) ).listFiles ();
54
65
if (file != null ) {
55
66
for (File f : file ) {
56
67
if (f .getName ().endsWith (".log" )) {
@@ -71,92 +82,100 @@ public static void zipAllLog(boolean delete, Optilog instance) {
71
82
instance .error ("Optilog Note:Failed to pack log file." , e );
72
83
}
73
84
74
- try (ZipOutputStream output = new ZipOutputStream (new FileOutputStream (error ))) {
75
- File [] file = new File (instance .error ).listFiles ();
76
- if (file != null ) {
77
- for (File f : file ) {
78
- if (f .getName ().endsWith (".log" )) {
79
- output .putNextEntry (new ZipEntry (f .getName ()));
80
- output .write (Files .readAllBytes (f .toPath ()));
81
- output .closeEntry ();
82
- if (delete ) {
83
- if (!f .delete ()) {
84
- instance .warn ("Optilog Note:Delete #1 failed!" , f .getName ());
85
+ if (!error .isFile ()) {
86
+ try (ZipOutputStream output = new ZipOutputStream (new FileOutputStream (error ))) {
87
+ File [] file = new File (instance .error .substring (0 , instance .error .length () - (instance .allSetting .fileName .length () + 1 ))).listFiles ();
88
+ if (file != null ) {
89
+ for (File f : file ) {
90
+ if (f .getName ().endsWith (".log" )) {
91
+ output .putNextEntry (new ZipEntry (f .getName ()));
92
+ output .write (Files .readAllBytes (f .toPath ()));
93
+ output .closeEntry ();
94
+ if (delete ) {
95
+ if (!f .delete ()) {
96
+ instance .warn ("Optilog Note:Delete #1 failed!" , f .getName ());
97
+ }
85
98
}
86
99
}
87
100
}
101
+ } else {
102
+ instance .info ("Optilog Note:No log file in error file path." );
88
103
}
89
- } else {
90
- instance .info ("Optilog Note:No log file in error file path." );
104
+ } catch ( IOException e ) {
105
+ instance .error ("Optilog Note:Failed to pack log file." , e );
91
106
}
92
- } catch (IOException e ) {
93
- instance .error ("Optilog Note:Failed to pack log file." , e );
94
107
}
95
108
96
- try (ZipOutputStream output = new ZipOutputStream (new FileOutputStream (warn ))) {
97
- File [] file = new File (instance .warn ).listFiles ();
98
- if (file != null ) {
99
- for (File f : file ) {
100
- if (f .getName ().endsWith (".log" )) {
101
- output .putNextEntry (new ZipEntry (f .getName ()));
102
- output .write (Files .readAllBytes (f .toPath ()));
103
- output .closeEntry ();
104
- if (delete ) {
105
- if (!f .delete ()) {
106
- instance .warn ("Optilog Note:Delete #1 failed!" , f .getName ());
109
+ if (!warn .isFile ()) {
110
+ try (ZipOutputStream output = new ZipOutputStream (new FileOutputStream (warn ))) {
111
+ File [] file = new File (instance .warn .substring (0 , instance .warn .length () - (instance .allSetting .fileName .length () + 1 ))).listFiles ();
112
+ if (file != null ) {
113
+ for (File f : file ) {
114
+ if (f .getName ().endsWith (".log" )) {
115
+ output .putNextEntry (new ZipEntry (f .getName ()));
116
+ output .write (Files .readAllBytes (f .toPath ()));
117
+ output .closeEntry ();
118
+ if (delete ) {
119
+ if (!f .delete ()) {
120
+ instance .warn ("Optilog Note:Delete #1 failed!" , f .getName ());
121
+ }
107
122
}
108
123
}
109
124
}
125
+ } else {
126
+ instance .info ("Optilog Note:No log file in warn file path." );
110
127
}
111
- } else {
112
- instance .info ("Optilog Note:No log file in warn file path." );
128
+ } catch ( IOException e ) {
129
+ instance .error ("Optilog Note:Failed to pack log file." , e );
113
130
}
114
- } catch (IOException e ) {
115
- instance .error ("Optilog Note:Failed to pack log file." , e );
116
131
}
117
132
118
- try (ZipOutputStream output = new ZipOutputStream (new FileOutputStream (debug ))) {
119
- File [] file = new File (instance .debug ).listFiles ();
120
- if (file != null ) {
121
- for (File f : file ) {
122
- if (f .getName ().endsWith (".log" )) {
123
- output .putNextEntry (new ZipEntry (f .getName ()));
124
- output .write (Files .readAllBytes (f .toPath ()));
125
- output .closeEntry ();
126
- if (delete ) {
127
- if (!f .delete ()) {
128
- instance .warn ("Optilog Note:Delete #1 failed!" , f .getName ());
133
+ if (!debug .isFile ()) {
134
+ try (ZipOutputStream output = new ZipOutputStream (new FileOutputStream (debug ))) {
135
+ File [] file = new File (instance .debug .substring (0 , instance .debug .length () - (instance .allSetting .fileName .length () + 1 ))).listFiles ();
136
+ if (file != null ) {
137
+ for (File f : file ) {
138
+ if (f .getName ().endsWith (".log" )) {
139
+ output .putNextEntry (new ZipEntry (f .getName ()));
140
+ output .write (Files .readAllBytes (f .toPath ()));
141
+ output .closeEntry ();
142
+ if (delete ) {
143
+ if (!f .delete ()) {
144
+ instance .warn ("Optilog Note:Delete #1 failed!" , f .getName ());
145
+ }
129
146
}
130
147
}
131
148
}
149
+ } else {
150
+ instance .info ("Optilog Note:No log file in debug file path." );
132
151
}
133
- } else {
134
- instance .info ("Optilog Note:No log file in debug file path." );
152
+ } catch ( IOException e ) {
153
+ instance .error ("Optilog Note:Failed to pack log file." , e );
135
154
}
136
- } catch (IOException e ) {
137
- instance .error ("Optilog Note:Failed to pack log file." , e );
138
155
}
139
156
140
- try (ZipOutputStream output = new ZipOutputStream (new FileOutputStream (fatal ))) {
141
- File [] file = new File (instance .fatal ).listFiles ();
142
- if (file != null ) {
143
- for (File f : file ) {
144
- if (f .getName ().endsWith (".log" )) {
145
- output .putNextEntry (new ZipEntry (f .getName ()));
146
- output .write (Files .readAllBytes (f .toPath ()));
147
- output .closeEntry ();
148
- if (delete ) {
149
- if (!f .delete ()) {
150
- instance .warn ("Optilog Note:Delete #1 failed!" , f .getName ());
157
+ if (!fatal .isFile ()) {
158
+ try (ZipOutputStream output = new ZipOutputStream (new FileOutputStream (fatal ))) {
159
+ File [] file = new File (instance .fatal .substring (0 , instance .fatal .length () - (instance .allSetting .fileName .length () + 1 ))).listFiles ();
160
+ if (file != null ) {
161
+ for (File f : file ) {
162
+ if (f .getName ().endsWith (".log" )) {
163
+ output .putNextEntry (new ZipEntry (f .getName ()));
164
+ output .write (Files .readAllBytes (f .toPath ()));
165
+ output .closeEntry ();
166
+ if (delete ) {
167
+ if (!f .delete ()) {
168
+ instance .warn ("Optilog Note:Delete #1 failed!" , f .getName ());
169
+ }
151
170
}
152
171
}
153
172
}
173
+ } else {
174
+ instance .info ("Optilog Note:No log file in fatal file path." );
154
175
}
155
- } else {
156
- instance .info ("Optilog Note:No log file in fatal file path." );
176
+ } catch ( IOException e ) {
177
+ instance .error ("Optilog Note:Failed to pack log file." , e );
157
178
}
158
- } catch (IOException e ) {
159
- instance .error ("Optilog Note:Failed to pack log file." , e );
160
179
}
161
180
}
162
181
}
0 commit comments