问题
I am using simple System.out.printf
/ System.out.println
statements to perform JBoss logging (I am deploying to JBoss EAP 6.2). I have noticed that when an exception occurs I tend to lose println
messages immediately preceding the throwing of the exception. This makes it impossible to debug the exception.
To verify this, I put in my WAR the following code:
System.out.println("foo");
int i = 0; if (i==0) throw new RuntimeException();
...
(the int i = 0; if (i==0)
part is just to silence the compiler which would otherwise complain)
Indeed, when this code is executed, in the server's log file (standalone/log/server.log
) I don't see foo
at all. I see the reporting of the exception but nothing above it.
I tried printing the same message 100 times in a loop, explicitly doing a System.out.flush()
and doing a java.util.concurrent.TimeUnit.MINUTES.sleep(1)
before throwing the exception. Nothing changed, no output at all is seen in server.log
. Only the exception trace shows, but no foo
above it.
I understand that JBoss wraps the std:out
and std:err
into its own logging framework and I am hypothesizing that there is some kind of buffering involved that causes System.out.println
output to be lost.
I don't have any logging.properties
file in my WAR and have not modified the standard configuration I am using (standalone-full.xml
) with respect to logging. I used to have a logging.properties
file but I removed it as it caused all System.out.println
to be lost as described in this question. Once the logging.properties
file is removed, some System.out.println
output is at least seen except right before an exception, i.e. when I need it the most.
My questions are:
- why am I losing this output and how can I ensure that
System.out.println
is always included inserver.log
? - what are some tips to simplify the logging situation in JBoss? The whole thing very complex and undocumented.
回答1:
Turns out there was another logging.properties
file lurking around. Once I removed it I could again seen all System.out.println
messages on the server.log
trail. It's still not clear to me why the presence of logging.properties
files should mess-up with plain STD:OUT
output.
Update
Tried to follow the suggestion by James R. Perkins to try add a jboss-deployment-structure.xml
file in my META-INF
directory to disable processing of my component by the logging subsystem and added the following:
<jboss-deployment-structure xmlns="urn:jboss:deployment-structure:1.1">
<deployment>
<exclusions>
<module name="org.jboss.logging"/>
<module name="org.apache.log4j"/>
</exclusions>
</deployment>
</jboss-deployment-structure>
… this didn't work. System.out.println
output was still lost. So the only workaround is to remove the logging.properties
and log4j.xml
files. Thanks to James R. Perkins for suggesting this may in fact be a bug in JBoss EAP 6.2.0
回答2:
Ran into same issue as that of the op and found this. It essentially says you should add
<console-handler name="STDOUT">
<level name="INFO"/>
<formatter>
<pattern-formatter pattern="%s%E%n"/>
</formatter>
</console-handler>
<logger category="stdout" use-parent-handlers="false">
<level name="INFO"/>
<handlers>
<handler name="STDOUT"/>
</handlers>
</logger>
to your JBoss logging subsystem in standalone.xml or domain.xml or using CLI, you can do:
/subsystem=logging/console-handler=STDOUT:add(level=INFO,formatter="%s%E%n")
/subsystem=logging/logger=stdout:add(level=INFO,handlers=[handler=STDOUT],use-parent-handlers=false)
You can do the same for STDERR by replacing "stdout" with "stderr".
The above configuration works thus:
- Adds a second ConsoleHandler
- This will be used just for calls to System.out.println(...).
- Defines the stdout logger
- This is used by JBoss Logging anyways, but here we explicitly define it so we can configure it.
- Add the STDOUT handler only to the stdout logger
- Set use-parent-handler=false so we don't get duplicate logging
- Change the formatter pattern in the second ConsoleHandler so it will simply print the string as-is — no extra formatting.
This implies that you may not need to remove your logging.properties and log4j.xml files. Hopefully, this solves the problem.
来源:https://stackoverflow.com/questions/41905471/logging-system-out-println-getting-lost-in-jboss