Re: Logging from controller

Louis Malenica <[email protected]> Thu, 11 May 2006 20:14:18 +1000
Newsgroups gmane.comp.web.maverick.general
Organization [email protected]
Message-ID <[email protected]>
This is a multi-part message in MIME format.
--------------000407090801080401050209
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: quoted-printable

Hi Jeff,

Thanks for responding. I have written a simple Listener class that is=20
loaded at startup by Tomcat, which makes a static logger available to=20
the web-app (both as a static getter and a servlet context param), and I=20
have no problem using it from other areas such as jsp or servlets or=20
non-maverick pojo's. The logger itself is a simple custom jdk1.4 logger=20
(ie not log4j) and uses a very simple formatter. It uses a FileHandler=20
that points to a txt file in WEB-INF. No errors appear to be generated=20
and as far as I'm aware there are no weird classloader issues.

here is the Listener that creates the logger:

public class SiteLogger implements ServletContextListener {
  =20
    private static Logger logger;
  =20
    public static Logger getLogger() {
        return logger;
    }

    public void contextInitialized(ServletContextEvent e) {
      =20
        ServletContext sc =3D e.getServletContext();
      =20
        // get logger instance.
        logger =3D Logger.getLogger("global");
        // TODO: Should really be injected.
        logger.setLevel(Level.INFO);
      =20
        try {
            FileHandler fh =3D null;
            String root =3D sc.getRealPath("/");
            // TODO: Again value should be injected.
            fh =3D new FileHandler(root+"WEB-INF/log.txt");
            CustomFormatter cf =3D new CustomFormatter();
            fh.setFormatter(cf);
          =20
            logger.addHandler(fh);
        } catch(IOException ioe) {
            System.err.println("Can't load logger" + ioe.getMessage());
        }
        sc.setAttribute("fw.logging.SiteLogger", logger);
    }
...etc

here is the web.xml entry for the listener:

