I'm using java.util.logging. In my code, I'm adding multiple handlers to my logger. I have 1 ConsoleHandler and 1 FileHandler. I only want levels above Info (inclusive) to be printed to the console and all levels to be printed to the log file. When I try to set the logging levels of the 2 file handlers accordingly, and add the handlers to the logger, the log file ends up only including levels above Info (as specified by the ConsoleHandler). Does this mean I cannot specify two different file handlers for one logger? How can I resolve this issue and get the desired functionality? Really appreciate your help.
I solved my question myself. You need to set the level of the logger to Level.FINEST before adding the handlers to it because the logger by default is set to a level higher than FINEST. Therefore setting the ConsoleHandler's level to FINEST without changing the level of the logger will not produce the required results.
Related
I've seen java util logging configured in a number of ways. Usually the fully qualified class name is used for the logger name. In the corresponding logging.properties file, I've seen log level configured at either the package level or the class level in a couple different ways. For example, to set the loglevel to FINE for com.example.MyClass:
com.example.level = FINE
com.example.* = FINE
com.example.MyClass = FINE
com.example.MyClass.level = FINE
Do all four of these variants work (assuming they are not overridden later in the file)?
Are any of the options "more correct" than the others?
Does java.util.logging just assume .level if its not there?
I tried finding the definitive guidance on this config file but stopped at
https://docs.oracle.com/javase/8/docs/technotes/guides/logging/overview.html#a1.8 which seems quite underspecified.
Do all four of these variants work (assuming they are not overridden later in the file)?
Most of the online tutorials I've seen use the explicit .level suffix...is that preferred (and why)?
Under the standard LogManager, com.example.* = FINE and com.example.MyClass = FINE would not change the level. The key has to end with .level in order to change the level.
Per the LogManager documentation:
All properties whose names end with ".level" are assumed to define log levels for Loggers. Thus "foo.level" defines a log level for the logger called "foo" and (recursively) for any of its children in the naming hierarchy. Log Levels are applied in the order they are defined in the properties file. Thus level settings for child nodes in the tree should come after settings for their parents. The property name ".level" can be used to set the level for the root of the tree.
If you are using a subclass of LogManager then you need to consult that documentation to verify the syntax.
Does java.util.logging just assume .level if its not there?
According to the documentation it does not. If you declare something without the .level it would just be considered a LogManager entry.
Are any of the options "more correct" than the others?
The LogManager properties file can't create loggers. This means that your log file must match how the code is creating the loggers. For instance, if your file is using com.example.level = FINE and your code is using com.example.MyClass1 and com.example.MyClass2 as the logger name you will never see MyClass1 or MyClass2 switch to FINE because the code never created package parent logger. Root is the parent of all named loggers so that is the ideal way to change multiple loggers at once. If you need to do anything really complicated then you use the config option supported by the LogManager.
I am trying to log with Jersey 2.23. Since this version, the class LoggingFilter is deprecated, as one can read for example here: https://jersey.java.net/documentation/latest/logging_chapter.html. So I have to use LoggingFeature instead.
What did not work was the register method of ResourceConfig as it is explained in this documentation. But in the end the property method worked:
client.property(LoggingFeature.LOGGING_FEATURE_LOGGER_LEVEL_SERVER, "WARN");
This prints every message as a warning. Unfortunately it is documented nowhere (at least I couldn't find anything) which values are allowed. Obviously it has to be a String, because I get a log message that there is no way to transform the value into a String, when I try anything else than a String. Now I want to log this messages with level TRACE and I can't find a suiting String to achieve this. "TRACE" and "FINE" did not work for example, in these cases nothing is logged. I have to mention that I use Log4j2 together with the Slf4jBridgeHandler because Jersey uses JUL.
I struggled with this myself for several hours before finally uncovering the "mystery" today.
It is somewhat counter-intuitive but the level you are setting with the LOGGING_FEATURE_LOGGER_LEVEL_SERVER is actually the minimum level the server's logger must be set to in order to print your logs. I had assumed, based on the name, that this was setting the actual logger level -- implying that setting it to FINER or FINEST would produce more output. Instead it is simply "turning off" logging unless a specific level is met.
As an example, if you set it to WARNING then you will see the logs as long as the server/client is set to print at least WARNING level. The levels as defined by java.util.logging are:
SEVERE (highest value)
WARNING
INFO
CONFIG
FINE
FINER
FINEST (lowest value)
So by setting it to WARNING (the literal WARN does not work for me in 2.23.1) you will see the logs because, by default, logging is typically at INFO level.
An alternate solution is to change the default logging level in your logging.properties file which is typically in $JAVA_HOME/jre/lib/logging.properties. You could, for example, make the following changes to the file and you would no longer need to set any special properties in your code:
java.util.logging.ConsoleHandler.level = FINEST
org.glassfish.jersey.test.JerseyTest.level = FINEST
The obvious disadvantage to this is that it will effect anything you run from this JDK/JRE. There are ways you can override this standard location and use an alternate logging.properties file but it depends on how you're executing your code so I will let you research that based on your circumstances.
One example would be this thread which explains how to do it when using Maven: Logging level under maven surefire
I have a message driven system with, say, com.example.BaseMessagingAgent class, which is a basic class for many message agents. This base class logs message events. There are many subclasses of this base class, implementing different specific agents of system. Let us com.example.MyAgent which extends com.example.BaseMessagingAgent is one of them.
I want to log messages only related to class MyAgent. But I cannot define logging as:
log4j.logger.com.example.MyAgent=DEBUG, APPENDER
because logging occurs in parent class com.example.BasicMessagingAgent - I will record nothing.
And I also do not want to set logging in base class:
log4j.logger.com.example.BaseMessagingAgent=DEBUG, APPENDER
because it will log events for all agents, and I will have a lot of unnecessary logging.
Does enyone know how to limit logging to only one subclass?
You should write a filter for Log4j since AFAIK there is no way to put such information on log4j.properties file. More details at http://books.google.it/books?id=vHvY008Zq-YC&lpg=PA95&ots=yi335bZU7z&dq=&pg=PA95#v=onepage&q&f=false
It's pretty simple, actually.
First, add the appender to the root logger. Really. It will make your life much more simple.
Now configure the whole thing:
log4j.rootLogger=DEBUG, APPENDER
log4j.logger.com=ERROR
log4j.logger.com.example.MyAgent=DEBUG
The default for all classes below "com.*" will be to log only errors. The sole exception is com.example.MyAgent which will log at debug level.
You need to set the root logger to DEBUG as well or it will throw away all the DEBUG log messages.
The next step is to use one logger per instance. To get that, simply remove the static in the line which you create your logger and replace BaseMessagingAgent with getClass()
I know, it looks like overkill but that's how log4j works. Also creating a logger per instance isn't very expensive (unless you create millions of MyAgent per second).
If you really want to add an appender to a single class, then don't forget to turn off additivity (...Class.additivity=false) or you will get all log messages twice.
I have to stop showing logging messages of some methods within the system without changing a Java code (loggers)...
I was thinking, is it possible to configure log4j.properties, where I could skip the logging for certain method? and is it possible to do log on method level at all with log4j?
Thanks,
K.
log4j does not support this level of configuration. But you can implement an appender that will extends your current appender (for example, create a MyConsoleAppender that extends ConsoleAppender). Then, add a check for the method that you which to skip in the checkEntryConditions() method.
Why is the Log4j rootLogger in my application not filtering log events according to level? In my log4j.properties, I have several loggers:
log4j.rootLogger=info,stdout
log4j.logger.com.name.myapp=debug,myapp
log4j.logger.org.castor=debug,castor
log4j.logger.org.exolab.castor=debug,castor
log4j.logger.org.hibernate=debug,hibernate
log4j.logger.org.springframework=debug,spring
Each of the loggers receive and record numerous log events at levels DEBUG and above, which is what I expect and desire. The rootLogger, however, despite being set to level INFO, is displaying all of these events, too, including the DEBUG events, which is not what I expect and not what I desire. Instead, I would expect it to filter the DEBUG events, but display only the events at level INFO and higher (WARN, ERROR, and FATAL), which is also what I want. Why is rootLogger displaying all of the events?
See this answer to a similar question about logger chaining in Log4j:
The way Log4j chaining works is a bit
counter intuitive (to me at least). If
the request level is equal to or above
the threshold of the most specific
matching logger, it is accepted. Once
the request is accepted, it gets
handled by the complete chain of
ancestors regardless of their
thresholds!
This means that no matter to what level you set the threshold of the root logger, it will always accept and output the log event that any other logger accepts, unless you disable chaining for that child logger or explicitly set the threshold of its appender to a higher level.
So, in this case, there are two ways that you can prevent root logger from capturing the events from the other loggers. The first is the more selective approach of disabling log event chaining:
log4j.additivity.com.name.myapp=false
log4j.additivity.org.castor=false
log4j.additivity.org.exolab.castor=false
log4j.additivity.org.hibernate=false
log4j.additivity.org.springframework=false
The second way is simpler, but more restrictive since it suppresses all events on the console that are lower than INFO (DEBUG and TRACE):
log4j.appender.stdout.Threshold=info
Check out the inheritance described in the intro. If you specify a level at the package level, it won't inherit the root logger's level. You're using debug in the packages you specify, not info. Specifying the level overrides whatever has been inherited.
If you want to inherit the root logger's level, get rid of the level specification in your logger configurations.
To retrieve the rootlogger, are you using Logger.getRootLogger()? If not, you may not be getting the real root logger. If so, make sure the Threshold of stdout isnt at debug; the Threshold of appenders override that of the logger Levels.