Skip to content

Commit

Permalink
own logging framework
Browse files Browse the repository at this point in the history
  • Loading branch information
ca-stefan-cordes committed Jan 26, 2017
1 parent fe7f137 commit a7dfdd0
Show file tree
Hide file tree
Showing 9 changed files with 217 additions and 55 deletions.
41 changes: 2 additions & 39 deletions pom.xml
Expand Up @@ -3,21 +3,8 @@
<groupId>de.quaddy_services</groupId>
<artifactId>escape-from-intranet</artifactId>
<packaging>jar</packaging>
<version>1.3</version>
<version>1.4</version>
<url>https://github.com/quaddy-services/escape-from-intranet</url>
<dependencies>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<version>1.1.7</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
Expand All @@ -32,6 +19,7 @@
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<configuration>
<finalName>escape-from-intranet</finalName>
<archive>
<manifest>
<mainClass>de.quaddy_services.proxy.EscapeProxy</mainClass>
Expand Down Expand Up @@ -59,31 +47,6 @@
<downloadSources>true</downloadSources>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<executions>
<execution>
<goals>
<goal>attached</goal>
</goals>
<phase>package</phase>
<configuration>
<appendAssemblyId>false</appendAssemblyId>
<finalName>escape-from-intranet</finalName>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
<archive>
<manifest>
<mainClass>de.quaddy_services.proxy.EscapeProxy</mainClass>
</manifest>
</archive>
<pomPropertiesFile/>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
5 changes: 2 additions & 3 deletions src/main/java/de/quaddy_services/proxy/EscapeProxy.java
Expand Up @@ -27,10 +27,9 @@
import javax.swing.Timer;
import javax.swing.WindowConstants;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import de.quaddy_services.proxy.events.PortStatusListener;
import de.quaddy_services.proxy.logging.Logger;
import de.quaddy_services.proxy.logging.LoggerFactory;

/**
*
Expand Down
5 changes: 2 additions & 3 deletions src/main/java/de/quaddy_services/proxy/EscapeProxyConfig.java
Expand Up @@ -19,12 +19,11 @@
import javax.crypto.spec.SecretKeySpec;
import javax.swing.event.EventListenerList;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import de.quaddy_services.proxy.events.CheckPortListener;
import de.quaddy_services.proxy.events.LogEventListener;
import de.quaddy_services.proxy.events.PortStatusListener;
import de.quaddy_services.proxy.logging.Logger;
import de.quaddy_services.proxy.logging.LoggerFactory;

/**
*
Expand Down
Expand Up @@ -10,10 +10,9 @@
import java.util.HashSet;
import java.util.Set;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import de.quaddy_services.proxy.events.CheckPortListener;
import de.quaddy_services.proxy.logging.Logger;
import de.quaddy_services.proxy.logging.LoggerFactory;

/**
*
Expand Down Expand Up @@ -120,7 +119,7 @@ private void acceptConnections(ServerSocket aServerSocket) {
EscapeProxyWorkerSocket tempEscapeProxyWorkerSocket = new EscapeProxyWorkerSocket(config, tempSocket);
tempEscapeProxyWorkerSocket.start();
} else {
config.fireLogEvent(tempInetAddress+" not localhost, closing " + tempSocket);
config.fireLogEvent(tempInetAddress + " not localhost, closing " + tempSocket);
tempSocket.close();
}
} catch (IOException | RuntimeException e) {
Expand All @@ -132,6 +131,7 @@ private void acceptConnections(ServerSocket aServerSocket) {
}

private Set<String> isLocalhostCache = new HashSet<>();

/**
*
*/
Expand Down
Expand Up @@ -15,8 +15,8 @@
import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import de.quaddy_services.proxy.logging.Logger;
import de.quaddy_services.proxy.logging.LoggerFactory;

/**
*
Expand Down
Expand Up @@ -2,23 +2,22 @@

import java.lang.Thread.UncaughtExceptionHandler;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import de.quaddy_services.proxy.logging.Logger;
import de.quaddy_services.proxy.logging.LoggerFactory;

/**
*
*/
public class LoggingUncaughtExceptionHandler implements UncaughtExceptionHandler {


private static final Logger LOGGER = LoggerFactory.getLogger(LoggingUncaughtExceptionHandler.class);

/**
*
*/
@Override
public void uncaughtException(Thread aT, Throwable aE) {
LOGGER.error("Error in "+aT,aE);
LOGGER.error("Error in " + aT, aE);
}

}
43 changes: 43 additions & 0 deletions src/main/java/de/quaddy_services/proxy/logging/FileLogger.java
@@ -0,0 +1,43 @@
package de.quaddy_services.proxy.logging;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.time.LocalDateTime;

