When I start Tomcat from within Eclipse, I get a console with logging information which are very much what I am used to, they always end with the statement that Tomcat was launched within n milliseconds.
However, whenever an exception occurs, the stack trace for this Exception does not show up in the console. I was used to be able to see them "happen" right from the IDE.
Setting the level in logging.properties to a different value (FINEST or ERROR) brings up the expected changes, but stil no log entry for Exceptions whatsoever.
Does Tomcat log Exceptions in a different way?
The HTTP 500 message mentions the Tomcat logs. So there should be a place where much more info is sent to.
The respective log directories only contain simple access logs and what I see on the console in Eclipse.
Which configuration is responsible for the output to the console?
My logging.properties looks like this:
handlers = java.util.logging.ConsoleHandler
java.util.logging.ConsoleHandler.level = FINE
org.apache.catalina.level=FINE
org.apache.catalina.core.ContainerBase.[Catalina].[localhost].level = INFO
org.apache.catalina.core.ContainerBase.[Catalina].[localhost].[/manager].level = INFO
org.apache.catalina.core.ContainerBase.[Catalina].[localhost].[/host-manager].level = INFO
Wich logging implementation are you using? in logging.propertie you are configuring apache log. To user pure java log, try to configure programmatically, something like this:
static Logger logger = Logger.getLogger(YourClass.class.getName());
System.setProperty("java.util.logging.SimpleFormatter.format", "%4$s: [%1$tc] %5$s %n");
//Creating consoleHandler and fileHandler
consoleHandler = new ConsoleHandler();
LOGGER.addHandler(consoleHandler);
//Setting levels to handlers and LOGGER
consoleHandler.setLevel(Level.ALL);
consoleHandler.setFormatter(new SimpleFormatter());
.. When you want to log, do this:
LOGGER.log(Level.FINE, "HELLO LOG");
Related
I looked up the api about the logger class(here) and I was looking at the Logger.info method. I was confused when I saw its perimeter as a message displayed as a string public void info(String msg) which is same as System.out.println(). I am wondering what is the different between these two, and why do we use Logger instead of System.out.println when they can print out the same thing.
In Logger.
Logger.info("Hello")
Output:
[INFO ] 2015-08-07 11:18:46.140 [main] ClassName Hello
In System.out.println
`System.out.println("Hello")
Output:
Hello
Usually, because a Logger can be configured to write to a file (and the console). It might also be configured at higher (or lower) granularity as to messaging. For example, you might configure (at runtime) for level of warn. In which case, that logger would not display debug or info messages. It can include information such as the class that is writing, a line number, and a date and time (of the message).
Using a logger allows you to abstract out a lot of details and do a lot more than you could writing to stdout.
You can specify different destinations to write to. Different appenders write to a file, roll the file for given time periods, write to a queue or database, etc.
You can specify a consistent format for log messages instead of having to add it to every line you write to stdout.
You can choose an appender that buffers the output so that multiple threads can log without having the threads contend for the lock on the console object.
You can do a lot with filtering by category (typically package and classname) and log level (trace, debug, info, error, fatal), to make it easy to configure what log messages you want to see and which you want to ignore. With logging you can change the configuration in the logger properties or include a page in your application to change what gets filtered on the fly.
You can mix and match this stuff, for instance, setting up a specific smtp appender to email log messages for logging level of error or higher, in addition to writing the messages to a rolling file or whatever.
The main difference between a Logger and System.out.println is Logger: Prints the text in a file(text file)System.out.println: Prints the output in console
Logger is useful when you are going for any LIVE projects. Because if any project is developed and deployed, then you cannot check the console. At that time Logger will be useful to track the flow of your project also you can find the Error or Exception if you have given the logger in catch{...} block.
Also go through this Logger vs. System.out.println
But when we use any logging mechanism(log4j, slf4j, logback etc) we
configure appenders and corresponding target log files for each
package. By default console appender is turned off unless you
explicitly configure it to log to a destination.
The System.out.println always logs the message to a console appender.
So, it should be used only when we are sure that the console appender
is configured in the logger configuration file. Otherwise we end up
having logs logged on server console which is incorrect. Any log from
inside the application should go to the corresponding application log
and not the server log.
Let me explain with an example.
If we are building an application called Tracker using a logging mechanism to run in a tomcat container, and we configured appenders for application logging with destination log file as tracker-application.log and we did not configure the console appender.
Then if the System.out.println is encountered by the JVM then the log will go to server log of the tomcat server which is wrong because server log should only have information about the server and not the application.
But if we created a console appender with a target log file then it will be logged properly.
Hope I was clear. ;)
For best practices refer Do not use System.out.println in server side code or http://www.vipan.com/htdocs/log4jhelp.html
For differences between both of them please refer
Logger vs. System.out.println
I'm struggling with this basic stuff for weeks already.
Can't make simple logs working in my Google App Engine (JAVA) app.
I started from this tutorial. Looks pretty straight forward.
Inside my Endpoint class I've defined a Logger like this:
private static final Logger log = Logger.getLogger(GowMainEndpoint.class.getName());
Then in one of my endpoint methods I tried to log event like:
log.info("test");
There is nothing showing up in logs. I have logging.properties file and appengine-web.xml configured just like in the mentioned tutorial.
EDIT: Weird thing: log.warning("test"); is showing up logs...
Ok I figured it out:
in the logging.properties file I've changed
.level = WARNING
to
.level = INFO
The comment line says:
# Set the default logging level for all loggers to INFO
but now I got the feeling it should be more like:
# Set the MINIMUM logging level for all loggers to INFO
I have tried to remove System.out.println("I am Ranjit") from log file on tomcat. By setting Context attribute in Context.xml swallowOutput="true", It works fine with stdout.log.
But, Start creating log in catalina.log which is not fine.
I want to log only ERROR and EXCEPTION on stdout.log. I have tried it by setting logging.properties as below:
java.util.logging.ConsoleHandler.level = ERROR
java.util.logging.ConsoleHandler.formatter = java.util.logging.SimpleFormatter
&
org.apache.catalina.core.ContainerBase.[Catalina].[localhost].level = ERROR
org.apache.catalina.core.ContainerBase.[Catalina].[localhost].handlers = 2localhost.org.apache.juli.FileHandler
But it does not work. Please, help me out.
It is difficult to know what you are really asking here. However, if you are trying to change the format of the logger messages produced by java.util.logging, will need to write a custom Formatter class to do this.
It might be a better to switch to log4j logging as described here: http://tomcat.apache.org/tomcat-6.0-doc/logging.html#Using_Log4j
I have a java program using an external library. The main program uses log4j to log its messages and the library uses java.util.logging.
My problem is that log messages from the external library and the main program are mixed in the console.
I would like to redirect all log messages from the external library to a file. I tried to do that with a logging.properties file:
handlers= java.util.logging.FileHandler
.level= INFO
java.util.logging.FileHandler.pattern = foo.log
java.util.logging.FileHandler.formatter = java.util.logging.XMLFormatter
This file is initialized with:
System.setProperty("java.util.logging.config.file", "logging.properties");
Unfortunately, log messages from the external library keep appearing in the console.
Should I use something like slf4j to intercept log messages from java.util.logging?
Thank you for your time.
Here's some code from one of my programs. This also does automatic rotation. The config class is my own that's read from a properties files. You can just replace that with your own values.
Logger rootLogger = Logger.getLogger("");
logHandler = new FileHandler(config.getLogFile(),
config.getLogRotateSize()*1024*1024,
config.getLogRotateCount(), false);
logHandler.setFormatter(new SimpleFormatter());
logHandler.setLevel(Level.INFO);
rootLogger.removeHandler(rootLogger.getHandlers()[0]);
rootLogger.setLevel(Level.INFO);
rootLogger.addHandler(logHandler);
Note this is for a stand-alone program. Any application server has it's own logging configuration tools. The program can also change the formatter and levels on the fly if a dynamic debug mode is desired.
I can not find the results of my logging calls.
To log messages I tried both:
System.out.println("some message");
and
Logger logger = Logger.getLogger("MyLogger"); // Logger is java.util.logging.Logger
// ...
logger.info("some message");
I have deployed my app and after few tests I decided check out some
log messages. But there were no messages. I changed minimum severity
level to "Info" from default "Error", and only messages i've seen were
service messages like this:
http://dl.dropbox.com/u/1678938/logs.png
I would also be grateful if someone show a little snippet with logging some data (if it's works) - I suspect my problem may be the one of that stupid problems when somewhat incorrectly located comma can be the cause of situation.
Could it be that your logging.properties is setting the default value to WARNING?
Ours has this in our war/WEB-INF/logging.properties file:
# Set the default logging level for all loggers to WARNING
.level = WARNING
# Default level for subpackages of 'server' will be INFO
com.company.whatever.server.level=INFO
Problem has shrunk into more concrete form - App Engine "eats" info-messages, but shows others, such as error and warning messages.
After this call I have eventually seen my info messages:
log.setLevel(Level.INFO);
But it is still not clearly - why info-messages weren't being shown. Google's manual states:
Everything the servlet writes to the
standard output stream (System.out)
and standard error stream (System.err)
is captured by App Engine and recorded
in the application logs. Lines written
to the standard output stream are
logged at the "INFO" level, and lines
written to the standard error stream
are logged at the "WARNING" level.
Had exactly the same problem and after changing the value in logging.properties from
.level = WARNING
to
.level = INFO
The problem was definitively fixed. Google needs to update their documentation and/or change the value of the supplied default so that "INFO" log messages don't get swallowed.
I used to set LEVEL to SEVERE for testing
logger.log(Level.SEVERE, "test message");
It does not need changing any value in logging.properties