java-lib-fw/src/main/java/de/synolo/lib/fw/utils/Logging.java

188 lines
4.2 KiB
Java

package de.synolo.lib.fw.utils;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.io.PrintStream;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
public class Logging {
public static final String DEFAULT_FORMAT = "[%date][%level] %msg\n";
public static final DateFormat DEFAULT_DATEFORMAT = new SimpleDateFormat("YYYY-MM-dd HH:mm:ss.SSS");
public static Logging out;
public static void init() {
out = new Logging(null);
}
public static void init(String lvl) {
LogLevel level = lvl == null ? null : LogLevel.valueOf(lvl.toUpperCase());
out = new Logging(level);
}
protected String format;
protected LogLevel level;
protected PrintStream filestream, defaultstream;
protected File file = null;
protected DateFormat dateformat;
protected ArrayList<String> messages = new ArrayList<String>();
protected Thread loggingThread;
protected Logging(LogLevel level) {
this.format = DEFAULT_FORMAT;
this.dateformat = DEFAULT_DATEFORMAT;
this.level = level == null ? LogLevel.INFO : level;
this.defaultstream = System.out;
System.setErr(new PrintStream(new LoggingOutputStream(LogLevel.ERROR)));
System.setOut(new PrintStream(new LoggingOutputStream(LogLevel.INFO)));
log("Initialized Logging with LogLevel: %s", this.level.toString());
}
private synchronized void writeToFile(String write) {
if (filestream != null) {
filestream.println(write);
filestream.flush();
}
defaultstream.print(write);
}
public void log(LogLevel level, String format, Object...args) {
if(level.val() > this.level.val()) {
return;
}
String dateStr = this.dateformat.format(new Date());
String levelStr = String.format("%-8s", level.toString());
String msgStr = String.format(format, args);
String formatted = String.format("[%s][%s] %s \n", dateStr, levelStr, msgStr);
writeToFile(formatted);
}
public void log(String format, Object...args) {
log(LogLevel.INFO, format, args);
}
public void log(String msg) {
log(msg, "");
}
public void err(String format, Object...args) {
log(LogLevel.ERROR, format, args);
}
public void err(String msg) {
err(msg, "");
}
public void err(Object obj) {
err(obj.toString());
}
public void warn(String format, Object...args) {
log(LogLevel.WARNING, format, args);
}
public void warn(String msg) {
warn(msg, "");
}
public void debug(String format, Object...args) {
log(LogLevel.DEBUG, format, args);
}
public void debug(String msg) {
debug(msg, "");
}
public void detail(String format, Object...args) {
log(LogLevel.TRACE, format, args);
}
public void detail(String msg) {
detail(msg, "");
}
public String getFormat() {
return format;
}
public void setFormat(String format) {
this.format = format;
}
public LogLevel getLevel() {
return level;
}
public void setLevel(LogLevel level) {
this.level = level;
}
public void setLevel(int level) {
this.level = LogLevel.forVal(level);
}
public File getFile() {
return file;
}
public void setFile(File file){
try {
if(!file.exists()) {
file.createNewFile();
}
this.filestream = new PrintStream(new FileOutputStream(file, true));
}catch(IOException e) {
e.printStackTrace();
}
this.file = file;
}
public DateFormat getDateformat() {
return dateformat;
}
public void setDateformat(DateFormat dateformat) {
this.dateformat = dateformat;
}
protected class LoggingOutputStream extends OutputStream {
byte[] buffer;
int bufferPosition;
LogLevel logLevel;
public LoggingOutputStream() {
buffer = new byte[4096];
bufferPosition = 0;
this.logLevel = LogLevel.INFO;
}
public LoggingOutputStream(LogLevel logLevel) {
buffer = new byte[4096];
bufferPosition = 0;
this.logLevel = logLevel;
}
@Override
public void flush(){
if (bufferPosition > 0){
String str = new String(buffer,0,bufferPosition);
log(this.logLevel, str, "");
bufferPosition = 0;
};
}
@Override
public void write(int b) throws IOException {
if (b == 0x0A){
flush();
} else {
buffer[bufferPosition++] = (byte)b;
if (bufferPosition == buffer.length){
flush();
}
};
}
}
}