add --log-level=debug/info/warn/error flag, so we can close the log print during db gen

This commit is contained in:
lion 2025-06-10 22:55:52 +08:00
parent ed57fa5c52
commit bff6b5661c
3 changed files with 24 additions and 22 deletions

View File

@ -19,12 +19,13 @@ public class MakerTest {
System.out.println("ip2region xdb maker"); System.out.println("ip2region xdb maker");
System.out.println("java -jar ip2region-maker-{version}.jar [command options]"); System.out.println("java -jar ip2region-maker-{version}.jar [command options]");
System.out.println("options:"); System.out.println("options:");
System.out.println(" --src string source ip text file path"); System.out.println(" --src string source ip text file path");
System.out.println(" --dst string destination binary xdb file path"); System.out.println(" --dst string destination binary xdb file path");
System.out.println(" --log-level string set the log level, options: debug/info/warn/error");
} }
public static void genDb(String[] args) throws Exception { public static void genDb(String[] args) throws Exception {
String srcFile = "", dstFile = ""; String srcFile = "", dstFile = "", logLevel = "info";
int indexPolicy = IndexPolicy.Vector; int indexPolicy = IndexPolicy.Vector;
for (final String r : args) { for (final String r : args) {
if (r.length() < 5) { if (r.length() < 5) {
@ -48,25 +49,23 @@ public class MakerTest {
srcFile = val; srcFile = val;
} else if ("dst".equals(key)) { } else if ("dst".equals(key)) {
dstFile = val; dstFile = val;
} else if ("index".equals(key)) { } else if ("log-level".equals(key)) {
try { logLevel = val;
indexPolicy = IndexPolicy.parse(val);
} catch (Exception e) {
System.out.println("parse policy " + e);
}
} else { } else {
System.out.printf("undefined option `%s`\n", r); System.out.printf("undefined option `%s`\n", r);
return; return;
} }
} }
if (srcFile.length() < 1 || dstFile.length() < 1) { if (srcFile.isEmpty() || dstFile.isEmpty()) {
printHelp(args); printHelp(args);
return; return;
} }
long tStart = System.currentTimeMillis(); long tStart = System.currentTimeMillis();
Maker maker = new Maker(indexPolicy, srcFile, dstFile); final Maker maker = new Maker(indexPolicy, srcFile, dstFile);
log.infof("Generating xdb with src=%s, dst=%s, logLevel=%s\n", srcFile, dstFile, logLevel);
Maker.log.setLevel(logLevel);
maker.init(); maker.init();
maker.start(); maker.start();
maker.end(); maker.end();

View File

@ -28,7 +28,7 @@ public class Log {
}; };
public final Class<?> baseClass; public final Class<?> baseClass;
private static int level; private int level = INFO;
public Log(Class<?> baseClass) { public Log(Class<?> baseClass) {
this.baseClass = baseClass; this.baseClass = baseClass;
@ -56,7 +56,7 @@ public class Log {
} }
// level filter // level filter
if (level < Log.level) { if (level < this.level) {
return; return;
} }
@ -96,21 +96,24 @@ public class Log {
printf(ERROR, format, args); printf(ERROR, format, args);
} }
public static void setLevel(int level) { public Log setLevel(int level) {
Log.level = level; this.level = level;
return this;
} }
public static void setLevel(String level) { public Log setLevel(String level) {
String v = level.toLowerCase(); String v = level.toLowerCase();
if ("debug".equals(v)) { if ("debug".equals(v)) {
Log.level = DEBUG; this.level = DEBUG;
} else if ("info".equals(v)) { } else if ("info".equals(v)) {
Log.level = INFO; this.level = INFO;
} else if ("warn".equals(v)) { } else if ("warn".equals(v)) {
Log.level = WARN; this.level = WARN;
} else if ("error".equals(v)) { } else if ("error".equals(v)) {
Log.level = ERROR; this.level = ERROR;
} }
return this;
} }
} }

View File

@ -68,7 +68,7 @@ public class Maker {
public static final int SegmentIndexSize = 14; public static final int SegmentIndexSize = 14;
public static final int VectorIndexLength = VectorIndexRows * VectorIndexCols * VectorIndexSize; public static final int VectorIndexLength = VectorIndexRows * VectorIndexCols * VectorIndexSize;
private static final Log log = Log.getLogger(Maker.class); public static final Log log = Log.getLogger(Maker.class);
// source text file handle // source text file handle
private final File srcFile; private final File srcFile;
@ -199,7 +199,7 @@ public class Maker {
// start to make the binary file // start to make the binary file
public void start() throws Exception { public void start() throws Exception {
if (segments.size() == 0) { if (segments.isEmpty()) {
throw new Exception("empty segment list"); throw new Exception("empty segment list");
} }