How to configure the log file globally for all environments?

Hi there,

In our web application we are using custom logging. We are using Eclipse IDE for our development.

During development, we retrieve the catalina.base and append the path of the log file, but in Eclipse it fails to identify the path. It works fine when we deploy the war file in Tomcat during test and development.

So we have two paths one for local development and another for test and production servers

// Path for testing and production
private static String logFile = System.getProperty("catalina.base") + "/webapps/SSG/logs/ssglog.txt";
   
//Path while development
//private static String logFile = "C:/SSG/logs/ssglog.txt";

How can we use the same path for development (in Eclipse), testing and production servers using Tomcat?

The complete code is given below

import java.io.*;
import java.text.*;
import java.util.*;

public class SSGLog {

    // Path for testing and production
      private static String logFile = System.getProperty("catalina.base") + "/webapps/SSG/logs/ssglog.txt";
   
      //Path while development
      //private static String logFile = "C:/SSG/logs/ssglog.txt";
   
    private final static DateFormat df = new SimpleDateFormat ("yyyy.MM.dd  hh:mm:ss ");
           
    private SSGLog() { }
   
    public static void setLogFilename(String filename) {
        logFile = filename;
        new File(filename).delete();

        try {
            write("LOG file : " + filename);
        }
        catch (Exception e) {
            System.out.println(stack2string(e));
        }
       
    }
   
    public static void write(String msg) {
        write(logFile, msg);
    }
   
    public static void write(Exception e) {
        write(logFile, stack2string(e));
    }

    public static void write(String file, String msg) {
        try {
            Date now = new Date();
            String currentTime = SSGLog.df.format(now);
            FileWriter aWriter = new FileWriter(file, true);
            aWriter.write(currentTime + " " + msg
                    + System.getProperty("line.separator"));
            System.out.println(currentTime + " " + msg);
            aWriter.flush();
            aWriter.close();
        }
        catch (Exception e) {
            System.out.println(stack2string(e));
        }
    }
   
    private static String stack2string(Exception e) {
        try {
            StringWriter sw = new StringWriter();
            PrintWriter pw = new PrintWriter(sw);
            e.printStackTrace(pw);
            return "------\r\n" + sw.toString() + "------\r\n";
        }
        catch(Exception e2) {
            return "bad stack2string";
        }
    }    
   
}

Please help.
ShajiAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

CEHJCommented:
In our web application we are using custom logging.
Why?
0
ShajiAuthor Commented:
CEHJ,

We find this easy and convenient for our needs.

Shaji
0
CEHJCommented:
How can it be easy and convenient if you have immediate problems with it?
0
OWASP: Avoiding Hacker Tricks

Learn to build secure applications from the mindset of the hacker and avoid being exploited.

mccarlIT Business Systems Analyst / Software DeveloperCommented:
Apart from the concerns that CEHJ has already raised above, which I agree with, your immediate problem should be easily resolved.

During development, we retrieve the catalina.base and append the path of the log file, but in Eclipse it fails to identify the path
That's correct, it is only Tomcat that will set that system property. But that is ok, you can set the same system property within Eclipse so that it can work from there also.

Go to the Run menu and then Run Configurations...  In the window, on the left hand side, make sure to select the configuration that launches your code/test/etc. And then on the right, go to the Arguments tab, and in the "VM arguments" textbox, add the following to the end of the list of arguments that are already there (if any)...

-Dcatalina.base="C:\"

Open in new window

(Or whatever path you want, for the development logs to go)
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
ShajiAuthor Commented:
Thanks mccarl, it is working like a champ.
0
ShajiAuthor Commented:
Thanks mccarl. Your solution was timely as well as accurate.
0
mccarlIT Business Systems Analyst / Software DeveloperCommented:
You're welcome! :)
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.