🔊 chore: Unify logs to logs folder
This commit is contained in:
parent
537fef36e9
commit
faba558f83
5 changed files with 87 additions and 52 deletions
|
|
@ -2,11 +2,15 @@ package telegram.files;
|
|||
|
||||
import cn.hutool.core.convert.Convert;
|
||||
import cn.hutool.core.io.FileUtil;
|
||||
import cn.hutool.core.util.ArrayUtil;
|
||||
import cn.hutool.core.util.StrUtil;
|
||||
import cn.hutool.log.Log;
|
||||
import cn.hutool.log.LogFactory;
|
||||
import cn.hutool.log.dialect.jdk.JdkLog;
|
||||
|
||||
import java.io.File;
|
||||
import java.util.logging.Level;
|
||||
import java.util.logging.Logger;
|
||||
import java.io.IOException;
|
||||
import java.util.logging.*;
|
||||
|
||||
public class Config {
|
||||
public static final String LOG_LEVEL = StrUtil.blankToDefault(System.getenv("LOG_LEVEL"), "INFO");
|
||||
|
|
@ -15,6 +19,8 @@ public class Config {
|
|||
|
||||
public static final String APP_ROOT = System.getenv("APP_ROOT");
|
||||
|
||||
public static final String LOG_PATH = APP_ROOT + File.separator + "logs";
|
||||
|
||||
public static final String TELEGRAM_ROOT = APP_ROOT + File.separator + "account";
|
||||
|
||||
public static final int TELEGRAM_API_ID = Convert.toInt(System.getenv("TELEGRAM_API_ID"), 0);
|
||||
|
|
@ -39,6 +45,52 @@ public class Config {
|
|||
throw new RuntimeException("TELEGRAM_API_HASH is not set");
|
||||
}
|
||||
|
||||
if (!FileUtil.exist(APP_ROOT)) {
|
||||
FileUtil.mkdir(APP_ROOT);
|
||||
}
|
||||
if (!FileUtil.exist(TELEGRAM_ROOT)) {
|
||||
FileUtil.mkdir(TELEGRAM_ROOT);
|
||||
}
|
||||
if (!FileUtil.exist(LOG_PATH)) {
|
||||
FileUtil.mkdir(LOG_PATH);
|
||||
}
|
||||
|
||||
initLogger();
|
||||
}
|
||||
|
||||
public static boolean isProd() {
|
||||
return "prod".equals(APP_ENV);
|
||||
}
|
||||
|
||||
public static void initLogger() {
|
||||
Logger rootLogger = Logger.getLogger("");
|
||||
rootLogger.setLevel(Level.INFO);
|
||||
|
||||
System.setProperty("java.util.logging.SimpleFormatter.format",
|
||||
"[%1$tF %1$tT] [%4$s] %5$s %6$s%n");
|
||||
|
||||
if (ArrayUtil.isNotEmpty(rootLogger.getHandlers())) {
|
||||
for (Handler handler : rootLogger.getHandlers()) {
|
||||
rootLogger.removeHandler(handler);
|
||||
}
|
||||
}
|
||||
|
||||
ConsoleHandler consoleHandler = new ConsoleHandler();
|
||||
consoleHandler.setLevel(Level.FINE);
|
||||
consoleHandler.setFormatter(new SimpleFormatter());
|
||||
rootLogger.addHandler(consoleHandler);
|
||||
|
||||
try {
|
||||
String logFilePattern = LOG_PATH + File.separator + "api.log";
|
||||
|
||||
FileHandler fileHandler = new FileHandler(logFilePattern, 5000000, 3, true);
|
||||
fileHandler.setLevel(Level.FINE);
|
||||
fileHandler.setFormatter(new SimpleFormatter());
|
||||
rootLogger.addHandler(fileHandler);
|
||||
} catch (IOException e) {
|
||||
System.out.println("Failed to create log FileHandler: " + e.getMessage());
|
||||
}
|
||||
|
||||
try {
|
||||
logLevel = Level.parse(Config.LOG_LEVEL);
|
||||
System.out.println("Setting telegram.files log level to " + logLevel);
|
||||
|
|
@ -46,17 +98,24 @@ public class Config {
|
|||
System.err.println("Invalid log level [" + Config.LOG_LEVEL + "], using default INFO.");
|
||||
}
|
||||
|
||||
Logger.getLogger("telegram.files").setLevel(logLevel);
|
||||
Logger nettyLogger = Logger.getLogger("io.netty");
|
||||
nettyLogger.setLevel(Level.WARNING);
|
||||
|
||||
if (!FileUtil.exist(APP_ROOT)) {
|
||||
FileUtil.mkdir(APP_ROOT);
|
||||
}
|
||||
if (!FileUtil.exist(TELEGRAM_ROOT)) {
|
||||
FileUtil.mkdir(TELEGRAM_ROOT);
|
||||
}
|
||||
Logger.getLogger("telegram.files").setLevel(logLevel);
|
||||
}
|
||||
|
||||
public static boolean isProd() {
|
||||
return "prod".equals(APP_ENV);
|
||||
public static class JDKLogFactory extends LogFactory {
|
||||
|
||||
public JDKLogFactory() {
|
||||
super("JDK Logging");
|
||||
}
|
||||
|
||||
public Log createLog(String name) {
|
||||
return new JdkLog(name);
|
||||
}
|
||||
|
||||
public Log createLog(Class<?> clazz) {
|
||||
return new JdkLog(clazz);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -9,7 +9,6 @@ import io.vertx.core.http.HttpHeaders;
|
|||
import io.vertx.core.http.HttpMethod;
|
||||
import io.vertx.core.http.HttpServerRequest;
|
||||
import io.vertx.core.http.HttpServerResponse;
|
||||
import io.vertx.core.net.impl.URIDecoder;
|
||||
import io.vertx.ext.web.RoutingContext;
|
||||
|
||||
import java.nio.charset.Charset;
|
||||
|
|
@ -33,17 +32,9 @@ public class FileRouteHandler {
|
|||
if (!request.isEnded()) {
|
||||
request.pause();
|
||||
}
|
||||
// decode URL path
|
||||
String uriDecodedPath = URIDecoder.decodeURIComponent(context.normalizedPath(), false);
|
||||
// if the normalized path is null it cannot be resolved
|
||||
if (uriDecodedPath == null) {
|
||||
LOG.warn("Invalid path: " + context.request().path());
|
||||
context.next();
|
||||
return;
|
||||
}
|
||||
|
||||
// Access fileSystem once here to be safe
|
||||
FileSystem fs = context.vertx().fileSystem();
|
||||
|
||||
sendStatic(context, fs, path, mimeType);
|
||||
}
|
||||
}
|
||||
|
|
@ -87,7 +78,9 @@ public class FileRouteHandler {
|
|||
if (!context.request().isEnded()) {
|
||||
context.request().resume();
|
||||
}
|
||||
context.fail(res.cause());
|
||||
if (LOG.isTraceEnabled()) {
|
||||
LOG.trace("Failed to read file properties", res.cause());
|
||||
}
|
||||
}
|
||||
});
|
||||
});
|
||||
|
|
@ -173,7 +166,9 @@ public class FileRouteHandler {
|
|||
if (!context.request().isEnded()) {
|
||||
context.request().resume();
|
||||
}
|
||||
context.fail(res2.cause());
|
||||
if (LOG.isTraceEnabled()) {
|
||||
LOG.trace("Failed to send file", res2.cause());
|
||||
}
|
||||
}
|
||||
});
|
||||
} else {
|
||||
|
|
@ -191,7 +186,9 @@ public class FileRouteHandler {
|
|||
if (!context.request().isEnded()) {
|
||||
context.request().resume();
|
||||
}
|
||||
context.fail(res2.cause());
|
||||
if (LOG.isTraceEnabled()) {
|
||||
LOG.trace("Failed to send file", res2.cause());
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
|
|
|||
|
|
@ -7,6 +7,10 @@ import io.vertx.core.Vertx;
|
|||
import java.util.concurrent.CountDownLatch;
|
||||
|
||||
public class Start {
|
||||
static {
|
||||
LogFactory.setCurrentLogFactory(new Config.JDKLogFactory());
|
||||
}
|
||||
|
||||
private static final Log log = LogFactory.get();
|
||||
|
||||
public static final String VERSION = "0.1.14";
|
||||
|
|
|
|||
|
|
@ -9,6 +9,7 @@ import org.drinkless.tdlib.TdApi;
|
|||
|
||||
import java.io.IOError;
|
||||
import java.io.IOException;
|
||||
import java.nio.file.Path;
|
||||
|
||||
public class TelegramClient {
|
||||
private static final Log log = LogFactory.get();
|
||||
|
|
@ -22,7 +23,8 @@ public class TelegramClient {
|
|||
|
||||
try {
|
||||
Client.execute(new TdApi.SetLogVerbosityLevel(Config.TELEGRAM_LOG_LEVEL));
|
||||
Client.execute(new TdApi.SetLogStream(new TdApi.LogStreamFile("tdlib.log", 1 << 27, false)));
|
||||
Client.execute(new TdApi.SetLogStream(new TdApi.LogStreamFile(Path.of(Config.LOG_PATH, "tdlib.log").toString(),
|
||||
1 << 27, false)));
|
||||
} catch (Client.ExecutionException error) {
|
||||
throw new IOError(new IOException("Write access to the current directory is required"));
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,27 +0,0 @@
|
|||
# \u5168\u5C40\u65E5\u5FD7\u7EA7\u522B\uFF08\u9ED8\u8BA4\u7EA7\u522B\uFF09, ALL < FINEST < FINER < FINE < CONFIG < INFO < WARNING < SEVERE < OFF
|
||||
.level = INFO
|
||||
|
||||
# \u63A7\u5236\u53F0 Handler \u914D\u7F6E
|
||||
handlers = java.util.logging.ConsoleHandler, java.util.logging.FileHandler
|
||||
|
||||
# \u63A7\u5236\u53F0 Handler \u7684\u65E5\u5FD7\u7EA7\u522B
|
||||
java.util.logging.ConsoleHandler.level = FINE
|
||||
|
||||
# \u63A7\u5236\u53F0 Handler \u7684\u65E5\u5FD7\u683C\u5F0F\u5316\u5668
|
||||
java.util.logging.ConsoleHandler.formatter = java.util.logging.SimpleFormatter
|
||||
|
||||
# \u6587\u4EF6 Handler \u7684\u65E5\u5FD7\u7EA7\u522B
|
||||
java.util.logging.FileHandler.level = FINE
|
||||
|
||||
## \u6587\u4EF6\u65E5\u5FD7\u8DEF\u5F84\u548C\u683C\u5F0F\uFF0C%u \u548C %g \u7528\u4E8E\u751F\u6210\u552F\u4E00\u7684\u65E5\u5FD7\u6587\u4EF6
|
||||
java.util.logging.FileHandler.pattern = api.log
|
||||
java.util.logging.FileHandler.limit = 5000000 # \u6587\u4EF6\u5927\u5C0F\u9650\u5236\uFF08\u5B57\u8282\uFF09
|
||||
java.util.logging.FileHandler.count = 3 # \u65E5\u5FD7\u6587\u4EF6\u8F6E\u66FF\u4E2A\u6570
|
||||
java.util.logging.FileHandler.append = true # \u662F\u5426\u8FFD\u52A0\u5230\u6587\u4EF6
|
||||
java.util.logging.FileHandler.formatter = java.util.logging.SimpleFormatter
|
||||
|
||||
# SimpleFormatter \u7684\u65E5\u5FD7\u683C\u5F0F
|
||||
java.util.logging.SimpleFormatter.format = [%1$tF %1$tT] [%4$s] %5$s %6$s%n
|
||||
#
|
||||
## \u4E3A\u7279\u5B9A\u7684\u65E5\u5FD7\u5668\u8BBE\u7F6E\u65E5\u5FD7\u7EA7\u522B
|
||||
io.netty.level = WARNING
|
||||
Loading…
Reference in a new issue