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