Skip to content

Commit b73917d

Browse files
committed
📈 b3log#12293 Finer logging
1 parent 3875cd4 commit b73917d

File tree

1 file changed

+55
-33
lines changed

1 file changed

+55
-33
lines changed

src/main/java/org/b3log/solo/service/ImportService.java

Lines changed: 55 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import org.b3log.latke.logging.Logger;
99
import org.b3log.latke.model.User;
1010
import org.b3log.latke.service.annotation.Service;
11+
import org.b3log.latke.util.Strings;
1112
import org.b3log.solo.SoloServletListener;
1213
import org.b3log.solo.model.Article;
1314
import org.json.JSONObject;
@@ -30,7 +31,7 @@ public class ImportService {
3031
/**
3132
* Logger.
3233
*/
33-
private static final Logger LOGGER = Logger.getLogger(UpgradeService.class);
34+
private static final Logger LOGGER = Logger.getLogger(ImportService.class);
3435

3536
/**
3637
* Default tag.
@@ -50,45 +51,66 @@ public class ImportService {
5051
private UserQueryService userQueryService;
5152

5253
public void importMarkdowns() {
53-
final ServletContext servletContext = SoloServletListener.getServletContext();
54-
final String markdownsPath = servletContext.getRealPath("markdowns");
55-
LOGGER.debug("Import directory [" + markdownsPath + "]");
54+
new Thread(() -> {
55+
final ServletContext servletContext = SoloServletListener.getServletContext();
56+
final String markdownsPath = servletContext.getRealPath("markdowns");
57+
LOGGER.debug("Import directory [" + markdownsPath + "]");
5658

57-
JSONObject admin;
58-
try {
59-
admin = userQueryService.getAdmin();
60-
} catch (final Exception e) {
61-
return;
62-
}
63-
64-
if (null == admin) { // Not init yet
65-
return;
66-
}
67-
68-
final String adminEmail = admin.optString(User.USER_EMAIL);
59+
JSONObject admin;
60+
try {
61+
admin = userQueryService.getAdmin();
62+
} catch (final Exception e) {
63+
return;
64+
}
6965

70-
final Collection<File> mds = FileUtils.listFiles(new File(markdownsPath), new String[]{"md"}, true);
71-
for (final File md : mds) {
72-
final String fileName = md.getName();
73-
if (StringUtils.equalsIgnoreCase(fileName, "README.md")) {
74-
continue;
66+
if (null == admin) { // Not init yet
67+
return;
7568
}
7669

77-
try {
78-
final String fileContent = FileUtils.readFileToString(md, "UTF-8");
79-
final JSONObject article = parseArticle(fileName, fileContent);
80-
article.put(Article.ARTICLE_AUTHOR_EMAIL, adminEmail);
70+
final String adminEmail = admin.optString(User.USER_EMAIL);
71+
72+
int succCnt = 0, failCnt = 0;
73+
final Set<String> failSet = new TreeSet<>();
74+
final Collection<File> mds = FileUtils.listFiles(new File(markdownsPath), new String[]{"md"}, true);
75+
for (final File md : mds) {
76+
final String fileName = md.getName();
77+
if (StringUtils.equalsIgnoreCase(fileName, "README.md")) {
78+
continue;
79+
}
80+
81+
try {
82+
final String fileContent = FileUtils.readFileToString(md, "UTF-8");
83+
final JSONObject article = parseArticle(fileName, fileContent);
84+
article.put(Article.ARTICLE_AUTHOR_EMAIL, adminEmail);
85+
86+
final JSONObject request = new JSONObject();
87+
request.put(Article.ARTICLE, article);
88+
89+
final String id = articleMgmtService.addArticle(request);
90+
FileUtils.moveFile(md, new File(md.getPath() + "." + id));
91+
LOGGER.info("Imported article [" + article.optString(Article.ARTICLE_TITLE) + "]");
92+
succCnt++;
93+
} catch (final Exception e) {
94+
LOGGER.log(Level.ERROR, "Import file [" + fileName + "] failed", e);
95+
96+
failCnt++;
97+
failSet.add(fileName);
98+
}
99+
}
81100

82-
final JSONObject request = new JSONObject();
83-
request.put(Article.ARTICLE, article);
101+
final StringBuilder logBuilder = new StringBuilder();
102+
logBuilder.append("[").append(succCnt).append("] imported, [").append(failCnt).append("] failed");
103+
if (failCnt > 0) {
104+
logBuilder.append(": ").append(Strings.LINE_SEPARATOR);
84105

85-
final String id = articleMgmtService.addArticle(request);
86-
FileUtils.moveFile(md, new File(md.getPath() + "." + id));
87-
LOGGER.info("Imported article [" + article.optString(Article.ARTICLE_TITLE) + "]");
88-
} catch (final Exception e) {
89-
LOGGER.log(Level.ERROR, "Import file [" + fileName + "] failed", e);
106+
for (final String fail : failSet) {
107+
logBuilder.append(" ").append(fail).append(Strings.LINE_SEPARATOR);
108+
}
109+
} else {
110+
logBuilder.append(" :p");
90111
}
91-
}
112+
LOGGER.info(logBuilder.toString());
113+
}).start();
92114
}
93115

94116
private JSONObject parseArticle(final String fileName, final String fileContent) {

0 commit comments

Comments
 (0)