• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 147
  • Last Modified:

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.
0
Shaji
Asked:
Shaji
  • 3
  • 2
  • 2
1 Solution
 
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
Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

 
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
 
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 3
  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now