Browse Source

Logger

TranslateOption
Jens Schuhmann 1 year ago
parent
commit
3062eb8e20

+ 5
- 1
src/aufgabe8_grafik2d/Zeiger.java View File



package aufgabe8_grafik2d; package aufgabe8_grafik2d;


import aufgabe8_grafik2d.util.OhmLogger;
import java.awt.BasicStroke; import java.awt.BasicStroke;
import java.awt.Color; import java.awt.Color;
import java.awt.Graphics; import java.awt.Graphics;
import java.util.concurrent.ExecutorService; import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors; import java.util.concurrent.Executors;
import java.util.concurrent.Future; import java.util.concurrent.Future;
import java.util.logging.Logger;
import javax.swing.JComponent; import javax.swing.JComponent;




*/ */
public class Zeiger extends JComponent implements Runnable public class Zeiger extends JComponent implements Runnable
{ {
private static Logger lg = OhmLogger.getLogger();
private static final float DICKE = 4f; private static final float DICKE = 4f;
private Line2D.Float linie; private Line2D.Float linie;
private BasicStroke stift; private BasicStroke stift;
} }
catch (Exception ex) catch (Exception ex)
{ {
//lg.severe(ex.toString());
lg.severe(ex.toString());
} }
} }
} }


public void start() public void start()
{ {
lg.info("Zeiger "+ this.zeigerlaenge+ " startet");
if (task == null) if (task == null)
{ {
task = eService.submit(this); task = eService.submit(this);

+ 111
- 0
src/aufgabe8_grafik2d/util/OhmLogger.java View File

package aufgabe8_grafik2d.util;

/*
* Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
* Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template
*/



import java.io.IOException;
import java.io.InputStream;
import java.time.LocalDateTime;
import java.util.Map;
import java.util.Properties;
import java.util.Set;
//import java.util.Properties;
import java.util.logging.*;

/**
* Klasse zur eigens konfigurierten Log-Ausgabe in der Console
* @author ahrens
*/
public class OhmLogger
{
private static final String LOGGER_NAME = "OhmLogger";
private static Logger lg = null;
/**
* privater Konstrukter -> Singleton
*/
private OhmLogger()
{
}
/**
* Public Method zum Erstellen des Loggers und zum Aufruf der Methode
* zum Initialisieren
* @return Referenz auf Logger
*/
public static Logger getLogger()
{
if (lg == null)
{
lg = Logger.getLogger(LOGGER_NAME);
initLogger();
}
return lg;
}
/**
* Methode zum Initialisieren, Suchen der Konfigurationsdatei, Anlegen eines
* neuen Console Handlers, Löschen der bisherigen Standard Handler und
* Einfügen des eigenen Handler, Setzen des Levels, je nach Properties
*/
private static void initLogger()
{
try (InputStream configFile = OhmLogger.class.getClassLoader().getResourceAsStream("config.properties")) {
if (configFile != null) {
Properties properties = new Properties();
properties.load(configFile);
java.util.logging.Level classLogLevel = java.util.logging.Level.parse(properties.getProperty("log.level"));
ConsoleHandler ch = new ConsoleHandler();
ch.setFormatter(new OhmFormatter());
lg.setUseParentHandlers(false);
lg.getHandlers();
lg.addHandler(ch);


lg.setLevel(classLogLevel);
String logDirectory = properties.getProperty("log.directory");
String logFileName = properties.getProperty("log.filename");

FileHandler fileHandler = new FileHandler(logDirectory + "/" + logFileName, true);
fileHandler.setFormatter(new OhmFormatter());
lg.addHandler(fileHandler);
} else {
System.err.println("Unable to find config.properties file. OhmLogger will use default settings.");
}
} catch (IOException | SecurityException e) {
System.err.println("Error configuring OhmLogger: " + e.getMessage());
}
}
}

/**
* Klasse zum Setzen des Formates des Auszugebenden Log-Strings
* @author ahren
*/
class OhmFormatter extends Formatter
{
@Override
public String format(LogRecord record)
{
String logline = "| ";
LocalDateTime ldt = LocalDateTime.now();
logline += ldt.toString();
logline += " | " + record.getLevel();
logline += " | " + record.getSourceClassName();
logline += " | " + record.getMessage();
logline += " |\n";
return logline;
}
}

+ 8
- 0
src/config.properties View File

log.level=INFO

log.directory=../Aufgabe7_bandit
log.filename=ohmLog

# Beispiel f\u00fcr verschiedene Level f\u00fcr unterschiedliche Pakete
# log.level.bandit.Zahlengenerator=FINE
# log.level.bandit.Model.Wuerfel=WARNING

Loading…
Cancel
Save