In a previous question, Java logger (of doom!), I had posted my own logger, written in Java. I have since taken the answers from that question and a few of my own things to update it.
New code:
/*
* 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;
protected DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy/MM/dd HH:mm:ss"); // example: 2021/27/5 12:07:23
public void init(File fileToLog) {
try {
logfile = fileToLog;
if (fileToLog.exists()) {
clearFile(fileToLog);
} else {
if(!fileToLog.createNewFile()) {
System.out.println("An unrecognized error occurred while creating the logfile.");
}
}
} 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, true);
osw = new OutputStreamWriter(fos, StandardCharsets.UTF_8);
clearFile(fileToLog);
} 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(Object o, int logType) throws IOException {
// logType should be 0 = INFO, 1 = WARN, 2 = ERROR, 3 = FATAL
osw.write(getTime() + ": ");
printErrorType(logType);
osw.write(o.toString() + '\n');
}
public void log(int i, int logType) throws IOException {
osw.write(getTime() + ": ");
printErrorType(logType);
osw.write(String.valueOf(i) + '\n');
}
private String getTime() {
LocalDateTime ldtNow = LocalDateTime.now();
return ldtNow.format(dtf);
}
private void clearFile(File file) throws IOException {
FileOutputStream fosClearer = new FileOutputStream(file, false);
OutputStreamWriter oswClearer = new OutputStreamWriter(fosClearer);
oswClearer.write("");
oswClearer.close();
}
private void printErrorType(int errorType) throws IOException {
switch (errorType) {
case 0:
osw.write("INFO: ");
break;
case 1:
osw.write("WARN: ");
break;
case 2:
osw.write("ERROR: ");
break;
case 3:
osw.write("FATAL: ");
break;
default:
throw new IllegalArgumentException("Parameter 'logType " +
"of 'personal.williamnelson.Logger.log'" +
" must be one of the following: \n" +
" 0: INFO\n" +
" 1: WARN\n" +
" 2: ERROR\n" +
" 3: FATAL\n");
}
}
public void close() throws IOException {
osw.close();
}
}
```