diff options
| author | shedaniel <daniel@shedaniel.me> | 2022-07-27 23:30:34 +0800 |
|---|---|---|
| committer | shedaniel <daniel@shedaniel.me> | 2022-08-26 10:52:27 +0900 |
| commit | acc7a8e33d34b05321711f29b2683dd83fda2deb (patch) | |
| tree | b4df36b21ccb547c2412a7a4fa9232b7fee98b09 /runtime-engine/logging/src/main/java/me/shedaniel | |
| parent | 05069aa62b09f02a8cd6e526ec58a30347a56500 (diff) | |
| download | RoughlyEnoughItems-acc7a8e33d34b05321711f29b2683dd83fda2deb.tar.gz RoughlyEnoughItems-acc7a8e33d34b05321711f29b2683dd83fda2deb.tar.bz2 RoughlyEnoughItems-acc7a8e33d34b05321711f29b2683dd83fda2deb.zip | |
Add logging module, remove build files
Diffstat (limited to 'runtime-engine/logging/src/main/java/me/shedaniel')
6 files changed, 347 insertions, 0 deletions
diff --git a/runtime-engine/logging/src/main/java/me/shedaniel/rei/impl/common/logging/FileLogger.java b/runtime-engine/logging/src/main/java/me/shedaniel/rei/impl/common/logging/FileLogger.java new file mode 100644 index 000000000..3e7d88eec --- /dev/null +++ b/runtime-engine/logging/src/main/java/me/shedaniel/rei/impl/common/logging/FileLogger.java @@ -0,0 +1,80 @@ +/* + * This file is licensed under the MIT License, part of Roughly Enough Items. + * Copyright (c) 2018, 2019, 2020, 2021, 2022 shedaniel + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ + +package me.shedaniel.rei.impl.common.logging; + +import me.shedaniel.rei.impl.common.InternalLogger; +import org.apache.commons.io.output.NullOutputStream; +import org.apache.logging.log4j.Level; +import org.jetbrains.annotations.ApiStatus; + +import java.io.*; +import java.nio.charset.StandardCharsets; +import java.nio.file.Files; +import java.nio.file.Path; +import java.time.LocalDateTime; +import java.time.format.DateTimeFormatter; + +@ApiStatus.Internal +public class FileLogger implements InternalLogger { + private static final DateTimeFormatter DATE_TIME_FORMATTER = DateTimeFormatter.ofPattern("HH:mm:ss"); + private final Writer writer; + + public FileLogger(Path file) { + Writer w; + try { + if (file.getParent() != null) Files.createDirectories(file.getParent()); + file.toFile().createNewFile(); + w = new OutputStreamWriter(new FileOutputStream(file.toFile()), StandardCharsets.UTF_8); + } catch (IOException exception) { + exception.printStackTrace(); + w = new OutputStreamWriter(new NullOutputStream()); + } + + this.writer = w; + } + + @Override + public void throwException(Throwable throwable) { + throwable.printStackTrace(new PrintWriter(writer, true)); + } + + @Override + public void log(Level level, String message) { + message = String.format("[%s] [%s/%s] %s", DATE_TIME_FORMATTER.format(LocalDateTime.now()), Thread.currentThread().getName(), level, message); + + try { + writer.write(message); + writer.write("\n"); + writer.flush(); + } catch (IOException exception) { + exception.printStackTrace(); + } + } + + @Override + public void log(Level level, String message, Throwable throwable) { + log(level, message); + throwable.printStackTrace(new PrintWriter(writer, true)); + } +} diff --git a/runtime-engine/logging/src/main/java/me/shedaniel/rei/impl/common/logging/FilteringLogger.java b/runtime-engine/logging/src/main/java/me/shedaniel/rei/impl/common/logging/FilteringLogger.java new file mode 100644 index 000000000..2e40beee8 --- /dev/null +++ b/runtime-engine/logging/src/main/java/me/shedaniel/rei/impl/common/logging/FilteringLogger.java @@ -0,0 +1,58 @@ +/* + * This file is licensed under the MIT License, part of Roughly Enough Items. + * Copyright (c) 2018, 2019, 2020, 2021, 2022 shedaniel + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ + +package me.shedaniel.rei.impl.common.logging; + +import me.shedaniel.rei.impl.common.InternalLogger; +import org.apache.logging.log4j.Level; +import org.jetbrains.annotations.ApiStatus; + +@ApiStatus.Internal +public class FilteringLogger implements InternalLogger { + private final InternalLogger logger; + private final Level minLevel; + + public FilteringLogger(InternalLogger logger, Level minLevel) { + this.logger = logger; + this.minLevel = minLevel; + } + + @Override + public void throwException(Throwable throwable) { + logger.throwException(throwable); + } + + @Override + public void log(Level level, String message) { + if (level.isLessSpecificThan(minLevel)) + return; + logger.log(level, message); + } + + @Override + public void log(Level level, String message, Throwable throwable) { + if (level.isLessSpecificThan(minLevel)) + return; + logger.log(level, message, throwable); + } +} diff --git a/runtime-engine/logging/src/main/java/me/shedaniel/rei/impl/common/logging/Log4JLogger.java b/runtime-engine/logging/src/main/java/me/shedaniel/rei/impl/common/logging/Log4JLogger.java new file mode 100644 index 000000000..3dfd8d395 --- /dev/null +++ b/runtime-engine/logging/src/main/java/me/shedaniel/rei/impl/common/logging/Log4JLogger.java @@ -0,0 +1,52 @@ +/* + * This file is licensed under the MIT License, part of Roughly Enough Items. + * Copyright (c) 2018, 2019, 2020, 2021, 2022 shedaniel + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ + +package me.shedaniel.rei.impl.common.logging; + +import me.shedaniel.rei.impl.common.InternalLogger; +import org.apache.logging.log4j.Level; +import org.jetbrains.annotations.ApiStatus; + +@ApiStatus.Internal +public class Log4JLogger implements InternalLogger { + private final org.apache.logging.log4j.Logger logger; + + public Log4JLogger(org.apache.logging.log4j.Logger logger) { + this.logger = logger; + } + + @Override + public void throwException(Throwable throwable) { + logger.throwing(throwable); + } + + @Override + public void log(Level level, String message) { + logger.log(level, message); + } + + @Override + public void log(Level level, String message, Throwable throwable) { + logger.log(level, message, throwable); + } +} diff --git a/runtime-engine/logging/src/main/java/me/shedaniel/rei/impl/common/logging/LoggerInitializer.java b/runtime-engine/logging/src/main/java/me/shedaniel/rei/impl/common/logging/LoggerInitializer.java new file mode 100644 index 000000000..88ad97977 --- /dev/null +++ b/runtime-engine/logging/src/main/java/me/shedaniel/rei/impl/common/logging/LoggerInitializer.java @@ -0,0 +1,43 @@ +/* + * This file is licensed under the MIT License, part of Roughly Enough Items. + * Copyright (c) 2018, 2019, 2020, 2021, 2022 shedaniel + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ + +package me.shedaniel.rei.impl.common.logging; + +import com.google.common.collect.ImmutableList; +import dev.architectury.platform.Platform; +import me.shedaniel.rei.impl.Internals; +import me.shedaniel.rei.impl.common.InternalLogger; +import org.apache.logging.log4j.Level; +import org.apache.logging.log4j.LogManager; + +public class LoggerInitializer { + private static final InternalLogger LOGGER = new TransformingLogger(new MultiLogger(ImmutableList.of( + new FileLogger(Platform.getGameFolder().resolve("logs/rei.log")), + new FilteringLogger(new FileLogger(Platform.getGameFolder().resolve("logs/rei-issues.log")), Level.WARN), + new Log4JLogger(LogManager.getFormatterLogger("REI")) + )), message -> "[REI] " + message); + + public static void onInitialize() { + Internals.attachInstanceSupplier(LOGGER, "logger"); + } +} diff --git a/runtime-engine/logging/src/main/java/me/shedaniel/rei/impl/common/logging/MultiLogger.java b/runtime-engine/logging/src/main/java/me/shedaniel/rei/impl/common/logging/MultiLogger.java new file mode 100644 index 000000000..92a11e3bc --- /dev/null +++ b/runtime-engine/logging/src/main/java/me/shedaniel/rei/impl/common/logging/MultiLogger.java @@ -0,0 +1,58 @@ +/* + * This file is licensed under the MIT License, part of Roughly Enough Items. + * Copyright (c) 2018, 2019, 2020, 2021, 2022 shedaniel + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ + +package me.shedaniel.rei.impl.common.logging; + +import me.shedaniel.rei.impl.common.InternalLogger; +import org.apache.logging.log4j.Level; +import org.jetbrains.annotations.ApiStatus; + +@ApiStatus.Internal +public class MultiLogger implements InternalLogger { + private final Iterable<InternalLogger> loggers; + + public MultiLogger(Iterable<InternalLogger> loggers) { + this.loggers = loggers; + } + + @Override + public void throwException(Throwable throwable) { + for (InternalLogger logger : loggers) { + logger.throwException(throwable); + } + } + + @Override + public void log(Level level, String message) { + for (InternalLogger logger : loggers) { + logger.log(level, message); + } + } + + @Override + public void log(Level level, String message, Throwable throwable) { + for (InternalLogger logger : loggers) { + logger.log(level, message, throwable); + } + } +} diff --git a/runtime-engine/logging/src/main/java/me/shedaniel/rei/impl/common/logging/TransformingLogger.java b/runtime-engine/logging/src/main/java/me/shedaniel/rei/impl/common/logging/TransformingLogger.java new file mode 100644 index 000000000..0540d8baa --- /dev/null +++ b/runtime-engine/logging/src/main/java/me/shedaniel/rei/impl/common/logging/TransformingLogger.java @@ -0,0 +1,56 @@ +/* + * This file is licensed under the MIT License, part of Roughly Enough Items. + * Copyright (c) 2018, 2019, 2020, 2021, 2022 shedaniel + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ + +package me.shedaniel.rei.impl.common.logging; + +import me.shedaniel.rei.impl.common.InternalLogger; +import org.apache.logging.log4j.Level; +import org.jetbrains.annotations.ApiStatus; + +import java.util.function.UnaryOperator; + +@ApiStatus.Internal +public class TransformingLogger implements InternalLogger { + private final InternalLogger logger; + private final UnaryOperator<String> transformer; + + public TransformingLogger(InternalLogger logger, UnaryOperator<String> transformer) { + this.logger = logger; + this.transformer = transformer; + } + + @Override + public void throwException(Throwable throwable) { + logger.throwException(throwable); + } + + @Override + public void log(Level level, String message) { + logger.log(level, transformer.apply(message)); + } + + @Override + public void log(Level level, String message, Throwable throwable) { + logger.log(level, transformer.apply(message), throwable); + } +} |