/**
*
*/
public class FileLogger {
private File file;

FileLogger(File aFile) {
file = aFile;
}

/**
*
*/
public synchronized void log(String aPrefix, String aLevel, String aMessage, Throwable aE) {
try {
FileWriter tempFileWriter = new FileWriter(file, true);
try {
String tempText = LocalDateTime.now().toString() + ":" + aLevel + ":" + aPrefix + ":" + aMessage;
System.out.println(tempText);
tempFileWriter.write(tempText);
if (aE != null) {
tempFileWriter.write(System.lineSeparator());
aE.printStackTrace(new PrintWriter(tempFileWriter));
aE.printStackTrace();
}
tempFileWriter.write(System.lineSeparator());
} finally {
tempFileWriter.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}

}
110 changes: 110 additions & 0 deletions src/main/java/de/quaddy_services/proxy/logging/Logger.java
@@ -0,0 +1,110 @@
package de.quaddy_services.proxy.logging;

/**
*
*/
public class Logger {

private String prefix;
private FileLogger fileLogger;
private Boolean debugEnabled;
private Boolean traceEnabled;

/**
*
*/
public Logger(String aPrefix, FileLogger aFile) {
prefix = aPrefix;
fileLogger = aFile;
}

/**
*
*/
public void info(String aMessage) {
info(aMessage, (Throwable) null);
}

/**
*
*/
public void error(String aMessage, Throwable aE) {
fileLogger.log(prefix, "error", aMessage, aE);
}

/**
*
*/
public void debug(String aMessage, Throwable aE) {
if (isDebugEnabled()) {
fileLogger.log(prefix, "debug", aMessage, aE);
}
}

/**
*
*/
public void debug(String aString, Object anObject) {
if (isDebugEnabled()) {
debug(aString.replaceFirst("\\{\\}", String.valueOf(anObject)));
}
}

/**
*
*/
public boolean isDebugEnabled() {
if (debugEnabled == null) {
String tempDefaultLevel = System.getProperty("defaultLogLevel");
debugEnabled = "debug".equalsIgnoreCase(tempDefaultLevel);
}
return debugEnabled;
}

public boolean isTraceEnabled() {
if (traceEnabled == null) {
String tempDefaultLevel = System.getProperty("defaultLogLevel");
traceEnabled = "trace".equalsIgnoreCase(tempDefaultLevel);
}
return traceEnabled;
}

/**
*
*/
public void debug(String aString) {
debug(aString, (Throwable) null);
}

/**
*
*/
public void info(String aMessage, Throwable aE) {
fileLogger.log(prefix, "info", aMessage, aE);

}

/**
*
*/
public void error(String aString) {
error(aString, (Throwable) null);
}

/**
*
*/
public void debug(String aString, Object aO1, Object aO2) {
debug(aString.replaceFirst("\\{\\}", String.valueOf(aO1)).replaceFirst("\\{\\}", String.valueOf(aO2)));
}

/**
*
*/
public void trace(String aString) {
if (isTraceEnabled()) {
fileLogger.log(prefix, "trace", aString, (Throwable) null);
}
}

}
49 changes: 49 additions & 0 deletions src/main/java/de/quaddy_services/proxy/logging/LoggerFactory.java
@@ -0,0 +1,49 @@
package de.quaddy_services.proxy.logging;

import java.io.File;

/**
*
*/
public class LoggerFactory {

private static LoggerFactory instance;
private FileLogger fileLogger;

/**
*
*/
public static Logger getLogger(Class<?> aClass) {
return getInstance().getLoggerFor(aClass);
}

/**
*
*/
private Logger getLoggerFor(Class<?> aClass) {
String aName = aClass.getName();

return new Logger(aName.substring(aName.lastIndexOf(".") + 1), fileLogger);
}

/**
*
*/
private static synchronized LoggerFactory getInstance() {
if (instance == null) {
File temp3 = new File(System.getProperty("java.io.tmpdir") + "/escape-from-intranet-3.log");
temp3.delete();
File temp2 = new File(System.getProperty("java.io.tmpdir") + "/escape-from-intranet-2.log");
temp2.renameTo(temp3);
File temp1 = new File(System.getProperty("java.io.tmpdir") + "/escape-from-intranet-1.log");
temp1.renameTo(temp2);
File temp0 = new File(System.getProperty("java.io.tmpdir") + "/escape-from-intranet.log");
temp0.renameTo(temp1);

instance = new LoggerFactory();
instance.fileLogger = new FileLogger(temp0);
}
return instance;
}

}

0 comments on commit a7dfdd0

Please sign in to comment.