I have a jsp project where I using the java.util.logging.Logger class to log information. At the moment my code is logging exceptions using this class, however, i would like to view the data written this log. How do I do that?
It depends how your logging is set up.
In general, it will either be in your server log (for example, for Tomcat, they're in the Tomcat home directory under the logs directory), or in a file that's been configured for the app.
You will need to configure the logging output to write into a file (or the console). You can then tail the file to keep up-to-date.
See these tutorials on configuration : writing a log file, configuring java logging
When you initialize your Logger class you have to add Handler to enable writing this somewhere.
For Example if you want to write this to a file use the FileHandler
logger.addHandler(new FileHandler("mylog.log");
Refer to the Handler documentation for more infos:
http://download.oracle.com/javase/1.4.2/docs/api/java/util/logging/Handler.html
As the anonymous horse said, there are lots of tools for doing this.
But if you want to do it within a Java application, then there are two approaches:
You could use something like the Apache Commons Tailer class to read stuff written to the end of the log file.
You could create your subclass of java.util.logging.Handler to process the log events before they reach the log file.
If you are using Java Logging API then you can use http://sourceforge.net/projects/jlogviewer/ - it accepts .log or .xml
Related
I have an existing application that logs messages using the java.util.logging API. As far as I can see, there are no configuration files for it in the framework, though there is some code to create a file appender. When I run the application, I get log messages to the console and to a file.
Now, I need to incorporate a library that uses Log4j 2. When I do so, I lose the console logging from the main application (though log messages still get written to the file appender that is created programmatically).
I imagine that, since the file appender is working in the original application, that I can solve my problem by also programmatically creating a console appender in the main application. However, I don't know if that's right or a kludge.
So, my question: is there anything general that I need to know about making java.util.logging and Log4j 2 interoperate? If the original application is not coded properly or according to best practices, I can change it.
Now, I need to incorporate a library that uses Log4j 2. When I do so, I lose the console logging from the main application (though log messages still get written to the file appender that is created programmatically).
The
Log4jBridgeHandler will remove handlers if the install method is called from code. You can always print the log tree to see what handlers are attached or attach a tool like JConsole to inspect the logger tree with and without the 3rd party lib.
I imagine that, since the file appender is working in the original application, that I can solve my problem by also programmatically creating a console appender in the main application. However, I don't know if that's right or a kludge.
Programmatic configuration of the logger tree should be done with the LogManager config option:
A property "config". This property is intended to allow arbitrary configuration code to be run. The property defines a whitespace or comma separated list of class names. A new instance will be created for each named class. The default constructor of each class may execute arbitrary code to update the logging configuration, such as setting logger levels, adding handlers, adding filters, etc.
Create a stand alone named class that just installs the handlers in the constructor.
Set the java.util.logging.config.class system parameter to the name of your class.
Otherwise if you have a logging.properties you set config to your class name.
So, my question: is there anything general that I need to know about making java.util.logging and Log4j 2 interoperate? If the original application is not coded properly or according to best practices, I can change it.
The java.util.logging.LogManager can only see classes on the system class loader. In that case log configuration in code is required to gain access to the correct classloader.
It might be easier to remove all JUL configuration and bridge to Log4j2. You can then leverage the configuration needed through that framework.
So basically i have a dropwizard project which uses logback by default,but i want to use log4j in my project.The log files i want to create are access and application logs.so basically application log will contain the overall logs and the access log will contain just logs for access of that service.
Now in logback i can do this directly using the logback-access jar and defining the required configs in config.yaml file.So my question is how exactly i can achieve this in log4j,like is there something available similar to logback-access in log4j or would i have to write an appender for it,if yes how exactly.
Thank you for your answers in advance
I'm creating a java open source utility package and I would like to know if it is ok to include logging (like log4j) into that package.
The dilemma is if I include log4j into my package, where will i output the log file, I wouldn't want the log file to be at a wrong place for the user, but i would still want to create logs for debugging.
As well there is the issue of the user wanting to integrate my logs into the project itself.
and how will he be able to do that.
What would you recommend is the best way of doing this?
Thanks.
I think the normal way to do it, is not include the Log4j Jar in the package and let the library user to decide which version of Log4j to use.
Also you don't have to worry about where to output the log file, since the user will have its own log4j.properties or log4j.xml configuration.
The library user can also decide which level of logging to use from your library or whether to suppress it. For instance with Amazon AWS libraries, I can tell that I only want warning messages or the output will be too verbose. In that case I add to my log4j.properties:
log4j.logger.com.amazonaws=WARN
Said that, I'd also remind you that a modern alternative to log4j is SL4J.
I am bit confused so, thought to ask experts.
I have written small java application. I have function which reads properties files which contains the path of the directory where to write the log and exceptions. Lets say I get exception while reading the properties file since logger wouldn't have been initialized by that time, how can I know that there was an error or exception? If I have written e.printStackTrace() in catch block where it will be printed? I am running this application through windows scheduler.
Thanks for advice.
BR
SC
Forget about Log4j. That was useful when logging capabilites were not embbeded inside JRE itself. You can configure the logging properties by a means of a logging.properties file (which is provided at JVM start point). Inside the application you can log messages with different levels of severity with or without nested exceptions.
Here is a useful page with example of basic configuration and logging capabilites use.
Use log4j configure it [here it will configure it self by using log4j.properties or .xml from the classpath ] , initialize it in app startup and use it app wide.
If you want to know for sure that the exception was thrown, don't catch it :) The printStackTrace() writes to stderr. You could run your app though console to read it. If you want to log no matter what without any config or extra libs, you can use java-util.logging which is bundled with the jdk and writes to stdout by default
I have made a java application and wants to generate log files so whenever my client would encounter some problem, he can deliver me those log files so that I can correct my code accordingly.
Kindly provide me a small sample program that writes a statement to a log file. Kindly mention the .class files you are using with their full import statements.
The application is multi-threaded so Is it better to generate separate log files for each thread or not?
Is it better to clear all previous log files before starting the program?
macleojw is correct: You should try writing the code yourself.
Here is an overview of the Java logging framework that ships with the JDK. You may wish to check out Commons Logging and Log4J.
Regarding the second part of your question (which was editted out for some reason) I would recommend having all threads log to the same file but logging the thread name along with the log message allowing you to grep the file for a specific thread if required. Also, with most logging frameworks you can configure them to maintain a rolling window of the last N log files rather than explicitly deleting old files when an application starts.
Apache Log4j does everything you require. I hope that you can figure out how to use it on your own.
Take a look at Log4j, and specifically this set of step-by-step examples. It's pretty trivial.