[slf4j-user] Upgrading from commons logging using SLF4J Migrator

Ceki Gulcu ceki at qos.ch
Fri Sep 23 16:52:35 CEST 2011


Hi  Lewis,

The problem has to do with how LogUtil is implemented.
Here are two possible solutions.

1) Get rid of the LogUtil class. For example, in HttpBase, instead of

  } catch (Throwable e) {
    e.printStackTrace(LogUtil.getErrorStream(logger));
    return new ProtocolOutput(null, new ProtocolStatus(e));
  }

  write instead

  } catch (Throwable e) {
    logger.error("Failed to get protocol output", e);
    return new ProtocolOutput(null, new ProtocolStatus(e));
  }

This latter form is the standard way of logging exceptions.
You would need to repeat this operation in all places where LogUtil is 
used. There are about 60 such places located in about a dozen classes.

2) In LogUtil, replace

static {
   try {
    TRACE=Logger.class.getMethod("trace", new Class[] { Object.class });
    DEBUG=Logger.class.getMethod("debug", new Class[] { Object.class });
    INFO=Logger.class.getMethod("info",  new Class[] { Object.class });
   } catch ...
   }
}

  with

static {
   try {
    TRACE=Logger.class.getMethod("trace", new Class[] {String.class});
    DEBUG=Logger.class.getMethod("debug", new Class[] {String.class});
    INFO=Logger.class.getMethod("info",  new Class[] {String.class});
   } catch ...
   }
}

On line 104 change
   method.invoke(logger, new Object[] {toString().trim() });
to
   method.invoke(logger, new String[] {toString().trim() });


Unless there is a good reason not to, I would get rid of LogUtil in 
Nutch. It seems like a very convoluted way for logging exceptions. But 
maybe I am missing the bigger picture...

HTH,
--
Ceki

On 23.09.2011 14:49, lewis john mcgibbney wrote:
> Hi list,
>
> I'm working on the task of upgrading our Apache Nutch codebase to use
> SLF4J with Log4J backend, therefore using the migrator. Testing in a
> development environment all JUnit tests, build and compile ant tasks
> pass successfully, however when moving into production (when exceptions
> are caught) we are having some problems. The full JIRA issue can be seen
> here[1].
>
> Namely the following class [2] at around this code snippet
> {code}
>
>      } catch (Throwable e) {
>        e.printStackTrace(LogUtil.getErrorStream(logger));
>        return new ProtocolOutput(null, new ProtocolStatus(e));
>      }
>    }
>
>    /* -------------------------- *
>     *</implementation:Protocol>  *
>     * -------------------------- */
>
> {code}
>
> To be honest, I'm pretty stumped as I've read all of the migrator
> documentation and not surprisingly as far as I know, none seems to be
> specific enough to address this issue. Does anyone have any suggestions
> as to how this code can be manually edited to prevent the various errors
> occuring as shown below.
>
> 2011-09-22 19:36:02,046 ERROR org.apache.nutch.util.LogUtil: Cannot log with method [null]
> java.lang.NullPointerException
> 	at org.apache.nutch.util.LogUtil$1.flush(LogUtil.java:103)
> 	at java.io.PrintStream.write(PrintStream.java:432)
> 	at sun.nio.cs.StreamEncoder.writeBytes(StreamEncoder.java:202)
> 	at sun.nio.cs.StreamEncoder.implFlushBuffer(StreamEncoder.java:272)
> 	at sun.nio.cs.StreamEncoder.flushBuffer(StreamEncoder.java:85)
> 	at java.io.OutputStreamWriter.flushBuffer(OutputStreamWriter.java:168)
> 	at java.io.PrintStream.newLine(PrintStream.java:496)
> 	at java.io.PrintStream.println(PrintStream.java:774)
> 	at java.lang.Throwable.printStackTrace(Throwable.java:461)
> 	at org.apache.nutch.protocol.http.api.HttpBase.getProtocolOutput(HttpBase.java:197)<<<<<<<  HERE
> 	at org.apache.nutch.fetcher.Fetcher$FetcherThread.run(Fetcher.java:665)
>
> 2011-09-22 19:44:26,929 ERROR org.apache.nutch.util.LogUtil: Cannot init log methods
> java.lang.NoSuchMethodException: org.slf4j.Logger.trace(java.lang.Object)
> 	at java.lang.Class.getMethod(Class.java:1605)
> 	at org.apache.nutch.util.LogUtil.<clinit>(LogUtil.java:48)
> 	at org.apache.nutch.protocol.http.api.HttpBase.getProtocolOutput(HttpBase.java:197)<<<<<<<  HERE
> 	at org.apache.nutch.fetcher.Fetcher$FetcherThread.run(Fetcher.java:665)
>
>
> Thank you kindly in advance for any suggestions.
>
> Lewis
>
> [1] https://issues.apache.org/jira/browse/NUTCH-1078
> [2]
> https://svn.apache.org/repos/asf/nutch/branches/branch-1.4/src/plugin/lib-http/src/java/org/apache/nutch/protocol/http/api/HttpBase.java
> [3] http://www.slf4j.org/migrator.html
> --
> /Lewis/
>
>


More information about the slf4j-user mailing list