Log4j2 JMX: Updating the logging level via jConsole doesn't take effect - java

I am trying to configure log4j2 so that
I can access the loggers via JMX and
change their log levels.
When I hook everything up, I am able to access the LoggerContext via JConsole, which contains all of my LoggerConfigs.
Each LoggerConfig show the correct log level with which the application is running. And if I update a log level in any LoggerConfig it call the MBean and update the logging level correctly, which I have inspected via debugging. But the problem is that updating the log level doesn't take any effect. The application keeps on logging with the old logging level.
For example If I start the application with the XYZ logger with log level DEBUG, and change this log level to FATAL via JConsole, it changes successfully but application keep on logging in DEBUG level.
If instead of updating the single LoggerConfig if I update the LoggerContext by passing the new xml configuration with updated logging levels it works as expected.
What should be the problem? Documentation is quite and google refused to help me.
My Findings:
As far as I understood this problem is that when I update the Log level in the LoggerConfig via JConsole, log4j2 update the log level via MBean correctly but its not updating the LoggerContext, it simply call the setter method and returns. But in case if I update LoggerCoentext log4j2 create the new context to update itself.

This was indeed a bug. Thanks for reporting it. This has been fixed in trunk and will be included in the next release (rc2).

Sounds like a bug in Log4j2. I see you have raised it on their JIRA # https://issues.apache.org/jira/browse/LOG4J2-637 so we'll track the progress there. :)

Related

How to enable DEBUG level logging with Jetty from UI

I'm trying to set the logging level to DEBUG in a Jetty instance. It is working when we add
-Dorg.eclipse.jetty.util.log.class=org.eclipse.jetty.util.log.StdErrLog
-Dorg.eclipse.jetty.LEVEL=DEBUG Under property file and pass it to the JVM.
My use case is I was trying to set logging level from UI so that without restarting the server we can switch logging level.
for example, DEBUG to WARN
Option 1: Jetty 9 and StdErrLog with JMX
Using Jetty 9, enable JMX on the server, and use a JMX client to set the StdErrLog debug attributes.
Look for MBean Name org.eclipse.jetty.util.log:type=stderrlog,id=0
The attribute is called debugEnabled (a boolean).
Option 2: Jetty 10+ and jetty-slf4j-impl with JMX
If using Jetty 10+, enable JMX on the server, and use a JMX client to set the level on your logger of choice.
Eg: if you are using the jetty-slf4j-impl (the spiritual successor of StdErrLog) ...
Look for MBean Name org.eclipse.jetty.logging:type=jettyloggerfactory,id=0
All of the named loggers are listed as attributes.
The operations let you getLoggerLevel and setLoggerLevel using normal JMX operation techniques.
Option 3: Different Logging Framework
Alternatively, you can use a more robust logger, like logback, or log4j2, or even java.util.logging and have even more options of runtime control over the logging.
Some offer runtime control over JMX.
Some offer the ability to reload the configuration if the configuration file changes.
Check your logger to know what it can do.
Option 4: Write your own logger level change API
You can easily write your own tooling to change the logger level based on some kind of event that your own application is in control over.
You can even make this a Handler on Jetty that only responds to specific named ServerConnectors that only list on localhost.
You just need to fetch the named Logger and then set the level. (the exact API and technique depends on your chosen logger implementation)

How to set Jetty's overall logging level SLF4J+Logback?

I am using Jetty http client in my application. I am also using SLF4J with LogBack.
How to set Jetty's overall logging level to INFO?
I have followed the instructions on Jetty's documentation to provide jetty-logging.properties file with contents like this:
# Configure Jetty for SLf4j Logging
org.eclipse.jetty.util.log.class=org.eclipse.jetty.util.log.Slf4jLog
# Overall Logging Level is INFO
org.eclipse.jetty.LEVEL=INFO
Jetty is logging fine via SLF4J, but the LEVEL setting doesn't seem to work.
When I set the level from Logback configuration then it works, but I want my code to be logged in DEBUG level and Jetty in INFO level.
Jetty is very noisy in DEBUG level.
Any help is appreciated!
You need to set the Properties....
// change Jetty Logger
Properties p = new Properties();
p.setProperty("org.eclipse.jetty.LEVEL", "OFF");
org.eclipse.jetty.util.log.StdErrLog.setProperties(p);
I wasn't able to make Jetty to log at custom level. Because if Jetty finds SLF4J in my application then it will start using it with the logging level I have specified in logback.xml configuration file.
So I made a little hack. I specified Jetty to use Java Utils Logging and set level to INFO.
// Setting Jetty logger implementation and level (DEBUG | INFO | WARN | IGNORE)
System.setProperty("org.eclipse.jetty.util.log.class",
"org.eclipse.jetty.util.log.JavaUtilLog");
System.setProperty("org.eclipse.jetty.util.log.class.LEVEL", "INFO");
And for redirecting Java Utils Logging messages to SLF4J I used jul-to-slf4j-1.7.12.jar
That's how I got it working. If there is a better way I sure want to know.

