Permalink
Browse files

Source code Log4j Web Tracker

  • Loading branch information...
mrsarm committed Jun 16, 2012
1 parent 8ebc367 commit 0b3da7354d9b78b9830399c8d6d6a33b3d8613d5
View
@@ -4,3 +4,10 @@
*.jar
*.war
*.ear
# Eclipse Files #
.project
.classpath
.settings
target
log
View
90 pom.xml
@@ -0,0 +1,90 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>log4jwebtracker</groupId>
<artifactId>log4jwebtracker</artifactId>
<packaging>jar</packaging>
<version>1.0.0</version>
<name>log4jwebtracker</name>
<url>http://www.log4jwebtracker.com</url>
<prerequisites>
<maven>2.0.6</maven>
</prerequisites>
<dependencies>
<!-- Build time only dependencies -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<version>2.3</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.16</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId>
<version>1.0.4</version>
<scope>provided</scope>
</dependency>
</dependencies>
<build>
<defaultGoal>install</defaultGoal>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.4</source>
<target>1.4</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>2.2</version>
<configuration>
<archive>
<manifestEntries>
<url>${pom.url}</url>
<Specification-Version>${pom.version}</Specification-Version>
<Package>${pom.name}</Package>
</manifestEntries>
</archive>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-source-plugin</artifactId>
<version>2.1.2</version>
<configuration>
<archive>
<manifestEntries>
<url>${pom.url}</url>
<Specification-Version>${pom.version}</Specification-Version>
<Package>${pom.name}</Package>
</manifestEntries>
</archive>
</configuration>
<executions>
<execution>
<id>attach-sources</id>
<phase>verify</phase>
<goals>
<goal>jar-no-fork</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
<finalName>log4jwebtracker</finalName>
</build>
</project>
@@ -0,0 +1,98 @@
package log4jwebtracker.io;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.RandomAccessFile;
/**
* Stream I/O utils.
*
* @author Mariano Ruiz
*/
public abstract class StreamUtils {
static public void readStream(InputStream inputStream, OutputStream outputStream, int bufferSize)
throws IOException {
byte[] buf = new byte[bufferSize];
int bytesRead = inputStream.read(buf);
while (bytesRead != -1) {
outputStream.write(buf, 0, bytesRead);
bytesRead = inputStream.read(buf);
}
}
static public void readFile(RandomAccessFile inputFile, OutputStream outputStream, int bufferSize)
throws IOException {
byte[] buf = new byte[bufferSize];
int bytesRead = inputFile.read(buf);
while (bytesRead != -1) {
outputStream.write(buf, 0, bytesRead);
bytesRead = inputFile.read(buf);
}
}
static private void readLines(RandomAccessFile inputFile, OutputStream outputStream, long seek)
throws IOException {
inputFile.seek(seek);
String line = inputFile.readLine();
while (line != null) {
outputStream.write((line + "\n").getBytes());
line = inputFile.readLine();
}
}
static public void tailFile(RandomAccessFile inputFile, OutputStream outputStream,
int bufferSize, int numLines) throws IOException {
if(bufferSize<1) {
throw new IllegalArgumentException("bufferSize < 1");
}
if(numLines<1) {
throw new IllegalArgumentException("numLines < 1");
}
byte[] buf = new byte[bufferSize];
long fileSizeMark = inputFile.length();
long offset = fileSizeMark;
long seek;
int len;
if(offset-bufferSize>=0) {
seek = offset-bufferSize;
len = bufferSize;
} else {
seek = 0;
len = (int)offset;
}
inputFile.seek(seek);
int bytesRead = inputFile.read(buf, 0, len);
while(bytesRead != -1 && bytesRead != 0) {
int i=bytesRead-1;
while(i>=0 && numLines>0) {
if(buf[i]=='\n') { // End line & not EOF
if(offset==fileSizeMark && i==bytesRead-1) {
fileSizeMark=-1;
} else {
numLines--;
}
}
i--;
}
if(numLines==0) {
readLines(inputFile, outputStream, offset-bytesRead+i+2);
return;
}
offset -= bytesRead;
if(offset-bufferSize>=0) {
seek = offset-bufferSize;
len = bufferSize;
} else {
seek = 0;
len = (int)offset;
}
inputFile.seek(seek);
bytesRead = inputFile.read(buf, 0, len);
}
readLines(inputFile, outputStream, 0);
// input and output stream must be closed
}
}
@@ -0,0 +1,73 @@
package log4jwebtracker.logging;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Enumeration;
import java.util.LinkedList;
import java.util.List;
import org.apache.log4j.Appender;
import org.apache.log4j.FileAppender;
import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;
/**
* Logging utils methods.
*
* @author Mariano Ruiz
*/
public abstract class LoggingUtils {
static synchronized public List getFileAppenders() {
List list = new ArrayList();
Enumeration e = LogManager.getRootLogger().getAllAppenders();
while(e.hasMoreElements()) {
Appender a = (Appender) e.nextElement();
if(a instanceof FileAppender) {
list.add(a);
}
}
return list;
}
static synchronized public FileAppender getFileAppender(String appenderName) {
Enumeration e = LogManager.getRootLogger().getAllAppenders();
while(e.hasMoreElements()) {
Appender a = (Appender) e.nextElement();
if(a instanceof FileAppender && a.getName().equals(appenderName)) {
return (FileAppender) a;
}
}
return null;
}
static public boolean contains(List loggers, String loggerName) {
int i=0;
while(i<loggers.size()) {
if(((Logger)loggers.get(i)).getName().equals(loggerName)) {
return true;
}
i++;
}
return false;
}
static public List getLoggers() {
Enumeration e = LogManager.getCurrentLoggers();
List loggersList = new LinkedList();
while(e.hasMoreElements()) {
loggersList.add(e.nextElement());
}
Collections.sort(loggersList, new Comparator() {
public int compare(Object arg0, Object arg1) {
Logger log0 = (Logger) arg0;
Logger log1 = (Logger) arg1;
return log0.getName().compareTo(log1.getName());
}
});
loggersList.add(0, LogManager.getRootLogger());
return loggersList;
}
}
Oops, something went wrong.

0 comments on commit 0b3da73

Please sign in to comment.