0
votes

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.

2
What version of JBoss AS, JBoss EAP or WildFly are you using? - James R. Perkins
Hi @JamesR.Perkins , I use WildFly 8 (8.2.1.Final) and Java (jdk 1.8.0_121) with Eclipse IDE. - stivex
Where does the [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
I have created a custom header caption. Where the structure are: [date hour] [ids that identifies the current user (it isn't the WildFly user, is the object instance of user)][type of message]: - stivex

2 Answers

0
votes

I don't know how your logger is initialized, but you could try setting the formatter on the root logger (call parent() on the logger untill null).

Another way is to change the global template itself instead of overriding it. This can be done programmatically or via command line.

System.setProperty("java.util.logging.SimpleFormatter.format", <template>);

see the javadoc of that class for how a template is constructed.

0
votes

If you already have a custom formatter the best way to use your formatter is to make it a module within WildFly. From there you can add it as a custom-formatter within WildFly and assign it to the handlers you want to see that format on.

Here's an example of some CLI commands:

module add --name=com.example.formatter --resources=/path/to/formatter.jar
/subsystem=logging/custom-formatter=my-formatter:add(class=com.example.formatter.MyCustomFormatter, module=com.example.formatter)
/subsystem=logging/console-handler=CONSOLE:write-attribute(name=named-formatter, value=my-formatter)

The above would use your formatter for all messages written to the console. You could change it to use a different handler or just assign a new handler to specific log categories.

One thing to note is you shouldn't alter the log manager in code like you're doing. This will change logging for the container and all deployments within the container.