Logging to Sentry in Android with extra info

I have logging working to Sentry from android just using the EventBuilder from raven-java, but I need to attach extra info which apparently EventBuilder or java.util.logging can't do.
I tried to use log4j but it doesn't pick up log4j.properties, how can I configure it with the Sentry DSN and other information? I have no preference about what logging library to use, so logback or log4j2 will do but they seem to suffer from the same problems.
I tried to configure logging programmatically but can't add the appender SentryAppender.
I also tried using android-logging-log4j library but this has the same problem.
I realize that https://github.com/joshdholtz/Sentry-Android exists which might work but it doesn't do retries in case of network issues which is quite important.
The https://github.com/joshdholtz/Sentry-Android library now supports saving of offline captures and failed captures as of version v1.1.0 - https://github.com/joshdholtz/Sentry-Android/releases/tag/v1.1.0

Framework internal logs going to system.out (websphere 7.0)

I'm using Log4j in my application and application logging is working fine while framework in my application is internally using commons-logging and logs are going to System.out but I want to redirect them to a separate file and also want to enable DEBUG level.
Tested this on tomcat and working fine. Generating all logs in their respective files. But when I deploy code on WAS7.0, only INFO level of framework logs are being generated SystemOut.log. (my log4j.xml is fine as it is working fine on tomcat)
Any help would be appreciated. Thanks!
I was having the same problem. I found the answer in Websphere Docs.
Note: Trace information, which includes events at the Fine, Finer and
Finest levels, can be written only to the trace log. Therefore, if you
do not enable diagnostic trace, setting the log detail level to Fine,
Finer, or Finest does not effect the logged data.
Reference: http://pic.dhe.ibm.com/infocenter/wasinfo/v7r0/index.jsp?topic=%2Fcom.ibm.websphere.base.doc%2Finfo%2Faes%2Fae%2Frtrb_loglevels.html
In Java Utility Logging terms FINE = DEBUG, so DEBUG level and below will not appear in System.out, only int the WAS trace log.

hsqldb internal event log configuration

How do I configure internal event monitoring in hsqldb? When I run my Java application, I get the following warnings:
log4j:WARN No appenders could be found for logger (HSQLDB2C7984E18B.org.hsqldb.persist.Logger).
log4j:WARN Please initialize the log4j system properly.
The documentation tells me log4j is not the only option, but it doesn't tell me how to configure my application. Can anyone point me to this documentation? Remember, I don't want to use log4j for hsqldb.
It bears mentioning that a 3rd-party jar I'm referencing requires log4j. Does hsqldb automatically detect that log4j is present and then attempt to use it? Or am I missing something fundamental about how logging works?
Check out this link. It says
The logging facility hands off to Log4j if Log4j is found in the classpath, and otherwise will hand off to java.util.logging.
The consequence of what the message indicates is that no logging for HSQLDB will take place because no appenders were found.
If you wish to suppress the message, add a line like the one below to the log4j.properties file:
log4j.logger.HSQLDB2C7984E18B.org.hsqldb.persist.Logger=FATAL
This will log only FATAL events, which wouldn't happen in normal operation.
You also state that you don't want to use log4j for HSQLDB. Software components that can use log4j leave the logging configuration (including level and where to log, etc.) to the log4j properties settings, which you can edit and configure.
In this case, the logger name is based on the "unique" database name which is initially autogenerated, but which you can change in HSQLDB.
Because as YWE noted hsqldb uses log4j by default if it is found in the classpath, I needed to figure out how to override the log4j.properties found in the 3rd-party library. This I managed to do as follows:
Copy the existing log4j.properties to my project, and add the following line at the beginning:
log4j.rootLogger=WARN, CONSOLE
Add the following VM Arguments:
-Dlog4j.log4j.defaultInitOverride=true
-Dlog4j.configuration=C:/full/path/to/my/log4j.properties
Make sure this line of code runs before anyone (e.g. hsqldb) attempts to use log4j:
org.apache.log4j.PropertyConfigurator.configure("log4j.properties");

Categories