Java logging in client application - java

I've made a small desktop application in java for OS X. I've packaged in into a .app using JarBundler. Everything runs fine on my computer.
When I send the .app to someone else (also running a mac), the app opens and closes immediately. Is there a log file of some kind I can get from their computer (which I have full access to). Is there a way to get System.out.println statements or similar to show up in that file?

execute the application from the console, from there any errors will be printed to the standard error stream.

Please avoid using System.out.println() statements on the application. The method is synchronized and results in poor performance. Not to mention you may not be able to retrieve the statements based on who captures the console.
Use a logging solution like sl4j and back it up with a logger like log4j with a file appender. The file appender writes to a file and you can get your debug statements / stack traces from there.

Related

How to log from Java UDF installed in DB2

I have a JAVA UDF installed in DB2 10.5 and everything works ok.
But i have some catch blocks in which i would like to log some info along with the stack trace. Question is how can i log them and in which db2 log file will these info be printed.
I tried using System.out.println and looking into db2diag log file, but nothing was printed there.
In a java routine for Db2-LUW, the output to System.out.println will never appear in the Db2 diagnostics file, and if you are wise you should not force that.
There's more than one way to handle it.
One way is for your routine to redirect stdout before calling System.out.println.
Example
System.setOut(new PrintStream(new FileOutputStream("java_routine_log.txt")));
In the above example the filename is unqualified so by default it will appear in the instance diagnostics directory (by default ~${DB2INSTANCE}/sqllib/db2dump ).
Another way is to use a configurable logging framework which lets you control logging locations and other details for tracing.
Other ways exist also.

Reading live output of application when its already running

My setup:
Red Hat with:
Weblogic installation which hosts my (java) application.
What I try to achieve:
See the output (an error) of my application
Why I this way and not easier (set proper logging on the application itself):
This is a production server of a big company, I am not allowed to do any changes to the running applications
In the application something goes wrong and I am tasked with fixing it.
I checked the weblogic logs but they dont capture all the output of the application, and thus it does not capture the error im searching for.
So is there a way I can sort of connect to stdout/stderr and see the output printed live? (other suggestions are also most welcome but if its possible this seems like the easiest way to go?)
Extra info:
I have checked and confirmed the application writes to console, so it should appear in stdout. (right?)
Unless I misunderstand your question, you want to tail the logs.
Navigate to the directory where your logs are stored and run
tail -f LOGNAME.log
This should print to your console live any additional log lines that are added.

Why would logback ever READ the log file it is writing to?

In a java application that uses logback, and runs under Windows 7, the sysinternals process monitor (www.sysinternals.com) shows that the java process is READING the application's log file. Why would this be?
Our app is having issues and logback came under scrutiny when this was discovered. We have since found that this is irrelevant to our issues, but I would still like to understand it.
I had thought that an appender would only append to the end of the log file as its name implies and am surprised and embarrassed to see this, especially after I insisted it couldn't be possibly be true.
Can someone explain why logback would need to READ a logfile? I can categorically state that none of the application's code reads the file.

Java app startup with computer and be able to see system.out.println?

I wrote a simple Java app which I have placed in the start up folder of my programs which makes the program starts when the computer starts up. what is the easiest way to make it open a command line or something which I can see the System.out.println results instead of just running in the background?
You should familiarize yourself with logging frameworks such as logback and log4j. Instead of using System.out.println you use some special API and the logging library redirects all messages to preconfigured appenders like console or file.
In your case you can configure your application to log on console while developing and switch to file when configuring an application to run from startup.
This won't really open a new command line window on startup, but instead it will store all messages to some predefined file on disk - which is actually even better.
You can use Log4j API for logging the details with the predefined outputs. It is far better then using SOP. Because it is light waighted and also very simple to configure the logs in the other files with the output format whichever you want to make.
http://logging.apache.org/log4j/1.2/ Go to this url where you can find log4j api available.
Hope this work for you
Enjoy !!!

Best way to interact with application logs output from log4j (while also are being updated by application)

May be it is simpler than I think but I am confused on the following:
I want to be able to present to a user (in a graphical interface) the logs produced by Log4j.
I could just read the files as it is and present it, but I was wondering if there is a standard way to do it to so as to also get any updates that happen at the same time from the other parts of the application that log concurrently.
The log4j files could be multiple i.e. rolling appender
Also the presentation could be while there is no logging happening.
I.e. view of logs up to date
UPDATE:
I am constraint to Java 6
You can use Java 7's NIO2 libraries to get notified when one of multiple files get's modified in a directory, and reread & display it:
http://blogs.oracle.com/thejavatutorials/entry/watching_a_directory_for_changes
Have you tried the following tools :
Chainsaw
Xpolog
Perhaps add a database appender (JDBCAppender) and present the log entries from that?
Fro the official documentation of log4j:
Is there a way to get log4j to automatically reload a configuration file if it changes?
Yes. Both the DOMConfigurator and the PropertyConfigurator support automatic reloading
through the configureAndWatch method. See the API documentation for more details.
PropertyConfigurator#configureAndWatch
DOMConfigurator#configureAndWatch
For the on-demand reload of log4j config using GUI I would suggest expose it via a servlet in your J2EE application so that whole file can be edited in a web page (text area may be) and once saved you can overwrite your existing log4j file and reload the log4j config.
Maybe you could think about more "OS-level" solution.
I don't know if you are using win or linux, but on linux there is this realy nice command "tail".
So you could use ProcessBuilder to create OS process which goes something like "tail -f yourLogFile.txt".
And then read the OutputStream of the returned Process. Reading the stream will block waiting for new output from the process to be available, and will immediately unblock when such is available, giving you immediate feedback and possibility to read the latest changes of the log file.
However, you might have problems shutting this process down from Java.
You should be able to send SIGTERM signal to it if you know the process id. Or you could start a different process which could lookup the id of the "tail" process and kill it via "kill" command or something similar.
Also I am not sure if there is similar tool available on windows, if this is your platform.
If you write your own simple appender and have your application include that appender in your log4j configuration, your appender will be called whenever events are written to other appenders, and you can choose to display the event messages, timestamps, etc. in a UI.
Try XpoLog log4j/log4net connector. It parses the data automaticly and has predefined set of dashboards for it:
Follow the below steps
Download and install XpoLog from here
Add the log4j data using the log4j data connector from here and
deploy the log4j app here

Categories