From 287698f4c78695e13aa31b11981fa689fea95d87 Mon Sep 17 00:00:00 2001 From: Haowei Wen Date: Mon, 2 May 2022 21:41:06 +0800 Subject: [PATCH] optimize logging --- src/main/java/moe/yushi/authlibinjector/util/Logging.java | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/src/main/java/moe/yushi/authlibinjector/util/Logging.java b/src/main/java/moe/yushi/authlibinjector/util/Logging.java index 5adb35f..ad967e6 100644 --- a/src/main/java/moe/yushi/authlibinjector/util/Logging.java +++ b/src/main/java/moe/yushi/authlibinjector/util/Logging.java @@ -1,5 +1,5 @@ /* - * Copyright (C) 2021 Haowei Wen and contributors + * Copyright (C) 2022 Haowei Wen and contributors * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU Affero General Public License as published by @@ -29,11 +29,14 @@ import java.nio.charset.Charset; import java.nio.file.Path; import java.nio.file.Paths; import java.time.Instant; +import java.util.regex.Pattern; import moe.yushi.authlibinjector.Config; public final class Logging { private Logging() {} + private static final Pattern CONTROL_CHARACTERS_FILTER = Pattern.compile("[\\p{Cc}&&[^\r\n\t]]"); + private static final PrintStream out = System.err; private static final FileChannel logfile = openLogFile(); @@ -83,13 +86,12 @@ public final class Logging { log += sw.toString(); } // remove control characters to prevent messing up the console - log = log.replaceAll("[\\p{Cc}&&[^\r\n\t]]", ""); + log = CONTROL_CHARACTERS_FILTER.matcher(log).replaceAll(""); out.println(log); if (logfile != null) { try { logfile.write(Charset.defaultCharset().encode(log + System.lineSeparator())); - logfile.force(true); } catch (IOException ex) { out.println("[authlib-injector] [ERROR] Error writing to log file: " + ex); }