Why is INFO logged but not FINEST - java

I have set my logging.properties with the following and restarted tomcat:
com.example.handler.level = FINEST
And I have a method of:
public SearchHistoryItem getSearchHistoryItem(Api1 api1, String stringId, String resultId) {
SearchHistoryItem item = api1.getSearchHistoryDetails(stringId, resultId);
Level level = logger.getLevel();
logger.log(Level.INFO, "Log level is: " + level);
logger.log(Level.FINEST, "item is: " + item);
return item;
}
And a return of the following:
13-Dec-2016 18:32:53.093 INFO [ajp-nio-127.0.0.1-8009-exec-4] com.example.handler.SomeHandler.getSearchHistoryItem Log level is: FINEST
If you note. The first log message prints what I am looking for. So I see that logging is indeed FINEST, and I see that log messages are being written. But I don't see the second log message ever print. Is there something other than setting the level in the properties file that I need to worry about?
UPDATE
I am using java.util.logging.Logger with default configurations as far as I can see.
UPDATE
I have been playing with this more and it seems that if I change to Level.FINE they will log. Perhaps there is something somewhere filtering out logs that are to high?

I suppose your problem is with logs in the console.
The used default level for that Handler is Level.INFO.
http://docs.oracle.com/javase/6/docs/api/java/util/logging/ConsoleHandler.html
With FileHandler which the used default level is Level.ALL, you would have not had the problem.
Either you set the level for ConsoleHandler programatically, either you set the it in the configuration file (https://docs.oracle.com/cd/E19717-01/819-7753/gcblo/)
This post gives more details about the question :
Why are the Level.FINE logging messages not showing?

Related

java.util.logging.Logger log level is coming up as null

I am running into logging issues after upgrading my Java EE app from Weblogic/JDeveloper to 12.1.2 to 12.1.3.
System.out.println is printing to the server log fine but log.info("test") is not. The logging works if I set the log level e.g. log.setLevel(Level.INFO). Here are my test results.
// This works
System.out.println("test1");
// Output when run: test1
// This does not work. Nothing prints to the server log
log.info("test2");
// The above works if I set the log level
log.setLevel(Level.INFO);
log.info("test3");
// Output when run: test3
// This prints null. It appears that logging level is set to null on server startup
System.out.println(" what is my current logging level: " + log.getLevel());
As per Oracle documentation if no logging configuration is provided then the default logging.properties in JDK/JRE/Lib is used. The default log level is INFO.
I have also tried to load up logging.properties and switch log4j but nothing works.
I don't want to set log level to something in every class. Is there a way to set this on server startup or debug what is causing/setting the logging level to null.
From the logger documentation:
The result may be null, which means that this logger's effective level will be inherited from its parent.
So you only have to set the level of the root logger. You should check that a ConsoleHandler is installed and that the level is set to INFO or lower. You should also check that writing to System.err shows up in the log file as that is the stream that is used for the ConsoleHandler.
I use this code for mine "Logg class" so every class is connected with it, so every exception from every class use this. if you have many classes you want to get logged from this is kind a smart and easy way. I just have one problem too, this code creates a txt file every time you run a program and it's connected with the Logg class. I will fix it soon, so i will edit this answer then. heres my code:
Top of class
private final static Logger logger = Logger.getLogger(Logg.class.getName());
private static FileHandler fh = null;
and in the method you can have this;
Properties prop = new Properties();
InputStream in = Logg.class.getResourceAsStream("loggconfig");
if (in != null) {
prop.load(in);
} else {
throw new FileNotFoundException("property file '" + in + "' not found in the classpath");
}
Some exemple, this txt file will be named the current year, date and time.
Date date = new Date();
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy_MM_dd_HH_mm");
fh = new FileHandler((dateFormat.format(date) + ".log"), false);
Logger l = Logger.getLogger("");
fh.setFormatter(new SimpleFormatter());
l.addHandler(fh);
l.setLevel(Level.INFO);
have this under argument you want to logg:
logger.log(Level.WARNING, "Bla bla");
you can also have a file, like a config so you can choose what level you want to get logged. Then you need to create a property file, my is named loggconfig.
in the config file i have this code:
Level=WARNING
so it just every exception i have as a WARNING, i can change it to INFO to so i get everything logged. Smart to only use two levels in the code.
So if you want to use config file u need to change l.setLevel too this:
l.setLevel(Level.parse(prop.getProperty("Level")));
so it takes the level from config file.

Unable to set log level in a Java web start application?

Some logging levels appear to be broke?
I run a Java web start (which I will begin to call JWS from now on) application straight from a GlassFish 3.1.2.2 instance. The client has a static logger like so:
private final static Logger LOGGER;
static {
LOGGER = Logger.getLogger(App.class.getName());
// Not sure how one externalize this setting or even if we want to:
LOGGER.setLevel(Level.FINER);
}
In the main method, I begin my logic with some simple testing of the logging feature:
alert("isLoggable() INFO? " + LOGGER.isLoggable(Level.INFO)); // Prints TRUE!
alert("isLoggable() FINE? " + LOGGER.isLoggable(Level.FINE)); // ..TRUE
alert("isLoggable() FINER? " + LOGGER.isLoggable(Level.FINER)); // ..TRUE
alert("isLoggable() FINEST? " + LOGGER.isLoggable(Level.FINEST)); // ..FALSE
My alert methods will display a JOptionPane dialog box for "true GUI logging". Anyways, you see the printouts in my comments I added to the code snippet. As expected, the logger is enabled for levels INFO, FINE and FINER but not FINEST.
After my alert methods, I type:
// Info
LOGGER.info("Level.INFO");
LOGGER.log(Level.INFO, "Level.INFO");
// Fine
LOGGER.fine("Level.FINE");
LOGGER.log(Level.FINE, "Level.FINE");
// Finer
LOGGER.finer("Level.FINER");
LOGGER.log(Level.FINER, "Level.FINER");
LOGGER.entering("", "Level.FINER", args); // <-- Uses Level.FINER!
// Finest
LOGGER.finest("Level.FINEST");
LOGGER.log(Level.FINEST, "Level.FINEST");
I go to my Java console and click on the tab "Advanced", then I tick "Enable logging". Okay let's start the application. Guess what happens? Only Level.INFO prints! Here's my proof (look at the bottom):
I've done my best to google for log files on my computer and see if not Level.FINE and Level.FINER end up somewhere on the file system. However, I cannot find the log messages anywhere.
Summary of Questions
Why does it appear that logging of Level.FINE and Level.FINER does not work in the example provided?
I set the logging level in my static initializing block, but I'd sure like to externalize this setting to a configuration file of some sort, perhaps packaged together with the EAR file I deploy on GlassFish. Or why not manually write in some property in the JNLP file we download from the server. Is this possible somehow?
Solution for problem no 1.
After doing a little bit more reading on the topic, I concluded that a logger in Java uses a handler to publish his logs. And this handler in his turn has his own set of "walls" for what levels he handles. But this handler need not be attached directly to our logger! You see loggers are organized in a hierarchical namespace and a child logger may inherit his parents handlers. If so, then By default a Logger will log any output messages to its parent's handlers, and so on recursively up the tree (see Java Logging Overview - Oracle).
I ain't saying I get the full picture just yet, and I sure didn't find any quotes about how all of this relates to a Java Web Start application. Surely there has to be some differences. Anyways, I did manage to write together this static initializing block that solves my immediate problem:
static {
LOGGER = Logger.getLogger(App.class.getName());
/*
* This logic can be externalized. See the next solution!
*/
// DEPRECATED: LOGGER.setLevel(Level.FINER);
if (LOGGER.getUseParentHandlers())
LOGGER.getParent().getHandlers()[0].setLevel(Level.FINER);
else
LOGGER.setLevel(Level.FINER);
}
Solution for problem no 2.
The LogManager API docs provided much needed information for the following solution. In a subdirectory of your JRE installation, there is a subdirectory called "lib" and in there you shall find a "logging.properties" file. This is the full path to my file on my Windows machine:
C:\Program Files (x86)\Java\jre7\lib\logging.properties
In here you can change a lot of flavors. One cool thing you could do is to change the global logging level. In my file, this was done on row 29 (why do we see only a dot in front of the "level"? The root-parent of all loggers is called ""!). That will produce a hole lot of output; on my machine I received about one thousand log messages per second. Thus changing the global level isn't even plausible enough to be considered an option. Instead, add a new row where you specify the level of your logger. In my case, I added this row:
martinandersson.com.malivechat.app.App.level = FINER
However, chances are you still won't see any results. In solution no 1, I talked about how loggers are connected to handlers. The default handler is specified in logging.properties, most likely on row 18. Here's how my line reads:
handlers= java.util.logging.ConsoleHandler
Also previously, I talked about how these handlers in their turn use levels for what should trouble their mind. So, find the line that reads something like this (should now be on row 44?):
java.util.logging.ConsoleHandler.level = INFO
..and in my case I swapped "INFO" to "FINER". Problem solved.
But!
My original inquiry into this matter still hasn't provided an answer how one can set these properties closer in par with the application deployment. More specifically, I would like to attach these properties in a separate file, bundled with the application EAR file I deploy on GlassFish or something like that. Do you have more information? Please share!

netbeans logging tutorial

hi i'm working with a netbeans project, that they gave me to work on, and i need to check if the connection to the database (postgres) are working well, and i notice that there are lines in the code like
static Logger log = Logger.getLogger(getNivel.class);
then
if (user != null) {
log.debug("Usuario identificado: " + user.getIdUsuario() + "obteniendo su cuadro de mandos");
but i don't know how to see if the connection is actually working, because i can't find the log file. so i searched the internet and i found this page
link
but i don't really understand what i should do to see those messages. Can anybody help me?
Not sure which logging library you use, but I presume you use the standard java logging api (java.util.logging package) ...
You dont probably see it because you are using DEBUG level of logging and your project is set to print WARNING and above I believe. So if you what to quickly see whats going on, either change temporarily the code to:
if (user != null) {
log.severe("Usuario identificado: " + user.getIdUsuario() + "obteniendo su cuadro de mandos");
OR create logging.properties file in the root of you project, where you will enable logging on DEBUG level for the class or package for which you want to see the logs:
com.mycompany.mypackgepath.myclass.level=DEBUG
Note, that there might be already such a file in your project. Also you can add more handlers in order to print the log output to a file as well as to netbeans console
handlers=java.util.logging.FileHandler, java.util.logging.ConsoleHandler
java.util.logging.ConsoleHandler.level=ALL
java.util.logging.FileHandler.pattern = %h/Library/Logs/xd%g-%u.log
java.util.logging.FileHandler.limit = 10485760
java.util.logging.FileHandler.count = 2
java.util.logging.FileHandler.formatter = java.util.logging.SimpleFormatter
java.util.logging.FileHandler.encoding = UTF8
You might want to check this tutorial for example: http://www.javapractices.com/topic/TopicAction.do?Id=143
Btw. the logger should be private and final ;)

Disabling awt/swing debug (fine) log messages

I'm using java logging to write log messages of my application to a log file and other destinations. Having set the log level to FINE, I also get (unwanted) messages from AWT/Swing, such as:
{0}, when grabbed {1}, contains {2}
and others. Looking at the JDK source code (see e.g. here), one sees that the name of the corresponding logger is sun.awt.X11.grab.XWindowPeer.
What I understood from the Java logging framework is that this logging handler should inherit its loglevel from its parents like sun.awt.
I tried the following:
Logger.getLogger("sun.awt").setLevel(Level.OFF);
but the AWT/Swing debug messages still appear in the log output.
What's the recommended way of programmatically disabling these log messages (while still allowing FINE messages from other sources) ?
If you just want to log the messages of your own application, you can disable all messages and then explicitly enable messages for your application:
Logger.getRootLogger().setLevel(Level.OFF);
Logger.getLogger("package.of.your.application").setLevel(Level.ALL);
Inside the properties-file for logging (e.g. logging.properties) this would be:
.level = OFF
package.of.your.application.level = ALL
I had the same problem today. Searching on google this is the top url and I don't find a good source to a answer, so I will post mine :)
Assuming that Andre use the java.util.logging API, it's possible to add a Handler that will control the format of your log, using setFormatter(Formatter newFormatter).
So I extended Formatter and checked if the class of the log contains java.awt, javax.swing or sun.awt.
class MyFormatLog extends Formatter {
#Override
public String format(LogRecord record) {
if( !record.getSourceClassName().contains("java.awt") &&
!record.getSourceClassName().contains("javax.swing.") &&
!record.getSourceClassName().contains("sun.awt.") ) {
//format my output...
} else {
return "";
}
}
}
I could not find the getRootLogger() method in the Logger class any more. This is what works for me:
logger = Logger.getLogger("my.package");
Logger l = logger;
while (l != null) {
l.setLevel(Level.OFF);
l = l.getParent();
}
logger.setLevel(Level.FINER);
Logger.getLogger("java.awt").setLevel(Level.OFF);
Logger.getLogger("sun.awt").setLevel(Level.OFF);
Logger.getLogger("javax.swing").setLevel(Level.OFF);
Try
Logger.getRootLogger().setLevel(Level.OFF);

Logger.setLevel() doesn't enable logging correctly

Situation: I have this log4j logger:
private static final Logger logger = Logger.getLogger(ThisClassName.class);
And am trying to set it programatically through:
Logger.getLogger(ThisClassName.class).setLevel(Level.DEBUG);
Still, DEBUG level prints are swalloed (while INFO prints are printed successfully).
Even this bit has no effect: Logger.getRootLogger().setLevel(Level.DEBUG);
Calling logger.debug("foo") reaches Category.forcedLog() and ConsoleAppender.doAppend(), and then fails (quits) at:
if(!isAsSevereAsThreshold(event.getLevel()))
Any idea why this is happening?
Your appender is configured with a threshold greater than debug, so while the logger doesn't ignore the entries, your appender doesn't record it. You need to configure the threshold of your ConsoleAppender to be DEBUG as well, either through your config file or programatically:
((ConsoleAppender)someLogger.getAppender("CONSOLE")).setThreshold(Level.DEBUG);
Config files are usually the more elegant solution for this sort of thing.
Edit: Note that apparently, any subclass of AppenderSkeleton (including ConsoleAppender) shouldn't have a threshold filter set by default. So it's likely that somewhere in your configuration you're actually manually assigning a threshold ( > Debug) to that appender, as #justkt hints.

Categories