I have spend some time to figure out a sustainable way to filter out ERROR
logs from my application in real time from JBoss
server. With Log4j
I can easily use SocketAppender
and I configured it the way it will notify ERROR
logs in real time. Unfortunately I can't do this with JBoss AS 7
. I can't find a SocketAppender
kind of implementation on it other than a SMTPAppender
. So I came up with a way to solve this. Please take a look what I have done so far.
I wrote a kind of wrapper for Logger
.
A sample class of my application
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class MyClass {
private Logger logger= LoggerFactory.getLogger(MyClass.class);
public void updateData(String data){
logger.info("updating data with {} ",data);
try {
// do somthing
}catch (Exception e){
//something went wrong
logger.error("update fail ",e);// I want to get this message real time
}
}
}
Now I thought of a way to get ERROR
log.
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class MyLogger {
private Logger logger = null;
private MyLogger() {
}
public static MyLogger getLogger(Class clazz) {
MyLogger logger = new MyLogger();
logger.logger = LoggerFactory.getLogger(clazz);
return logger;
}
public void info(String format, Object... arguments) {
logger.info(format,arguments);
}
public void error(String format, Object... arguments) {
// now i can send this message real time,
// i am calling web service with error log by a new Thread
logger.error(format,arguments); // say line number 10
}
}
MyLogger
use as follows
public class MyClass {
private static MyLogger logger= MyLogger.getLogger(MyClass.class);
public void updateData(String data){
logger.info("updating data with {} ",data);
try {
// do somthing
}catch (Exception e){
//something went wrong
logger.error("update fail ",e); // say line number 14
}
}
}
At this point my requirement matched, But this create a new issue.
Following is the logger format configuration of JBoss
<pattern-formatter pattern="%K{level}%d{HH:mm:ss,SSS} %-5p (%F:%L) (%t) %s%E%n"/>
Now %F
-File name will become the MyLogger
class. and %L
- Line number will become the line number of MyLogger
which leads inaccurate logs .
part of the sample log
(MyLogger.java : 10) // but this should be (MyClass.java : 14)
Now I have change this pattern to
<pattern-formatter pattern="%K{level}%d{HH:mm:ss,SSS} %-5p [%c:%L] (%t) %s%E%n"/>
This will almost work but %c
-class name gives correct class as expected. But there is no way to get line number(correct line number).
After change logger paten what I get
(com.my.test.MyClass : 10) // but this should be (com.my.test.MyClass : 14)
Here are my questions.
- Is there any log appender (
SocketAppender
orJMSAppender
) work withJBoss AS7
?(found none working solution with Jboss AS 7) - Is there any better approach rather than this?
- Is there any way to get correct Line number using this approach?
I have already asked an question in JBossDeveloper
. But still there is no comment at least.
https://developer.jboss.org/thread/249729
Any help highly appreciate.
If you are using a version equal or higher to JBoss 7.2 (EAP 6.1), you can use the Log4j appenders as custom handlers (check this issue).
eg.
In other case the only one way to make it work in JBoss AS 7 is to develop a custom
java.util.logging.Handler
forSocketAppender
.Take a look in Creating a custom logging handler in JBOSS as 7.1.0 Final and Custom log handlers on 7.0.1 to create your custom log handler and then delegate them to
Log4jAppenderHandler
.eg (sample code):
I think the best approach is this, so you would not have the logging implementation in your application.
See also: Logging Configuration
I hope this help.