Skip to content

Commit

Permalink
Upgrading to Apache log4j 2.11 - using parts of PR deegree#916 by aut…
Browse files Browse the repository at this point in the history
…hor hwbllmnn
  • Loading branch information
tfr42 committed Mar 28, 2019
1 parent b79fff4 commit 2058cc6
Show file tree
Hide file tree
Showing 12 changed files with 94 additions and 745 deletions.
18 changes: 5 additions & 13 deletions deegree-core/deegree-core-annotations/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -46,24 +46,16 @@

<dependencies>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-api</artifactId>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>jcl-over-slf4j</artifactId>
</dependency>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<artifactId>slf4j-api</artifactId>
</dependency>
</dependencies>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@
public @interface PackageLoggingNotes {

/**
* @return the string that can be used as title string delimiting a log4j.properties section
* @return the string that can be used as title string delimiting a log4j2.xml section
*/
String title() default "";

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,8 @@

import static org.slf4j.LoggerFactory.getLogger;

import org.apache.log4j.spi.Filter;
import org.apache.log4j.spi.LoggingEvent;
import org.apache.logging.log4j.core.LogEvent;
import org.apache.logging.log4j.core.filter.AbstractFilter;
import org.slf4j.Logger;

/**
Expand All @@ -48,7 +48,7 @@
*
* @version $Revision: $, $Date: $
*/
public class DuplicateMessageFilter extends Filter {
public class DuplicateMessageFilter extends AbstractFilter {

private static final Logger LOG = getLogger( DuplicateMessageFilter.class );

Expand All @@ -57,26 +57,26 @@ public class DuplicateMessageFilter extends Filter {
private int count;

@Override
public int decide( LoggingEvent event ) {
public Result filter( LogEvent event ) {
if ( last == null ) {
last = event.getRenderedMessage();
last = event.getMessage().getFormattedMessage();
count = 0;
return ACCEPT;
return Result.ACCEPT;
}

if ( last.equals( event.getRenderedMessage() ) ) {
if ( last.equals( event.getMessage().getFormattedMessage() ) ) {
++count;
// would be cool to log (... repeated 12452 times)
if ( count % 100 == 0 ) {
LOG.warn( "Last message repeated 100 times." );
}
return DENY;
return Result.DENY;
}

last = event.getRenderedMessage();
last = event.getMessage().getFormattedMessage();
count = 1;

return ACCEPT;
return Result.ACCEPT;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@

@SupportedAnnotationTypes(value = { "org.deegree.commons.annotations.PackageLoggingNotes",
"org.deegree.commons.annotations.LoggingNotes" })
@SupportedSourceVersion(SourceVersion.RELEASE_6)
@SupportedSourceVersion(SourceVersion.RELEASE_8)
@SupportedOptions({ "log4j.outputdir" })
public class LoggingAnnotationProcessor extends AbstractProcessor {

Expand All @@ -100,35 +100,35 @@ public void init( ProcessingEnvironment env ) {
}

// breaks the lines at max width
String format( String str ) {
private String format( String str ) {
StringBuilder res = new StringBuilder();
outer: while ( str.length() > ( width - 3 ) ) {
int len = 3;
res.append( "## " );
res.append( "<!-- " );
while ( len < width && str.length() > 0 ) {
int idx = str.indexOf( " " );
if ( idx == -1 ) {
if ( len > 3 ) {
res.append( "\n## " );
res.append( "\n<!-- " );
}
res.append( str );
str = "";
} else {
if ( len + idx + 1 > width ) {
res.append( "\n" );
res.append( " -->\n" );
continue outer;
}
res.append( str.substring( 0, idx + 1 ) );
str = str.substring( idx + 1 );
res.append(str.substring(0, idx + 1));
str = str.substring(0, idx + 1);
len += idx + 1;
}
}
if ( !str.isEmpty() ) {
res.append( "\n" );
res.append( " -->\n" );
}
}
if ( !str.isEmpty() ) {
res.append( "## " + str );
res.append( "<!-- " ).append( str ).append( " -->\n" );
}
return res.toString();
}
Expand All @@ -148,30 +148,33 @@ private void find( Element e, Tree root ) {
}
}

void block( String text, RollbackPrintWriter out, boolean big ) {
private void block( String text, RollbackPrintWriter out, boolean big ) {
if ( big ) {
out.print( "# " );
out.print( "<!-- " );
for ( int i = 0; i < width - 2; ++i ) {
out.print( "=" );
}
out.print(" -->");
out.println();
}
int odd = text.length() % 2;
int len = ( width - text.length() - 4 ) / 2;
out.print( "# " );
out.print( "<!-- " );
for ( int i = 0; i < len; ++i ) {
out.print( "=" );
}
out.print( " " + text + " " );
for ( int i = 0; i < len + odd; ++i ) {
out.print( "=" );
}
out.print(" -->");
out.println();
if ( big ) {
out.print( "# " );
out.print( "<!-- " );
for ( int i = 0; i < width - 2; ++i ) {
out.print( "=" );
}
out.print(" -->");
out.println();
}
out.println();
Expand Down Expand Up @@ -212,9 +215,7 @@ public boolean process( Set<? extends TypeElement> annotations, RoundEnvironment
tree.print( out, "", false, false, false, false, true );
out.close();
return true;
} catch ( UnsupportedEncodingException e ) {
e.printStackTrace();
} catch ( FileNotFoundException e ) {
} catch ( UnsupportedEncodingException | FileNotFoundException e ) {
e.printStackTrace();
}
return false;
Expand All @@ -225,17 +226,16 @@ class Tree {

LoggingNotes notes;

TreeMap<String, Tree> children = new TreeMap<String, Tree>();
TreeMap<String, Tree> children = new TreeMap<>();

void insert( String qname, PackageLoggingNotes pnotes, LoggingNotes notes ) {
LinkedList<String> pkgs = new LinkedList<String>( Arrays.asList( qname.split( "\\." ) ) );
LinkedList<String> pkgs = new LinkedList<>( Arrays.asList( qname.split( "\\." ) ) );
Tree node = this;
while ( true ) {
String next = pkgs.poll();
Tree nextNode = node.children.get( next );
if ( nextNode == null ) {
nextNode = new Tree();
// nextNode.segment = next;
node.children.put( next, nextNode );
}
node = nextNode;
Expand All @@ -257,41 +257,45 @@ void print( RollbackPrintWriter out, String qname, boolean error, boolean warn,
}
for ( Entry<String, Tree> entry : children.entrySet() ) {
entry.getValue().print( out, qname + ( qname.isEmpty() ? "" : "." ) + entry.getKey(), error, warn,
info, debug, trace );
info, debug, trace );
}
out.rollback();
}

private void handleNotes( RollbackPrintWriter out, String qname, boolean error, boolean warn, boolean info,
boolean debug, boolean trace ) {
handleNote( out, error, notes.error(), qname, "ERROR" );
handleNote( out, warn, notes.warn(), qname, "WARN" );
handleNote( out, info, notes.info(), qname, "INFO" );
handleNote( out, debug, notes.debug(), qname, "DEBUG" );
handleNote( out, trace, notes.trace(), qname, "TRACE" );
handleNote( out, error, notes.error(), qname, "error" );
handleNote( out, warn, notes.warn(), qname, "warn" );
handleNote( out, info, notes.info(), qname, "info" );
handleNote( out, debug, notes.debug(), qname, "debug" );
handleNote( out, trace, notes.trace(), qname, "trace" );
}

private void handlePackageNotes( RollbackPrintWriter out, String qname, boolean error, boolean warn,
boolean info, boolean debug, boolean trace ) {
String title = pnotes.title();

boolean isSubsystem = qname.replaceAll( "[^\\.]", "" ).length() == 2;
boolean isSubsystem = qname.replaceAll( "[^.]", "" ).length() == 2;

if ( !title.isEmpty() ) {
block( title, out, isSubsystem );
}

handleNote( out, error, pnotes.error(), qname, "ERROR" );
handleNote( out, warn, pnotes.warn(), qname, "WARN" );
handleNote( out, info, pnotes.info(), qname, "INFO" );
handleNote( out, debug, pnotes.debug(), qname, "DEBUG" );
handleNote( out, trace, pnotes.trace(), qname, "TRACE" );
handleNote( out, error, pnotes.error(), qname, "error" );
handleNote( out, warn, pnotes.warn(), qname, "warn" );
handleNote( out, info, pnotes.info(), qname, "info" );
handleNote( out, debug, pnotes.debug(), qname, "debug" );
handleNote( out, trace, pnotes.trace(), qname, "trace" );
}

private void handleNote( RollbackPrintWriter out, boolean level, String note, String qname, String levelName ) {
if ( !note.isEmpty() && level ) {
out.println( format( note ) );
out.println( "#log4j.logger." + qname + " = " + levelName );
out.println("<!--");
out.println(" <Logger name=\"" + qname + "\" level=\"" + levelName + "\">");
out.println(" <AppenderRef ref=\"Console\"/>");
out.println(" </Logger>");
out.println("-->");
out.println();
out.flush();
}
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<Configuration status="ERROR">
<!-- by default, only log to stdout -->
<Appenders>
<Console name="Console" target="SYSTEM_OUT">
<PatternLayout
pattern="[%d{HH:mm:ss}] %5p: [%c{1}] %m%n" />
</Console>
</Appenders>
<Loggers>
<!-- The log level for all classes that are not configured below. -->
<Logger name="org.deegree" level="info" additivity="false">
<AppenderRef ref="Console" />
</Logger>
</Loggers>
</Configuration>
8 changes: 8 additions & 0 deletions deegree-core/deegree-core-commons/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,14 @@
<groupId>javax.mail</groupId>
<artifactId>mail</artifactId>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>jcl-over-slf4j</artifactId>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
Expand Down
4 changes: 4 additions & 0 deletions deegree-core/deegree-core-workspace/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,10 @@
<artifactId>deegree-core-annotations</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
</dependency>
<dependency>
<groupId>org.reflections</groupId>
<artifactId>reflections</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,6 @@
import org.apache.commons.fileupload.servlet.ServletFileUpload;
import org.apache.commons.io.IOUtils;
import org.apache.commons.logging.LogFactory;
import org.apache.log4j.LogManager;
import org.deegree.commons.annotations.LoggingNotes;
import org.deegree.commons.concurrent.Executor;
import org.deegree.commons.config.DeegreeWorkspace;
Expand Down Expand Up @@ -1401,7 +1400,6 @@ private void plugClassLoaderLeaks() {
Executor.getInstance().shutdown();

LogFactory.releaseAll();
LogManager.shutdown();

// image io
Iterator<Class<?>> i = IIORegistry.getDefaultInstance().getCategories();
Expand Down
1 change: 1 addition & 0 deletions deegree-services/deegree-webservices/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
<executions>
<execution>
<goals>
<goal>assemble-log4j</goal>
<goal>generate-buildinfo</goal>
</goals>
</execution>
Expand Down
Loading

0 comments on commit 2058cc6

Please sign in to comment.