Replace logging with Log4j

Used to be logged with `System.out.println`
This commit is contained in:
AxolotlMaid 2024-05-28 23:59:12 +01:00
parent d43be9b51e
commit 2056f44b43
2 changed files with 20 additions and 22 deletions

View file

@ -1,5 +1,8 @@
package com.axolotlmaid.optionsprofiles; package com.axolotlmaid.optionsprofiles;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import java.io.IOException; import java.io.IOException;
import java.nio.file.Files; import java.nio.file.Files;
import java.nio.file.Path; import java.nio.file.Path;
@ -7,6 +10,7 @@ import java.nio.file.Paths;
public class OptionsProfilesMod { public class OptionsProfilesMod {
public static final String MOD_ID = "optionsprofiles"; public static final String MOD_ID = "optionsprofiles";
public static final Logger LOGGER = LogManager.getLogger("Options Profiles");
public static void init() { public static void init() {
Path profilesDirectory = Paths.get("options-profiles"); Path profilesDirectory = Paths.get("options-profiles");
@ -15,8 +19,7 @@ public class OptionsProfilesMod {
try { try {
Files.createDirectory(profilesDirectory); Files.createDirectory(profilesDirectory);
} catch (IOException e) { } catch (IOException e) {
System.out.println("An error occurred when creating the 'options-profiles' directory."); LOGGER.error("An error occurred when creating the 'options-profiles' directory.", e);
e.printStackTrace();
} }
} }
} }

View file

@ -1,5 +1,7 @@
package com.axolotlmaid.optionsprofiles.profiles; package com.axolotlmaid.optionsprofiles.profiles;
import com.axolotlmaid.optionsprofiles.OptionsProfilesMod;
import java.io.File; import java.io.File;
import java.io.IOException; import java.io.IOException;
import java.nio.file.Files; import java.nio.file.Files;
@ -24,15 +26,14 @@ public class Profiles {
Files.createDirectory(profile); Files.createDirectory(profile);
if (Files.exists(profile)) { if (Files.exists(profile)) {
System.out.println("[Options Profiles] Profile created."); OptionsProfilesMod.LOGGER.info("Profile created");
writeOptionsFilesIntoProfile(profileName); writeOptionsFilesIntoProfile(profileName);
} else { } else {
System.out.println("[Options Profiles] Profile was not created successfully."); OptionsProfilesMod.LOGGER.warn("Profile was not created successfully");
} }
} catch (IOException e) { } catch (IOException e) {
System.out.println("[Options Profiles] An error occurred when creating a profile."); OptionsProfilesMod.LOGGER.error("An error occurred when creating a profile", e);
e.printStackTrace();
} }
} }
@ -57,13 +58,11 @@ public class Profiles {
Files.write(profileOptions, line.getBytes(), StandardOpenOption.CREATE, StandardOpenOption.APPEND); Files.write(profileOptions, line.getBytes(), StandardOpenOption.CREATE, StandardOpenOption.APPEND);
Files.write(profileOptions, "\n".getBytes(), StandardOpenOption.APPEND); Files.write(profileOptions, "\n".getBytes(), StandardOpenOption.APPEND);
} catch (IOException e) { } catch (IOException e) {
System.out.println("[Options Profiles] An error occurred when writing a profile."); OptionsProfilesMod.LOGGER.error("An error occurred when writing a profile", e);
e.printStackTrace();
} }
}); });
} catch (IOException e) { } catch (IOException e) {
System.out.println("[Options Profiles] An error occurred when reading an options file."); OptionsProfilesMod.LOGGER.error("An error occurred when reading an options file.", e);
e.printStackTrace();
} }
} }
@ -136,13 +135,11 @@ public class Profiles {
Files.write(options, line.getBytes(), StandardOpenOption.APPEND); Files.write(options, line.getBytes(), StandardOpenOption.APPEND);
Files.write(options, "\n".getBytes(), StandardOpenOption.APPEND); Files.write(options, "\n".getBytes(), StandardOpenOption.APPEND);
} catch (IOException e) { } catch (IOException e) {
System.out.println("[Options Profiles] An error occurred when loading a profile."); OptionsProfilesMod.LOGGER.error("An error occurred when loading a profile", e);
e.printStackTrace();
} }
}); });
} catch (IOException e) { } catch (IOException e) {
System.out.println("[Options Profiles] An error occurred when loading a profile."); OptionsProfilesMod.LOGGER.error("An error occurred when loading a profile", e);
e.printStackTrace();
} }
} }
@ -168,19 +165,18 @@ public class Profiles {
Path newProfile = Paths.get("options-profiles/" + newProfileName); Path newProfile = Paths.get("options-profiles/" + newProfileName);
if (Files.exists(newProfile)) if (Files.exists(newProfile))
System.out.println("[Options Profiles] New profile already exists!"); OptionsProfilesMod.LOGGER.warn("A profile with that name already exists!");
try { try {
Files.move(profile, newProfile); Files.move(profile, newProfile);
if (Files.exists(newProfile)) { if (Files.exists(newProfile)) {
System.out.println("[Options Profiles] Profile renamed."); OptionsProfilesMod.LOGGER.info("Profile renamed");
} else { } else {
System.out.println("[Options Profiles] Profile was not renamed successfully."); OptionsProfilesMod.LOGGER.warn("Profile was not renamed successfully!");
} }
} catch (IOException e) { } catch (IOException e) {
System.out.println("[Options Profiles] Profile was not renamed successfully."); OptionsProfilesMod.LOGGER.error("Profile not renamed successfully", e);
e.printStackTrace();
} }
} }
@ -193,10 +189,9 @@ public class Profiles {
.map(Path::toFile) .map(Path::toFile)
.forEach(File::delete); .forEach(File::delete);
} catch (IOException e) { } catch (IOException e) {
System.out.println("[Options Profiles] Profile was not deleted."); OptionsProfilesMod.LOGGER.error("Profile was not deleted", e);
e.printStackTrace();
} }
System.out.println("[Options Profiles] Profile deleted."); OptionsProfilesMod.LOGGER.info("Profile deleted");
} }
} }