Browse Source

Logger

TranslateOption
Jens Schuhmann 11 months ago
parent
commit
3062eb8e20
3 changed files with 124 additions and 1 deletions
  1. 5
    1
      src/aufgabe8_grafik2d/Zeiger.java
  2. 111
    0
      src/aufgabe8_grafik2d/util/OhmLogger.java
  3. 8
    0
      src/config.properties

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

@@ -5,6 +5,7 @@

package aufgabe8_grafik2d;

import aufgabe8_grafik2d.util.OhmLogger;
import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Graphics;
@@ -15,6 +16,7 @@ import static java.lang.Math.*;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import java.util.logging.Logger;
import javax.swing.JComponent;


@@ -24,6 +26,7 @@ import javax.swing.JComponent;
*/
public class Zeiger extends JComponent implements Runnable
{
private static Logger lg = OhmLogger.getLogger();
private static final float DICKE = 4f;
private Line2D.Float linie;
private BasicStroke stift;
@@ -72,13 +75,14 @@ public class Zeiger extends JComponent implements Runnable
}
catch (Exception ex)
{
//lg.severe(ex.toString());
lg.severe(ex.toString());
}
}
}

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

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

@@ -0,0 +1,111 @@
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

@@ -0,0 +1,8 @@
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