Change logging level for Logger.throwing

S

Stefan Istrate

Is there any possibility to change the logging level used by
Logger.throwing from FINER to SEVERE?

Thanks,
Stefan
 
M

Mike Schilling

Stefan said:
Is there any possibility to change the logging level used by
Logger.throwing from FINER to SEVERE?

It would be quite straightforward for you to write a method that
create the same LogRecord that thrown() does except that the level is
set to SEVERE, and then log it.
 
A

Arne Vajhøj

Stefan said:
Is there any possibility to change the logging level used by
Logger.throwing from FINER to SEVERE?

Not really.

The FINER is hard coded in that method.

If you need a practical solution and are willing to
do what it takes to solve the problem, then you
can use AspectJ.

Just compile all your code together with the
following:

import java.lang.reflect.*;
import java.util.logging.*;

import org.aspectj.lang.*;
import org.aspectj.lang.annotation.*;

@Aspect
public class Hack {
@Pointcut("call(void
java.util.logging.Logger.throwing(String,String,Throwable))")
public void changeLevel() {}
@Around(value = "changeLevel()")
public void makeSevere(ProceedingJoinPoint pjp) {
Logger log = (Logger)pjp.getTarget();
Object[] args = pjp.getArgs();
LogRecord lr = new LogRecord(Level.SEVERE, "THROW");
lr.setSourceClassName((String)args[0]);
lr.setSourceMethodName((String)args[1]);
lr.setThrown((Throwable)args[2]);
log.log(lr);
}
}

and the resulting code will do what you want:

C:\>javac TLog.java

C:\>java TLog
11-04-2009 22:34:25 TLog main
FINER: THROW
java.lang.Exception: Ooops
at TLog.main(TLog.java:9)

C:\>ajc -source 1.5 TLog.java Hack.aj

C:\>java TLog
11-04-2009 22:34:35 TLog main
SEVERE: THROW
java.lang.Exception: Ooops
at TLog.main(TLog.java:9)

Arne
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,430
Messages
2,571,676
Members
48,796
Latest member
Greg L.

Latest Threads

Top