I'm developing a web application with Java and Jboss.
I use the java logger java.util.logging for logging messages.
I'm interested that all output messages that makes the application have a custom header caption. For example:
[21.2.2017 13:25:28][CERT:1642 ENS:5][ERROR]: java.lang.ArithmeticException: / by zero
Where the custom header caption is:
[21.2.2017 13:25:28][CERT:1642 ENS:5][ERROR]
I've been able to do it, creating a custom (extend java.util.logging.Formatter).
Logger logger = null;
StreamHandler handler = null;
MyCustomFormatter format = null;
logger = Logger.getLogger(Logger.GLOBAL_LOGGER_NAME + "." + custom_name);
format = new MyCustomFormatter();
handler = new StreamHandler(System.out, format);
logger.addHandler(handler);
I'm only able to show the header caption in cases where I call the logger explicitly. For example:
Anywhere in the code:
logger.info(msg);In catch exception:
logger.severe(e.printStackTrace());
I would like to add this caption in ALL messages, also in error or exception messages that I don't control with try/catch.
How I can do?
EDIT 1
My custom logger has a parent logger, and this parent logger has the root logger.
Logger.getLogger("")
|------> Logger.getLogger("global")
|------> Logger.getLogger(Logger.GLOBAL_LOGGER_NAME + "." + custom_name)
I have done a test. I have created a custom PrintStream and OutputStream to avoid that the errors don't show on the console and log file, setting it on System.err:
CustomNullPrintStream pse = new CustomNullPrintStream(new CustomNullOutputStream());
System.setErr(pse);
- CustomNullPrintStream extends PrintStream
- CustomNullPrintStream extends OutputStream
Which I have overrided all methods without content code.
Then, if I provoke an exception like this:
try {
throw new Exception("ERROR TEST");
} catch (Exception e1) {
e1.printStackTrace();
}
The exception is not visible on console and log file. But If I provoke an exception like this without try/catch:
int iii = 1/0;
iii = iii+1;
The exception is visible and keeps showing on console and log file. Why? In two cases are exceptions/errors.
[CERT:1642 ENS:5]part of the message come from? The date format is quite easy with a pattern-formatter. The other part may be too. - James R. Perkins[date hour] [ids that identifies the current user (it isn't the WildFly user, is the object instance of user)][type of message]:- stivex