add command line parser, implement logging
This commit is contained in:
parent
b0e3b379d3
commit
07c3128753
5 changed files with 104 additions and 5 deletions
4
.gitignore
vendored
4
.gitignore
vendored
|
|
@ -14,4 +14,6 @@ buildNumber.properties
|
|||
# Eclipse Core
|
||||
.project
|
||||
# JDT-specific (Eclipse Java Development Tools)
|
||||
.classpath
|
||||
.classpath
|
||||
# Log files
|
||||
log/
|
||||
|
|
|
|||
|
|
@ -12,3 +12,8 @@ on successful peer list retrieval - local tracker cache updated. If not, peers f
|
|||
On re-announce to destination trackers, proxied peer's real IP must be injected into announce request and
|
||||
other parameters should remain same as in original request.
|
||||
|
||||
|
||||
|
||||
|
||||
Maven exec with args:
|
||||
mvn exec:java -Dexec.args=''
|
||||
15
pom.xml
15
pom.xml
|
|
@ -45,6 +45,21 @@
|
|||
<artifactId>netty-buffer</artifactId>
|
||||
<version>4.1.114.Final</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>commons-cli</groupId>
|
||||
<artifactId>commons-cli</artifactId>
|
||||
<version>1.9.0</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.slf4j</groupId>
|
||||
<artifactId>slf4j-api</artifactId>
|
||||
<version>2.0.16</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>ch.qos.logback</groupId>
|
||||
<artifactId>logback-classic</artifactId>
|
||||
<version>1.5.11</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
|
|
|
|||
|
|
@ -1,10 +1,69 @@
|
|||
package com.mykola2312.retracker;
|
||||
|
||||
/**
|
||||
* Hello world!
|
||||
*/
|
||||
import java.io.File;
|
||||
import java.nio.file.Path;
|
||||
|
||||
import org.apache.commons.cli.CommandLine;
|
||||
import org.apache.commons.cli.DefaultParser;
|
||||
import org.apache.commons.cli.Option;
|
||||
import org.apache.commons.cli.Options;
|
||||
import org.apache.commons.cli.ParseException;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import ch.qos.logback.classic.Level;
|
||||
import ch.qos.logback.classic.LoggerContext;
|
||||
import ch.qos.logback.classic.encoder.PatternLayoutEncoder;
|
||||
import ch.qos.logback.classic.spi.ILoggingEvent;
|
||||
import ch.qos.logback.core.FileAppender;
|
||||
|
||||
public class App {
|
||||
private static final Logger log = LoggerFactory.getLogger(App.class);
|
||||
|
||||
public static void main(String[] args) {
|
||||
System.out.println("Hello World!");
|
||||
final Options options = new Options()
|
||||
.addOption("l", "log", true, "log dir path");
|
||||
final Option logDirOpt = options.getOption("log");
|
||||
|
||||
CommandLine cli = null;
|
||||
try {
|
||||
cli = new DefaultParser().parse(options, args);
|
||||
} catch (ParseException e) {
|
||||
System.err.printf("Argument error: %s\n", e.toString());
|
||||
System.exit(1);
|
||||
}
|
||||
|
||||
String logDirPath = cli.getOptionValue(logDirOpt);
|
||||
if (logDirPath != null) {
|
||||
// setup file logging
|
||||
File logDir = new File(logDirPath);
|
||||
if (!logDir.exists()) {
|
||||
if (!logDir.mkdir()) {
|
||||
System.err.println("Failed to create log dir");
|
||||
System.exit(1);
|
||||
}
|
||||
}
|
||||
|
||||
// https://stackoverflow.com/questions/47299109/programmatically-add-appender-in-logback
|
||||
final LoggerContext lc = (LoggerContext)LoggerFactory.getILoggerFactory();
|
||||
final PatternLayoutEncoder ple = new PatternLayoutEncoder();
|
||||
ple.setPattern("%d{HH:mm:ss} %-5level %logger{0} - %msg%n");
|
||||
ple.setContext(lc);
|
||||
ple.start();
|
||||
|
||||
FileAppender<ILoggingEvent> fileAppender = new FileAppender<ILoggingEvent>();
|
||||
fileAppender.setFile(Path.of(logDirPath, "log_out.log").toString());
|
||||
fileAppender.setEncoder(ple);
|
||||
fileAppender.setContext(lc);
|
||||
fileAppender.start();
|
||||
|
||||
ch.qos.logback.classic.Logger logbackLogger =
|
||||
(ch.qos.logback.classic.Logger)LoggerFactory.getLogger("com.mykola2312");
|
||||
logbackLogger.addAppender(fileAppender);
|
||||
logbackLogger.setLevel(Level.INFO);
|
||||
logbackLogger.setAdditive(false);
|
||||
}
|
||||
|
||||
log.info("retracker started!");
|
||||
}
|
||||
}
|
||||
|
|
|
|||
18
src/main/resources/logback.xml
Normal file
18
src/main/resources/logback.xml
Normal file
|
|
@ -0,0 +1,18 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<configuration>
|
||||
<appender name="CONSOLE" class="ch.qos.logback.core.ConsoleAppender">
|
||||
<layout class="ch.qos.logback.classic.PatternLayout">
|
||||
<Pattern>
|
||||
%d{HH:mm:ss} %highlight(%-5level) %logger{0} - %msg%n
|
||||
</Pattern>
|
||||
</layout>
|
||||
</appender>
|
||||
|
||||
<logger name="com.mykola2312" level="info" additivity="false">
|
||||
<appender-ref ref="CONSOLE"/>
|
||||
</logger>
|
||||
|
||||
<root level="info">
|
||||
<appender-ref ref="CONSOLE"/>
|
||||
</root>
|
||||
</configuration>
|
||||
Loading…
Add table
Reference in a new issue