      <listener>
            <listener-class>fw.logging.SiteLogger</listener-class>
      </listener>

here is the code that does the logging in the controller's perform() meth=
od:

import fw.logging.SiteLogger;
...
SiteLogger.getLogger().log(Level.INFO, obj);

LM

Jeff Schnitzer wrote:
> Unfortunately I'm pretty sure that's going to be far too general a=20
> question.  You need to explain how your logging framework works if=20
> anyone's going to be able to help you.  Does it do anything weird with=20
> classloaders?
>
> Jeff
>
> Louis Malenica wrote:
>> Hi,
>>
>> I am trying to log via a standard custom logger from within the
>> perform() method of a controller that extends ThrowawayBean2. HOwever
>> nothing is being logged. My logging framework seems ok as it is
>> logging elsewhere in the app. Anyone come across this before?
>>
>> thanks.
>>
>> LM
>> R=C8=A7=EF=BF=BD:&q=EF=BF=BD[=EF=BF=BD=EF=BF=BD=EF=BF=BDy=EF=BF=BDhv=EF=
=BF=BD=EF=BF=BD=EF=BF=BD=EF=BF=BD^y=EF=BF=BDh=EF=BF=BD=EF=BF=BDi=EF=BF=BD=
=EF=BF=BDpy=EF=BF=BD=1E=EF=BF=BD=EF=BF=BDz=EF=BF=BD=1Er=EF=BF=BD=EF=BF=BD=
!=EF=BF=BD=EF=BF=BD=EF=BF=BDn}=EF=BF=BDh=EF=BF=BD=EA=AE=89=EF=BF=BD%=EF=BF=
=BD=08=EF=BF=BD=EF=BF=BD=EF=BF=BD=DE=8A{^=EF=BF=BD=EF=BF=BD=EF=BF=BDy=EF=BF=
=BD^r=19=20
>> =E8=96=882=EF=BF=BD=EF=BF=BD=EF=BF=BD=EF=BF=BD=EC=A8=BA=EF=BF=BD=EF=BF=
=BDm=E6=AC=89=EF=BF=BD=C3=A3    =E5=A1=A7H=04=C5=9Em*az=EF=BF=BD=EF=BF=BD=
=EF=BF=BD=EF=BF=BDbq=EF=BF=BDb=EF=BF=BDt=EF=BF=BD=EF=BF=BD=EF=BF=BD]5m=EF=
=BF=BD=1Ev=EF=BF=BD=EF=BF=BD=EF=BF=BD!xg=EF=BF=BD=EF=BF=BDx=EF=BF=BD=EF=BF=
=BD=1B=20
>> m=EF=BF=BD=EF=BF=BD=EF=BF=BDzV=EF=BF=BD=EF=BF=BD=EF=BF=BD=DA=96F=EF=BF=
=BD=EF=BF=BD=EF=BF=BD=7F=EF=BF=BD=EF=BF=BD\=EF=BF=BD=D0=9EI"t
>> =D3=BD=3Dn'=DB=AD=EF=BF=BD=EF=BF=BDZ=EF=BF=BD
>> =D7=AE6 =EF=BF=BD@,=EF=BF=BD=EF=BF=BD8=EF=BF=BD=EF=BF=BDR=3D=3D=3D
>
>
>
> -------------------------------------------------------
> Using Tomcat but need to do more? Need to support web services, securit=
y?
> Get stuff done quickly with pre-integrated technology to make your job=20
> easier
> Download IBM WebSphere Application Server v.1.0.1 based on Apache=20
> Geronimo
> http://sel.as-us.falkag.net/sel?cmd=3Dk&kid=120709&bid&3057&dat=121642
> [INVALID FOOTER]
>

--------------000407090801080401050209
Content-Type: text/html; charset=UTF-8
Content-Transfer-Encoding: quoted-printable

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
  <meta content=3D"text/html;charset=3DUTF-8" http-equiv=3D"Content-Type"=
>
  <title></title>
</head>
<body bgcolor=3D"#ffffff" text=3D"#000000">
Hi Jeff,<br>
<br>
Thanks for responding. I have written a simple Listener class that is
loaded at startup by Tomcat, which makes a static logger available to
the web-app (both as a static getter and a servlet context param), and
I have no problem using it from other areas such as jsp or servlets or
non-maverick pojo's. The logger itself is a simple custom jdk1.4 logger
(ie not log4j) and uses a very simple formatter. It uses a FileHandler
that points to a txt file in WEB-INF. No errors appear to be generated
and as far as I'm aware there are no weird classloader issues.<br>
<br>
here is the Listener that creates the logger:<br>
<br>
<font color=3D"#336666">public class SiteLogger implements
ServletContextListener {<br>
=C2=A0=C2=A0=C2=A0 <br>
=C2=A0=C2=A0=C2=A0 private static Logger logger;<br>
=C2=A0=C2=A0=C2=A0 <br>
=C2=A0=C2=A0=C2=A0 public static Logger getLogger() {<br>
=C2=A0=C2=A0=C2=A0 =C2=A0=C2=A0=C2=A0 return logger;<br>
=C2=A0=C2=A0=C2=A0 }</font><br>
<br>
<font color=3D"#336666">=C2=A0=C2=A0=C2=A0 public void
contextInitialized(ServletContextEvent e) {<br>
=C2=A0=C2=A0=C2=A0 =C2=A0=C2=A0=C2=A0 <br>
=C2=A0=C2=A0=C2=A0 =C2=A0=C2=A0=C2=A0 ServletContext sc =3D e.getServletC=
ontext();<br>
=C2=A0=C2=A0=C2=A0 =C2=A0=C2=A0=C2=A0 <br>
=C2=A0=C2=A0=C2=A0 =C2=A0=C2=A0=C2=A0 // get logger instance.<br>
=C2=A0=C2=A0=C2=A0 =C2=A0=C2=A0=C2=A0 logger =3D Logger.getLogger("global=
");<br>
=C2=A0=C2=A0=C2=A0 =C2=A0=C2=A0=C2=A0 // TODO: Should really be injected.=
<br>
=C2=A0=C2=A0=C2=A0 =C2=A0=C2=A0=C2=A0 logger.setLevel(Level.INFO);<br>
=C2=A0=C2=A0=C2=A0 =C2=A0=C2=A0=C2=A0 <br>
=C2=A0=C2=A0=C2=A0 =C2=A0=C2=A0=C2=A0 try {<br>
=C2=A0=C2=A0=C2=A0 =C2=A0=C2=A0=C2=A0 =C2=A0=C2=A0=C2=A0 FileHandler fh =3D=
 null;<br>
=C2=A0=C2=A0=C2=A0 =C2=A0=C2=A0=C2=A0 =C2=A0=C2=A0=C2=A0 String root =3D =
sc.getRealPath("/");<br>
=C2=A0=C2=A0=C2=A0 =C2=A0=C2=A0=C2=A0 =C2=A0=C2=A0=C2=A0 // TODO: Again v=
alue should be injected.<br>
=C2=A0=C2=A0=C2=A0 =C2=A0=C2=A0=C2=A0 =C2=A0=C2=A0=C2=A0 fh =3D new FileH=
andler(root+"WEB-INF/log.txt");<br>
=C2=A0=C2=A0=C2=A0 =C2=A0=C2=A0=C2=A0 =C2=A0=C2=A0=C2=A0 CustomFormatter =
cf =3D new CustomFormatter();<br>
=C2=A0=C2=A0=C2=A0 =C2=A0=C2=A0=C2=A0 =C2=A0=C2=A0=C2=A0 fh.setFormatter(=
cf);<br>
=C2=A0=C2=A0=C2=A0 =C2=A0=C2=A0=C2=A0 =C2=A0=C2=A0=C2=A0 <br>
=C2=A0=C2=A0=C2=A0 =C2=A0=C2=A0=C2=A0 =C2=A0=C2=A0=C2=A0 logger.addHandle=
r(fh);<br>
=C2=A0=C2=A0=C2=A0 =C2=A0=C2=A0=C2=A0 } catch(IOException ioe) {<br>
=C2=A0=C2=A0=C2=A0 =C2=A0=C2=A0=C2=A0 =C2=A0=C2=A0=C2=A0 System.err.print=
ln("Can't load logger" + ioe.getMessage());<br>
=C2=A0=C2=A0=C2=A0 =C2=A0=C2=A0=C2=A0 }<br>
=C2=A0=C2=A0=C2=A0 =C2=A0=C2=A0=C2=A0 sc.setAttribute("fw.logging.SiteLog=
ger", logger);<br>
=C2=A0=C2=A0=C2=A0 }<br>
...etc<br>
</font><br>
here is the web.xml entry for the listener:<br>
<br>
<font color=3D"#336666">=C2=A0=C2=A0=C2=A0 =C2=A0 &lt;listener&gt;<br>
=C2=A0=C2=A0=C2=A0 =C2=A0 =C2=A0=C2=A0=C2=A0 =C2=A0
&lt;listener-class&gt;fw.logging.SiteLogger&lt;/listener-class&gt;<br>
=C2=A0=C2=A0=C2=A0 =C2=A0 &lt;/listener&gt;</font><br>
<br>
here is the code that does the logging in the controller's perform()
method:<br>
<font color=3D"#660000"><br>
<font color=3D"#336666">import fw.logging.SiteLogger;<br>
...<br>
SiteLogger.getLogger().log(Level.INFO, obj);</font></font><br>
<br>
LM<br>
<br>
Jeff Schnitzer wrote:
<blockquote cite=3D"[email protected]" type=3D"cite">Unf=
ortunately
I'm pretty sure that's going to be far too general a question.=C2=A0 You
need to explain how your logging framework works if anyone's going to
be able to help you.=C2=A0 Does it do anything weird with classloaders?
  <br>
  <br>
Jeff
  <br>
  <br>
Louis Malenica wrote:
  <br>
  <blockquote type=3D"cite">Hi,
    <br>
    <br>
I am trying to log via a standard custom logger from within the
    <br>
perform() method of a controller that extends ThrowawayBean2. HOwever
    <br>
nothing is being logged. My logging framework seems ok as it is
    <br>
logging elsewhere in the app. Anyone come across this before?
    <br>
    <br>
thanks.
    <br>
    <br>
LM
    <br>
R=C8=A7=EF=BF=BD:&amp;q=EF=BF=BD[=EF=BF=BD=EF=BF=BD=EF=BF=BDy=EF=BF=BDhv=EF=
=BF=BD=EF=BF=BD=EF=BF=BD=EF=BF=BD^y=EF=BF=BDh=EF=BF=BD=EF=BF=BDi=EF=BF=BD=
=EF=BF=BDpy=EF=BF=BD=1E=EF=BF=BD=EF=BF=BDz=EF=BF=BD=1Er=EF=BF=BD=EF=BF=BD=
!=EF=BF=BD=EF=BF=BD=EF=BF=BDn}=EF=BF=BDh=EF=BF=BD=EA=AE=89=EF=BF=BD%=EF=BF=
=BD=08=EF=BF=BD=EF=BF=BD=EF=BF=BD=DE=8A{^=EF=BF=BD=EF=BF=BD=EF=BF=BDy=EF=BF=
=BD^r=19
=E8=96=882=EF=BF=BD=EF=BF=BD=EF=BF=BD=EF=BF=BD=EC=A8=BA=EF=BF=BD=EF=BF=BD=
m=E6=AC=89=EF=BF=BD=C3=A3=C2=A0=C2=A0=C2=A0 =E5=A1=A7H=04=C5=9Em*az=EF=BF=
=BD=EF=BF=BD=EF=BF=BD=EF=BF=BDbq=EF=BF=BDb=EF=BF=BDt=EF=BF=BD=EF=BF=BD=EF=
=BF=BD]5m=EF=BF=BD=1Ev=EF=BF=BD=EF=BF=BD=EF=BF=BD!xg=EF=BF=BD=EF=BF=BDx=EF=
=BF=BD=EF=BF=BD=1B
m=EF=BF=BD=EF=BF=BD=EF=BF=BDzV=EF=BF=BD=EF=BF=BD=EF=BF=BD=DA=96F=EF=BF=BD=
=EF=BF=BD=EF=BF=BD=7F=EF=BF=BD=EF=BF=BD\=EF=BF=BD=D0=9EI"t
    <br>
=D3=BD=3Dn'=DB=AD=EF=BF=BD=EF=BF=BDZ=EF=BF=BD
    <br>
=D7=AE6 =EF=BF=BD@,=EF=BF=BD=EF=BF=BD8=EF=BF=BD=EF=BF=BDR=3D=3D=3D
    <br>
  </blockquote>
  <br>
  <br>
  <br>
-------------------------------------------------------
  <br>
Using Tomcat but need to do more? Need to support web services,
security?
  <br>
Get stuff done quickly with pre-integrated technology to make your job
easier
  <br>
Download IBM WebSphere Application Server v.1.0.1 based on Apache
Geronimo
  <br>
<a class=3D"moz-txt-link-freetext" href=3D"http://sel.as-us.falkag.net/se=
l?cmd=3Dk&kid=120709&bid&3057&dat=121642">http://sel.as-us.falkag.net/sel=
?cmd=3Dk&amp;kid=120709&amp;bid&amp;3057&amp;dat=121642</a>
  <br>
[INVALID FOOTER]
  <br>
  <br>
</blockquote>
</body>
</html>

--------------000407090801080401050209--


-------------------------------------------------------
Using Tomcat but need to do more? Need to support web services, security?
Get stuff done quickly with pre-integrated technology to make your job easier
Download IBM WebSphere Application Server v.1.0.1 based on Apache Geronimo
http://sel.as-us.falkag.net/sel?cmd=lnk&kid=120709&bid=263057&dat=121642
[INVALID FOOTER]