cli: путь до настроек логгера
This commit is contained in:
@@ -15,11 +15,11 @@
|
|||||||
### Gradle
|
### Gradle
|
||||||
|
|
||||||
```shell
|
```shell
|
||||||
gradle :server:run --args="--config=config.yml" --project-prop jvmArgs="-Dlogback.configurationFile=logback.xml"
|
gradle :server:run --args="--config=config.yml --logconfig==logback.xml"
|
||||||
```
|
```
|
||||||
|
|
||||||
### Jar
|
### Jar
|
||||||
|
|
||||||
```shell
|
```shell
|
||||||
java -Dlogback.configurationFile=logback.xml -jar server.jar --config=config.yml
|
java -jar server.jar --config=config.yml --logconfig==logback.xml
|
||||||
```
|
```
|
||||||
@@ -1,5 +1,8 @@
|
|||||||
package mc.server;
|
package mc.server;
|
||||||
|
|
||||||
|
import ch.qos.logback.classic.LoggerContext;
|
||||||
|
import ch.qos.logback.classic.joran.JoranConfigurator;
|
||||||
|
import ch.qos.logback.core.joran.spi.JoranException;
|
||||||
import joptsimple.OptionParser;
|
import joptsimple.OptionParser;
|
||||||
import joptsimple.OptionSet;
|
import joptsimple.OptionSet;
|
||||||
import joptsimple.util.PathConverter;
|
import joptsimple.util.PathConverter;
|
||||||
@@ -19,6 +22,7 @@ import mc.server.di.ConfigModule;
|
|||||||
import mc.server.di.DaggerServerComponent;
|
import mc.server.di.DaggerServerComponent;
|
||||||
import mc.server.di.ServerComponent;
|
import mc.server.di.ServerComponent;
|
||||||
import org.apache.commons.io.IOUtils;
|
import org.apache.commons.io.IOUtils;
|
||||||
|
import org.slf4j.LoggerFactory;
|
||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.io.InputStream;
|
import java.io.InputStream;
|
||||||
@@ -35,6 +39,7 @@ import java.util.Objects;
|
|||||||
@SuppressWarnings("java:S106")
|
@SuppressWarnings("java:S106")
|
||||||
public class Main {
|
public class Main {
|
||||||
private static final String CLI_CONFIG = "config";
|
private static final String CLI_CONFIG = "config";
|
||||||
|
private static final String CLI_LOGCONFIG = "logconfig";
|
||||||
|
|
||||||
private void run(OptionSet optionSet) {
|
private void run(OptionSet optionSet) {
|
||||||
log.info("mc-project launch");
|
log.info("mc-project launch");
|
||||||
@@ -104,7 +109,7 @@ public class Main {
|
|||||||
return;
|
return;
|
||||||
} else if (optionSet.has("init")) {
|
} else if (optionSet.has("init")) {
|
||||||
Path configPath = (Path) optionSet.valueOf(CLI_CONFIG);
|
Path configPath = (Path) optionSet.valueOf(CLI_CONFIG);
|
||||||
Path logbackPath = Paths.get(System.getProperty("logback.configurationFile", "logback.xml"));
|
Path logbackPath = (Path) optionSet.valueOf(CLI_LOGCONFIG);
|
||||||
|
|
||||||
if (!initializeCheckFiles(configPath, logbackPath)) {
|
if (!initializeCheckFiles(configPath, logbackPath)) {
|
||||||
return;
|
return;
|
||||||
@@ -123,6 +128,8 @@ public class Main {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
reconfigureLogback(optionSet);
|
||||||
|
|
||||||
if (log.isDebugEnabled()) {
|
if (log.isDebugEnabled()) {
|
||||||
optionSet.asMap().forEach((optionSpec, objects) -> {
|
optionSet.asMap().forEach((optionSpec, objects) -> {
|
||||||
if (optionSpec.isForHelp()) return;
|
if (optionSpec.isForHelp()) return;
|
||||||
@@ -135,12 +142,19 @@ public class Main {
|
|||||||
|
|
||||||
private static OptionParser createOptionParser() {
|
private static OptionParser createOptionParser() {
|
||||||
OptionParser optionParser = new OptionParser();
|
OptionParser optionParser = new OptionParser();
|
||||||
|
|
||||||
optionParser.acceptsAll(List.of("h", "help"), "Help page").forHelp();
|
optionParser.acceptsAll(List.of("h", "help"), "Help page").forHelp();
|
||||||
|
optionParser.accepts("init", "Initialize environment");
|
||||||
|
|
||||||
optionParser.accepts(CLI_CONFIG, "Path to configuration file")
|
optionParser.accepts(CLI_CONFIG, "Path to configuration file")
|
||||||
.withRequiredArg()
|
.withRequiredArg()
|
||||||
.withValuesConvertedBy(new PathConverter())
|
.withValuesConvertedBy(new PathConverter())
|
||||||
.defaultsTo(Paths.get("config.yml"));
|
.defaultsTo(Paths.get("config.yml"));
|
||||||
optionParser.accepts("init", "Initialize environment");
|
|
||||||
|
optionParser.accepts(CLI_LOGCONFIG, "Path to logger configuratuin file")
|
||||||
|
.withRequiredArg()
|
||||||
|
.withValuesConvertedBy(new PathConverter())
|
||||||
|
.defaultsTo(Paths.get("logback.xml"));
|
||||||
|
|
||||||
return optionParser;
|
return optionParser;
|
||||||
}
|
}
|
||||||
@@ -167,4 +181,20 @@ public class Main {
|
|||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private static void reconfigureLogback(OptionSet optionSet) throws IOException {
|
||||||
|
LoggerContext logbackContext = (LoggerContext) LoggerFactory.getILoggerFactory();
|
||||||
|
logbackContext.reset();
|
||||||
|
JoranConfigurator configurator = new JoranConfigurator();
|
||||||
|
|
||||||
|
Path logbackPath = (Path) optionSet.valueOf(CLI_LOGCONFIG);
|
||||||
|
try(InputStream in = Objects.requireNonNull(
|
||||||
|
Files.newInputStream(logbackPath), "File not found: " + logbackPath.toAbsolutePath())) {
|
||||||
|
|
||||||
|
configurator.setContext(logbackContext);
|
||||||
|
configurator.doConfigure(in);
|
||||||
|
} catch (JoranException e) {
|
||||||
|
throw new IOException(e);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user