You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@struts.apache.org by "Sundar @eSaravana" <su...@eSaravana.com> on 2001/02/27 20:01:51 UTC

Has any one used struts and log4j successfully?

Has anyone used log4j with struts? I have used log4j before, but I am new to
struts. So, I am just looking for a short cut. If you have,could you please
send me some code fragments to start off.

I would really appreciate it.

Cheers.....!
Sundar


Re: Has any one used struts and log4j successfully?

Posted by Thierry Cools <th...@s1.com>.
What's your problem actually ? I tried to use Log4 in my project using struts as well and I didn't get any, I just out my 'log4j.properties' in the classpath and put some 'Category' variables in my code like this

public class ListBillerAction extends ActionBase {
    
  private static Category cat = Category.getInstance("Main.Biller"); 

  public ActionForward perform(ActionServlet servlet,
                               ActionMapping mapping,
                               ActionForm form,
                               HttpServletRequest request,
                               HttpServletResponse response) {

    ListBillerSearchCriteriaForm criteriaForm = (ListBillerSearchCriteriaForm) form;
    BillerSearcher billerSearcher;
    Screen dataScreen = null;

      if (buttonPressed(request, "search"))  {
        if (cat.isInfoEnabled())
         cat.info("!!!!!! Button Search has been pressed !!!!!!");

     ....


And it works great for me !

Hope it helps,
Thierry
 

Thierry Cools
 
Senior Java Developer 
S1 Brussels 
Kleine Kloosterstraat, 23 
1932 st. Stevens-Woluwe 
Belgium 
Tel : +32 2 200 43 82 
Email : thierry.cools@s1.com 

  ----- Original Message ----- 
  From: Sundar @eSaravana 
  To: struts-user@jakarta.apache.org 
  Sent: Tuesday, February 27, 2001 8:01 PM
  Subject: Has any one used struts and log4j successfully?


  Has anyone used log4j with struts? I have used log4j before, but I am new to
  struts. So, I am just looking for a short cut. If you have,could you please
  send me some code fragments to start off.

  I would really appreciate it.

  Cheers.....!
  Sundar



Re: Has any one used struts and log4j successfully?

Posted by Mark Balster <ma...@hotmail.com>.
I currently use Log4j with NDC enabled combined with struts.

Here is how I use it:

- Run PropertyConfigurator from a startup servlet
- import org.apache.log4j.Category in your code
- set reference to log4j in your code

public class loginAction extends Action {
    static Category cat = Category.getInstance(loginAction.class.getName());
....
...
.
.


and in the body just call it:
            if ( cat.isDebugEnabled() )
                cat.debug("Attempting login with session: " + sessionid);


I hope that helps.  I wish all products were as easy as log4j to integrate.

-Mark

----- Original Message -----
From: "Sundar @eSaravana" <su...@eSaravana.com>
To: <st...@jakarta.apache.org>
Sent: Tuesday, February 27, 2001 2:01 PM
Subject: Has any one used struts and log4j successfully?


> Has anyone used log4j with struts? I have used log4j before, but I am new
to
> struts. So, I am just looking for a short cut. If you have,could you
please
> send me some code fragments to start off.
>
> I would really appreciate it.
>
> Cheers.....!
> Sundar
>
>

RE: Has any one used struts and log4j successfully?

Posted by "Martin J. La Jeunesse" <mj...@pacbell.net>.
I had a little trouble using PropertyConfigurator.configure() to log to a
file: I wasn't exactly sure where to put the .lcf file so that servlets
could find it. I ended-up coding an absolute location. I kind-of thought it
should go in the application directory under WEBAPPS or in the application
web-inf, but that didn't seem to work. Other than that, works great!

> -----Original Message-----
> From: Sundar @eSaravana [mailto:sundar@eSaravana.com]
> Sent: Tuesday, February 27, 2001 11:02 AM
> To: struts-user@jakarta.apache.org
> Subject: Has any one used struts and log4j successfully?
>
>
> Has anyone used log4j with struts? I have used log4j before,
> but I am new to
> struts. So, I am just looking for a short cut. If you
> have,could you please
> send me some code fragments to start off.
>
> I would really appreciate it.
>
> Cheers.....!
> Sundar
>


Re: Has any one used struts and log4j successfully?

Posted by "Craig R. McClanahan" <Cr...@eng.sun.com>.
Mark Balster wrote:

>             FileInputStream inFile = new
> FileInputStream(getServletContext().getRealPath(messages.getMessage("Log.Pro
> pertiesFile")) );

You should also be aware that this will only work if your servlet container
unpacks your web application into an open directory structure.  If it runs
directly out of the WAR file (or out of blob objects in a database, for
example), the getRealPath() call will return null.

In such a case, you might wish to use the temporary working directory provided
to you by the servlet container as a context attribute:

    File workDir = (File)
      getServletContext().getAttribute("javax.servlet.context.tempdir");
    File logFile =
      new File(workDir, messages.getMessage("Log.Properties.File"));
    String logPathname = logFile.getAbsolutePath();

Of course, there are no guarantees that the temporary work directory will
survive the shutdown of your servlet container -- check the container docs for
specifics.

Craig



Re: Has any one used struts and log4j successfully?

Posted by Mark Balster <ma...@hotmail.com>.
PropertyConfigurator.configure() does like an absolute path and you still
can give it one using a relative web path in your webapp directory.  You can
even specify the relative path your struts resource file ( if you chose to
do so ).

If you use a startup servlet, something like this should work:

        MessageResources messages = (MessageResources)
getServletContext().getAttribute(Action.MESSAGES_KEY);

        Properties props = new Properties();
        try {
            FileInputStream inFile = new
FileInputStream(getServletContext().getRealPath(messages.getMessage("Log.Pro
pertiesFile")) );
            props.load(inFile);
            inFile.close();

            // manipulate what needs to be manipulated :-)
            props.setProperty("log4j.appender.stdlog.File",

getServletContext().getRealPath(messages.getMessage("Log.Qwickrate"))  );

            props.setProperty("log4j.appender.ddrlog.File",

getServletContext().getRealPath(messages.getMessage("Log.DDR"))  );

            // configure the logging device
            PropertyConfigurator.configure(props);

        } catch (IOException io) {
          System.out.println(io.getMessage());
        }



----- Original Message -----
From: "Martin J. La Jeunesse" <mj...@pacbell.net>
To: <st...@jakarta.apache.org>
Sent: Tuesday, February 27, 2001 3:57 PM
Subject: RE: Has any one used struts and log4j successfully?


> I had a little trouble using PropertyConfigurator.configure() to log to a
> file: I wasn't exactly sure where to put the .lcf file so that servlets
> could find it. I ended-up coding an absolute location. I kind-of thought
it
> should go in the application directory under WEBAPPS or in the application
> web-inf, but that didn't seem to work. Other than that, works great!
>
> > -----Original Message-----
> > From: Sundar @eSaravana [mailto:sundar@eSaravana.com]
> > Sent: Tuesday, February 27, 2001 11:02 AM
> > To: struts-user@jakarta.apache.org
> > Subject: Has any one used struts and log4j successfully?
> >
> >
> > Has anyone used log4j with struts? I have used log4j before,
> > but I am new to
> > struts. So, I am just looking for a short cut. If you
> > have,could you please
> > send me some code fragments to start off.
> >
> > I would really appreciate it.
> >
> > Cheers.....!
> > Sundar
> >
>
>

RE: Has any one used struts and log4j successfully?

Posted by "Martin J. La Jeunesse" <mj...@pacbell.net>.
I had a little trouble using PropertyConfigurator.configure() to log to a
file: I wasn't exactly sure where to put the .lcf file so that servlets
could find it. I ended-up coding an absolute location. I kind-of thought it
should go in the application directory under WEBAPPS or in the application
web-inf, but that didn't seem to work. Other than that, works great!

> -----Original Message-----
> From: Sundar @eSaravana [mailto:sundar@eSaravana.com]
> Sent: Tuesday, February 27, 2001 11:02 AM
> To: struts-user@jakarta.apache.org
> Subject: Has any one used struts and log4j successfully?
>
>
> Has anyone used log4j with struts? I have used log4j before,
> but I am new to
> struts. So, I am just looking for a short cut. If you
> have,could you please
> send me some code fragments to start off.
>
> I would really appreciate it.
>
> Cheers.....!
> Sundar
>