Browse Source

Added OhmLogger

master
Gerhard Switalski 6 years ago
parent
commit
502b68915d

+ 62
- 0
src/chatprogramm/logger/OhmLogger.java View File

@@ -0,0 +1,62 @@
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/

package chatprogramm.logger;

import java.io.File;
import java.io.IOException;
import java.util.logging.ConsoleHandler;
import java.util.logging.FileHandler;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
*
* @author nobody
*/
public class OhmLogger
{
private static Logger instance;
private OhmLogger()
{
}
public static Logger getLogger()
{
if (instance == null)
{
instance = Logger.getLogger("OhmLogger");
initLogger();
}
return instance;
}
private static void initLogger()
{
instance.setUseParentHandlers(false);
try
{
String datei = System.getProperty("java.io.tmpdir") + File.separator + "log.csv";
FileHandler fh = new FileHandler(datei);
ConsoleHandler ch = new ConsoleHandler();
instance.addHandler(fh);
ch.setFormatter(new OhmLoggerFormatter());
instance.addHandler(ch);
instance.setLevel(Level.ALL);
}
catch(IOException ioex)
{
System.err.println(ioex);
}
}

}

+ 56
- 0
src/chatprogramm/logger/OhmLoggerFormatter.java View File

@@ -0,0 +1,56 @@
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/

package chatprogramm.logger;

import java.text.SimpleDateFormat;
import java.util.GregorianCalendar;
import java.util.Locale;
import java.util.TimeZone;
import java.util.logging.LogRecord;
import java.util.logging.SimpleFormatter;

/**
*
* @author nobody
*/
public class OhmLoggerFormatter extends SimpleFormatter
{
public OhmLoggerFormatter()
{
super();
}
@Override
public String format(LogRecord record)
{
String myLog;
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss,SSS", Locale.GERMANY);
GregorianCalendar calendar = new GregorianCalendar(TimeZone.getTimeZone("Europe/Berlin"));
calendar.setTimeInMillis(record.getMillis());
myLog = sdf.format(calendar.getTime());
myLog = addSemikolon(myLog);
myLog += record.getLevel().toString();
myLog = addSemikolon(myLog);
myLog += record.getSourceClassName();
myLog = addSemikolon(myLog);
myLog += record.getMessage();
myLog = addSemikolon(myLog);
myLog += "\n";
return myLog;
}
private String addSemikolon(String myString)
{
return myString += ("; ");
}
}

Loading…
Cancel
Save