Where does java.util.logging.Logger store their log
Asked Answered
S

3

44

I am a bit lost with java Logger

private static Logger logger = Logger.getLogger("order.web.OrderManager");
logger.info("Removed order " + id + ".");

Where do I see the log? Also this quote from java.util.logging.Logger library:

On each logging call the Logger initially performs a cheap check of the request level (e.g. SEVERE or FINE) against the effective log level of the logger. If the request level is lower than the log level, the logging call returns immediately.
After passing this initial (cheap) test, the Logger will allocate a LogRecord to describe the logging message. It will then call a Filter (if present) to do a more detailed check on whether the record should be published. If that passes it will then publish the LogRecord to its output Handlers.`

Does this mean that if I have 3 request level log:

logger.log(Level.FINE, "Something");
logger.log(Level.WARNING, "Something");
logger.log(Level.SEVERE, "Something");

And my log level is SEVERE, I can see all three logs, and if my log level is WARNING, then I can't see SEVERE log, is that correct? And how do I set the log level?

Sod answered 11/6, 2010 at 19:36 Comment(4)
The built in logger isn't that great. Replace it with log4j (logging.apache.org/log4j/1.2). There are some good documents on the log4j site explaining how to use it...Chitin
@bwawok: It's also not a bad idea writing against Apache Commons Logging. It will use log4j if it's available or fall back to the built-in logging if log4j isn't available.Addendum
I would not use commons logging. Has a lot of problems with it, and stinks when using tomcat. If coding for yourself code in log4j. If coding a library, code with slf4j (slf4j.org)Chitin
Instead of just saying "built in logging sucks", it might be helpful to explain why instead of just parroting popular opinion blindly...Escadrille
H
28

Where do I see the log?

In a log file or standard output, depending on your actual log handler configuration. This can be set via a property file or directly via the logging API.

Does this mean that if I have 3 request level log...

SEVERE is the most important (highest priority) and FINE is the least important message type of the 3 shown in your example. So if your log level is SEVERE, only the SEVERE messages get logged. If level is FINE, all 3 messages get logged.

This is very useful when in real production environment, you may want to log only the errors and possibly warnings (which are - hopefully - fairly rare, but you want to know about them), so you can set log level to WARNING. However, in your development environment, when e.g. debugging a problem, you want to see all information in the logs, even though it creates a large amount of log data and slows down the application. So you set log level to FINE or FINEST.

Here is a good introduction to Java Logging.

Update: a simple example from the above page to configure the logger to log to a file at level FINEST:

Handler fh = new FileHandler("%t/wombat.log");
Logger.getLogger("").addHandler(fh);
Logger.getLogger("com.wombat").setLevel(Level.FINEST);

To log to the console, replace the FileHandler above with a ConsoleHandler:

Handler ch = new ConsoleHandler();
Logger.getLogger("").addHandler(ch);

This is just an example though - in a real app, it is preferable to configure the logging via a config property file.

Hydrophobic answered 11/6, 2010 at 19:55 Comment(6)
ohhh it is the other way around. Thank you very much. Peter, can u show me how to configure the log to print to file or console?Sod
You mention that in real app, it is preferable to configure the logging via property file. Would be kind enough to show me how to do it?Sod
@Harry, I am not sure if the format is standard, but at least for Log4J you can find good examples with explanation here: logging.apache.org/log4j/1.2/manual.html, in the "Configuration" section.Thirzia
Thanks for this. But the link 'introduction to Java Logging' isn't working. Would be great if you update it.Armoury
You have getLogger("") in some places and getLogger("com.wombat") in others. What should I use when I'm trying to make a third-party library log to the console that was constructed with Logger.getLogger("com.wombat.MyClass")?Dorthadorthea
The answer of where to see your logs: "In a log file". Lovely.Ardell
F
5

The Java TM Logging Overview is quite interesting to answer all your questions on the Java Logger:

logging overview

You will see your log where the Handler(s) associated with your Logger will generate said Log (in a Console, or in a Stream...).
The default configuration establishes a single handler on the root logger for sending output to the console.

Log Level:

Each log message has an associated log Level. The Level gives a rough guide to the importance and urgency of a log message. Log level objects encapsulate an integer value, with higher values indicating higher priorities.

The Level class defines seven standard log levels, ranging from FINEST (the lowest priority, with the lowest value) to SEVERE (the highest priority, with the highest value).

Florence answered 11/6, 2010 at 19:58 Comment(0)
N
1

Where it goes is dependent on your configuration. There are details about this in the API docs. The log level is the exact reverse of what you said. If you have a configuration of FINE, everything that is FINE, WARNING, SEVERE will show up, but if you have it set to SEVERE then only those will come up.

In general you should use FINE while debugging and switch to SEVERE when it is in a production environment.

Nourishment answered 11/6, 2010 at 20:0 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.