I wrote my own logger, but I dislike how many times I overloaded the log() function. If anyone knows of another way of doing that, feel free to share.
/*
* Syml, a free and open source programming language.
* Copyright (C) 2021 William Nelson
* mailto: catboardbeta AT gmail DOT com
* Syml is free software, licensed under MIT license. See LICENSE
* for more information.
*
* Syml is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
*/
package personal.williamnelson;
import java.io.Closeable;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.nio.charset.StandardCharsets;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
public class Logger implements Closeable {
public File logfile = new File("");
protected FileOutputStream fos;
protected OutputStreamWriter osw;
public void init(File fileToLog) {
try {
logfile = fileToLog;
if (fileToLog.exists()) {
clearFile(fileToLog);
} else {
fileToLog.createNewFile();
}
} catch (IOException e) { //NOSONAR
System.out.println("An error occurred while creating a logfile. Are you sure that '" +
logfile.toString() +
"' is a valid filename?");
System.exit(1);
}
try {
fos = new FileOutputStream(fileToLog, false);
osw = new OutputStreamWriter(fos, StandardCharsets.UTF_8);
clearFile(fileToLog);x
} catch (IOException e) {
System.out.println("An unrecognized error occurred while start logging stream and " +
"writing to log stream, despite passing all tests.");
}
}
public void log(String s, boolean n) throws IOException {
osw.write(getTime() + '\n' + s + (n ? '\n' : ""));
}
public void log(char c, boolean n) throws IOException {
osw.write(getTime() + '\n' + c + (n ? '\n' : ""));
}
public void log(File f, boolean n) throws IOException {
osw.write(getTime() + '\n' + f.getAbsolutePath() + (n ? '\n' : ""));
}
public void log(StringBuilder sb, boolean n) throws IOException {
osw.write(getTime() + '\n' + sb.toString() + (n ? '\n' : ""));
}
public void log(int i, boolean n) throws IOException {
osw.write(getTime() + '\n' + i + (n ? '\n' : ""));
}
public void log(Integer i, boolean n) throws IOException {
osw.write(getTime() + '\n' + i + (n ? '\n' : ""));
}
public void log(float f, boolean n) throws IOException {
osw.write(getTime() + '\n' + f + (n ? '\n' : ""));
}
public void log(double d, boolean n) throws IOException {
osw.write(getTime() + '\n' + d + (n ? '\n' : ""));
}
public void log(long l, boolean n) throws IOException {
osw.write(getTime() + '\n' + l + (n ? '\n' : ""));
}
public void log(short s, boolean n) throws IOException {
osw.write(getTime() + '\n' + s + (n ? '\n' : ""));
}
public void log(boolean b, boolean n) throws IOException {
osw.write(getTime() + '\n' + (b ? "true" : "false") + (n ? '\n' : ""));
}
public void log(byte b, boolean n) throws IOException {
osw.write(getTime() + '\n' + b + (n ? '\n' : ""));
}
private String getTime() {
DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy/MM/dd HH:mm:ss"); // example: 2021/27/5 12:07:23
LocalDateTime ldtNow = LocalDateTime.now();
return ldtNow.format(dtf);
}
private void clearFile(File file) throws IOException {
OutputStreamWriter oswClearer = new OutputStreamWriter(fos);
oswClearer.write("");
oswClearer.close();
}
public void close() throws IOException {
osw.close();
fos.close();
}
}