Java Application + Logging - java

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

Related

java.util.logging stops working when I incorporate a library that uses Log4j 2

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.

Delaying Log4j initialization

My application is going to have a configuration file that configures its settings.
I want this to be the only configuration file the application uses,
The application will be run from a jar file, which means that log4j will be started as soon as the application is run.
Is there any way to delay initialization of log4j so i can use information from configuration file to configure log4j and then start log4j?
As Andreas writes, you (or a library you call) obviously use Log4J. But without a short code example, we can't tell you what's going on (Hint: by removing code to get a short example, you might find the culprit yourself).
According to the Documentation it should even be possible (starting from release 2.4) to change the configuration programmatically after Log4J started.

Log4J - determine if logger cannot write to file system

I am trying to use Log4J in my application, but my problem is that the machine where I want to run this app has not given me write permissions to the local drive. But Log4J is not throwing any errors, it simply skips the logging.
So what I want to do is write some code in which if Log4J cannot write to the local file system it will return some feedback/message in my app. Is there a way to tell if Log4J can write to the local filesystem, from Log4J itself? Or is there any way that I determine how many words Log4J has written into its log file in each operation? It's so then I could determine whether or not logging was actually occurring and then take appropriate action.
have you read the FAQ
Quote from the site
No. log4j is not reliable. It is a best-effort fail-stop logging
system.
By fail-stop, we mean that log4j will not throw unexpected exceptions
at run-time potentially causing your application to crash. If for any
reason, log4j throws an uncaught exception, please send an email to
the log4j-user#logging.apache.org mailing list. Uncaught exceptions
are handled as serious bugs requiring immediate attention.
I prefer my application continue even if logging fails.
What you described is possibly a function of 'application monitoring'. There are many tools to see if particular file system is getting full or a directory/file not changed for a while etc.
Having said that, you can do basic checks at the very beginning of application - like permissions to create file in the directory meant for logging.

How do I view the Java Log?

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

How to write to log files in java?

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.

Categories

Resources