You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@tomcat.apache.org by Christopher Schultz <ch...@christopherschultz.net> on 2010/03/02 18:41:57 UTC

Re: Access Log /Filter/?

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Xie,

On 2/26/2010 4:33 PM, Xie Xiaodong wrote:
> No, there is no AccessLogFilter in Tomcat 7 for now. I've got my
> version of AccessLogFilter during Google Summer Code 2009, but has
> not yet submit it for some reason.

Uh, why not submit it and get paid?

> Please attach your version so we could discuss it and make an
> AccessLogFilter in Tomcat 7.

I don't have a version, yet. I was checking to see if someone had done
it already. But if I give you my code so Google can pay you, I'd like
some of that money :)

- -chris
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.10 (MingW32)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org/

iEYEARECAAYFAkuNTeUACgkQ9CaO5/Lv0PCOOwCeM78myOdUfyyHxTTrbQloeb3W
Ja8AnA4gDir14UAIIoG9hoc8YcKMEeJ6
=nst1
-----END PGP SIGNATURE-----

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
For additional commands, e-mail: users-help@tomcat.apache.org


Re: Access Log /Filter/?

Posted by Christopher Schultz <ch...@christopherschultz.net>.
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Xie,

On 3/2/2010 6:20 PM, Xie Xiaodong wrote:
> Second, you are absolutely right about the log.info(....). I first
> wrote like this for testing and forgot to get it back to debug
> level.

Don't forget that calling log.debug() with a bunch of string
concatenations still performs those string concatenations. It's better
to do something like this:

if(log.isDebugEnabled())
  log.debug(something + something else + a third thing);

> In modern jvm, it does not matter much between StringBuffer and 
> StringBuilder, jvm will change StringBuffer used in single thread
> scenario into StringBuilder automaticlly.

No, it won't: if you ask for StringBuffer, you'll get a StringBuffer. If
you just do "a" + "b", the /compiler/ will use StringBuilder if your
target is 1.5+ but the JVM doesn't do anything like what you describe.

> You could google this information. There are some benchmark test
> about it.

I'd love to see an example demonstrating your claim.

- -chis
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.10 (MingW32)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org/

iEUEARECAAYFAkuOid0ACgkQ9CaO5/Lv0PD7FQCYjz53of2knok9gwKfyShrecka
JACgjWY1WBpOYTSTJJ5dlDOO5BAnJJ4=
=8yWQ
-----END PGP SIGNATURE-----

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
For additional commands, e-mail: users-help@tomcat.apache.org


Re: Access Log /Filter/?

Posted by Xie Xiaodong <xx...@gmail.com>.
Thank the comments. I should have rechecked this file before I sent it here.
:)

First, for the init() part: in the super class FilterBase, we have a init()
method which will do the initialization work you mentioned.

Second, you are absolutely right about the log.info(....). I first wrote
like this for testing and forgot to get it back to debug level.

In modern jvm, it does not matter much between StringBuffer and
StringBuilder, jvm will change StringBuffer used in single
thread scenario into StringBuilder automaticlly. You could google this
information. There are some benchmark test about it.

I'll check the remaining part tomorrow morning. It is rather late now.

Wish you have a nice day.

Thanks for the comments.


On Tue, Mar 2, 2010 at 11:51 PM, Christopher Schultz <
chris@christopherschultz.net> wrote:

> -----BEGIN PGP SIGNED MESSAGE-----
> Hash: SHA1
>
> Xie,
>
> On 3/2/2010 3:58 PM, Xie Xiaodong wrote:
> > public class AccessLogFilter
> >     extends FilterBase {
>
> For the most part, you've just replaced the invoke() method with a
> doFilter() method and introduced init(), which calls start(). Then, you
> removed all the Lifecycle stuff.
>
> How will Filters be configured in TC7? If they will use the same
>
> <filter>
>  <init-param>
>
> ...style of configuration, then you'll need to have your init() method
> fetch all the init-param values and set those values on the
> AccessLogFilter before calling start().
>
> I might just re-name start() to init() and make whatever changes are
> necessary.
>
> >     private static Log log = LogFactory.getLog(AccessLogFilter.class);
>
> Oh, the irony.
>
> > public void doFilter(ServletRequest request, ServletResponse response,
> > FilterChain chain) throws IOException, ServletException {
> >
> > log.info("In AccessLogFilter doFilter. ");
>
> Obviously, you can't have this log message at the INFO level. I'll
> assume that all logging currently in the code is for development
> purposes and will be removed for a final version.
>
> >  if (!isHttpServlet(request, response)) {
> >             chain.doFilter(request, response);
> >             return;
> >         }
>
> Why does the request have to be an HTTP request in order to have the
> access log run? I would flip the logic around to only do the logging if
> the request is an HTTP request, rather than having this mid-method return.
>
> >  HttpServletRequest httpRequest = ((HttpServletRequest) request);
> >
> > HttpServletResponse httpResponse = ((HttpServletResponse) response);
> >
> > if (started && getEnabled()) {
> > // Pass this request on to the next filter in the filterChain
> > long t1 = System.currentTimeMillis();
> >
> > chain.doFilter(request, response);
> >
> > long t2 = System.currentTimeMillis();
> > long time = t2 - t1;
>
> This isn't your choice, it's in the original code, but why not just do:
>
> long elapsed = System.currentTimeMillis();
> ...
> elapsed = System.currentTimeMillis() - elapsed;
>
> ??
>
> Fewer items on the stack, etc.
>
> > Date date = getDate();
> > StringBuffer result = new StringBuffer();
>
> The original default size for the StringBuffer was 128 characters.
> Presumably, this was done to avoid the cost of re-sizing the
> StringBuffer whenever it grew too large. The default initial capacity of
> a StringBuffer is 16 characters, which is almost certainly too small for
> a reasonable access log entry. You should put the larger size back in.
>
> While you're at it, use StringBuilder instead to avoid the overhead of
> synchronization for an object in a single-threaded environment.
>
> >     public void log(String message) {
>
> Maybe re-write this method to avoid having to convert from
> StringBuffer/StringBuilder to String: why do the work if you don't have to?
>
> >     private static String lookup(String month) {
> >         int index;
> >         try {
> >             index = Integer.parseInt(month) - 1;
>
> Why not have a dummy "month" at index 0 and /not/ subtract? Come on...
> we're smarter than the Sun engineers, right? 0 = January? Stupid...
>
> >     private Date getDate() {
> >         // Only create a new Date once per second, max.
> >         long systime = System.currentTimeMillis();
> >         AccessDateStruct struct = currentDateStruct.get();
> >         if ((systime - struct.currentDate.getTime()) > 1000) {
> >             struct.currentDate.setTime(systime);
> >             struct.currentDateString = null;
> >         }
> >         return struct.currentDate;
> >     }
>
> I don't understand why this is ThreadLocal, instead of just synchronized
> across the object. Maybe it's slightly faster to avoid the
> synchronization and just use ThreadLocals, but I'm not sure how many
> requests per second a single Thread is going to process, so I'm not
> convinced that caching this data is worth the complexity it requires in
> this class. I'd love to hear from a Tomcat dev about this.
>
> >     protected static class ByteSentElement implements AccessLogElement {
> >         private boolean conversion;
> >
> >         /**
> >          * if conversion is true, write '-' instead of 0 - %b
> >          */
> >         public ByteSentElement(boolean conversion) {
> >             this.conversion = conversion;
> >         }
> >
> >         public void addElement(StringBuffer buf, Date date,
> > HttpServletRequest request, HttpServletResponse response,
> > long time) {
> >             long length = 0;
> >             if (response.getHeader("Content-Length") != null
> >                     && !"".equals(response.getHeader("Content-Length")))
> {
> >                 length =
> > Long.parseLong(response.getHeader("Content-Length"));
> >             }
> >             if (length <= 0 && response.getHeader("content-length") !=
> null
> >                     && !"".equals(response.getHeader("content-length")))
> {
> >                 length =
> > Long.parseLong(response.getHeader("content-length"));
> >             }
> > if (length <= 0 && conversion) {
> > buf.append('-');
> > } else {
> > buf.append(length);
> > }
> >         }
> >     }
>
> Note that this class will only report what was sent with the
> Content-Length header, rather than actually counting the bytes that were
> sent. Fixing this requires an architectural change: the BytesSentElement
> must be able to observe the OutputStream/Writer used by the servlet and
> count the bytes that were sent.
>
> Also, this method can cause an error if the Content-Type exceeds 2^31-1,
> which is bad. :( Why bother parsing the Content-Length in this case?
>
> This leads to another question: if the class is "improved" to actually
> count bytes, how will you count higher than 2^31 - 1?
>
> >     protected AccessLogElement[] createLogElements() {
>
> This method (historical, I know) seems way more complicated than it
> really needs to be.
>
> For instance, there's no need to keep a separate "buf" buffer just to
> keep track of non-patterned text: just keep an index to the start and
> end positions of the text, then create a "StringElement" out of that: no
> need to create lots of StringBuffer objects for this.
>
> Also, if desired, it would be almost trivial to create a pluggable
> LogElement capability into this class with a set of defaults equal to
> the current behavior: instead of using hard-coded switch statements to
> determine which pattern letter corresponds to which LogElement class, a
> registry could be used which would shorten-up the code a bit, too.
>
> So, those are my thoughts on this class.
>
> - -chris
> -----BEGIN PGP SIGNATURE-----
> Version: GnuPG v1.4.10 (MingW32)
> Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org/
>
> iEYEARECAAYFAkuNloUACgkQ9CaO5/Lv0PD2IQCgtLggELEMQw8iNYf+gXOnnm9B
> 3cAAoKZL2qIu+LxsYHUoCC3N1L3cA50s
> =aLcF
> -----END PGP SIGNATURE-----
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
> For additional commands, e-mail: users-help@tomcat.apache.org
>
>


-- 
Sincerely yours and Best Regards,
Xie Xiaodong

RE: Access Log /Filter/?

Posted by "Caldarale, Charles R" <Ch...@unisys.com>.
> From: Xie Xiaodong [mailto:xxd82329@gmail.com]
> Subject: Re: Access Log /Filter/?
> 
> http://www.javalobby.org/java/forums/t88518.html

I think you're cherry-picking the data.  The most useful comment about StringBuilder in that thread was this:

"Re: StringBuffer
> Thus, the performance of the two classes becomes identical.

Reality is more complicated than that.
- You can't rely on latest/hardest optimizations in the HotSpot Client VM, which is the VM you gotta use for most desktop apps, games and others.
- Optimizations are not portable. Just because Sun has optimizations X it doesn't mean that IBM, BEA, GCJ etc. (even at the same Java SE level) have it too, and vice-versa.
- Optimizations are fragile and difficult to predict. Say you have a StringBuffer that is non-escaping (thus benefits from lock elision etc) because it's a local var of a single method. Now if this method grows too big and you refactor it into multiple smaller, private helper methods, suddenly the StringBuffer is passed from one method to another and it's not anymore a non-escaping local... unless, thanks to inlining or smarter escape analysis, the optimized code can handle it again as non-escaping... but now you're depending on a combination of several optimizations, and it's increasingly difficult (at least for the average developer) to predict the compiler's behavior and to rely on its behavior.

Conclusion (for this particular optimization): Using StringBuilder is no more complex than StringBuffer, so there's no excuse to not use it when you now the buffer is unshared. Unlike some other optimizations, this doesn't confuse your code, doesn't create maintenance problems or incur increased development cost. in cases like this - where writing optimal code is just as easy as non-optimal code - I'd optimize since version 0."

 - Chuck


THIS COMMUNICATION MAY CONTAIN CONFIDENTIAL AND/OR OTHERWISE PROPRIETARY MATERIAL and is thus for use only by the intended recipient. If you received this in error, please contact the sender and delete the e-mail and its attachments from all computers.


---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
For additional commands, e-mail: users-help@tomcat.apache.org


Re: Access Log /Filter/?

Posted by Christopher Schultz <ch...@christopherschultz.net>.
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Xie,

On 3/3/2010 11:54 AM, Xie Xiaodong wrote:
> I think log.debug() method should first check current logging levels, or our
> code will have those if() {} template everywhere.

[snip]

> For log.debug() part, seems I misunderstood your meaning. Sorry about that,
> you are right. But I do not think it matters too much. :)

Well, if were using tricks to avoid synchronization, we probably ought
to avoid four object creations (StringBuilder, char[] in sb, String, and
char[] in s) plus the character-copy operations. This is a filter that
is supposed to avoid performance impact on the webapp: this is such a
cheap optimization, it would be foolish not to do it.

> Java 6 hotspot can determine that the StringBuffer synchronization isn't
> actually used across threads in many cases, and thus it doesn't bother
> synchronizing. Thus, the performance of the two classes becomes identical.
> 
> http://www.javalobby.org/java/forums/t88518.html

It's a rough reference from a comment on a blog. Gimmie something I can
really read, not "I remember a guy saying once..".

Jeremy Manson says this:
"
A final note. Some people think that there is no performance impact to
using StringBuffer instead of StringBuilder, because of all of the
clever things JVMs do to eliminate synchronization (I can blog about
this, too, if you want). This is a little unclear. Whether it can even
perform these optimizing transformations definitely depends wildly on
which JDK you use; I wrote a microbenchmark to test it, and the results
differed on different JDKs -- but all that microbenchmarks really
demonstrate is the performance of the microbenchmark.
"
(http://jeremymanson.blogspot.com/2008/08/dont-use-stringbuffer.html
from 24 August 2008)

> But it is more secure to not depend on specific jvm version, so it is more
> appropriate to use StringBuilder when necessary.

I agree. Using my own microbenchmark (below), these are the results I get:

(On my OpenVZ dev box, Linux 2.6 kernel)
$ java -showversion BufferVsBuilder 10000000
java version "1.6.0_12"
Java(TM) SE Runtime Environment (build 1.6.0_12-b04)
Java HotSpot(TM) Server VM (build 11.2-b01, mixed mode)

Priming...
Running...............
Builder:   24538
Buffer:    25745
Overhead:  1657

$ java -showversion BufferVsBuilder 10000000
java version "1.6.0_12"
Java(TM) SE Runtime Environment (build 1.6.0_12-b04)
Java HotSpot(TM) Server VM (build 11.2-b01, mixed mode)

Priming...
Running...............
Builder:   24175
Buffer:    25416
Overhead:  1656


(On my laptop, W7 32-bit)
F:\Users\Chris\Desktop>java -showversion BufferVsBuilder 10000000
java version "1.6.0_18"
Java(TM) SE Runtime Environment (build 1.6.0_18-b07)
Java HotSpot(TM) Client VM (build 16.0-b13, mixed mode, sharing)

Priming...
Running...............
Builder:   27507
Buffer:    31525
Overhead:  1183

F:\Users\Chris\Desktop>java -showversion BufferVsBuilder 10000000
java version "1.6.0_18"
Java(TM) SE Runtime Environment (build 1.6.0_18-b07)
Java HotSpot(TM) Client VM (build 16.0-b13, mixed mode, sharing)

Priming...
Running...............
Builder:   27339
Buffer:    31346
Overhead:  1116

It seems that, in these two environments, StringBuilder outperforms
StringBuffer by a small margin (5% on average in my Linux environment,
13% in my Microsoft Windows environment), but consistently.

It seems that the JVM has to do some work to even determine if the
synchronization can be eliminated before it can actually do it, so why
bother making it do that work in the first place? Avoiding
synchronization is simply the right choice.

- -chris

The code:
public class BufferVsBuilder
{
    static long elapsedBuilder = 0;
    static long elapsedBuffer = 0;
    static long elapsedNothing = 0;

    public static void testBuilder(long iterations)
    {
        long elapsed = System.currentTimeMillis();

        for(long i=iterations; i>0; --i)
        {
            StringBuilder sb = new StringBuilder(128);
            sb.append("some text")
                .append("some text")
                .append("some text")
                .append("some text")
                .append("some text")
                .append("some text")
                .append("some text")
                .append("some text")
                .append("some text")
                .append("some text")
                .append("some text")
                .append("some text")
                ;
        }

        elapsed = System.currentTimeMillis() - elapsed;

        elapsedBuilder += elapsed;
    }

    public static void testBuffer(long iterations)
    {
        long elapsed = System.currentTimeMillis();

        for(long i=iterations; i>0; --i)
        {
            StringBuffer sb = new StringBuffer(128);
            sb.append("some text")
                .append("some text")
                .append("some text")
                .append("some text")
                .append("some text")
                .append("some text")
                .append("some text")
                .append("some text")
                .append("some text")
                .append("some text")
                .append("some text")
                .append("some text")
                ;
        }

        elapsed = System.currentTimeMillis() - elapsed;

        elapsedBuffer += elapsed;
    }

    public static void testNothing(long iterations)
    {
        long elapsed = System.currentTimeMillis();

        for(long i=iterations; i>0; --i)
        {
            StringBuffer sb = new StringBuffer(128);
        }

        elapsed = System.currentTimeMillis() - elapsed;

        elapsedNothing += elapsed;
    }


    public static void main(String[] args)
    {
        long iterations = Long.parseLong(args[0]);

        System.out.println("Priming..."); System.out.flush();
        // Allow the JIT to work it's magic
        testBuilder(iterations);
        testBuffer(iterations);

        // Re-set counters and start the real tests
        elapsedBuilder = 0;
        elapsedBuffer = 0;

        System.out.print("Running..."); System.out.flush();
        testBuilder(iterations);
        System.out.print("."); System.out.flush();
        testBuffer(iterations);

        System.out.print("."); System.out.flush();
        testBuilder(iterations);
        System.out.print("."); System.out.flush();
        testBuffer(iterations);

        System.out.print("."); System.out.flush();
        testBuilder(iterations);
        System.out.print("."); System.out.flush();
        testBuffer(iterations);

        System.out.print("."); System.out.flush();
        testBuilder(iterations);
        System.out.print("."); System.out.flush();
        testBuffer(iterations);

        System.out.print("."); System.out.flush();
        testBuilder(iterations);
        System.out.print("."); System.out.flush();
        testBuffer(iterations);

        System.out.print("."); System.out.flush();
        testBuilder(iterations);
        System.out.print("."); System.out.flush();
        testBuffer(iterations);

        System.out.print("."); System.out.flush();
        testNothing(iterations);

        System.out.println();
        System.out.println("Builder:   " + elapsedBuilder);
        System.out.println("Buffer:    " + elapsedBuffer);
        System.out.println("Overhead:  " + elapsedNothing);
    }
}
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.10 (MingW32)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org/

iEYEARECAAYFAkuOrj0ACgkQ9CaO5/Lv0PCAbgCeLkbndgo1VsC/M+e2dGfVy3Fl
4dMAoKCHnBRwZvayRdSPXibVxFI7Hes3
=k26e
-----END PGP SIGNATURE-----

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
For additional commands, e-mail: users-help@tomcat.apache.org


Re: Access Log /Filter/?

Posted by Xie Xiaodong <xx...@gmail.com>.
Hello, Christopher,


For log.debug() part, seems I misunderstood your meaning. Sorry about that,
you are right. But I do not think it matters too much. :)



On Wed, Mar 3, 2010 at 5:54 PM, Xie Xiaodong <xx...@gmail.com> wrote:

> Hello,
>
>
> I think log.debug() method should first check current logging levels, or
> our code will have those if() {} template everywhere.
>
> I checked java.util.logging.Logger, and found this:
>
>     public void log(Level level, String msg, Object param1) {
> if (level.intValue() < levelValue || levelValue == offValue) {
>     return;
> }
> LogRecord lr = new LogRecord(level, msg);
>  Object params[] = { param1 };
> lr.setParameters(params);
> doLog(lr);
>     }
>
>
> Java 6 hotspot can determine that the StringBuffer synchronization isn't
> actually used across threads in many cases, and thus it doesn't bother
> synchronizing. Thus, the performance of the two classes becomes identical.
>
> http://www.javalobby.org/java/forums/t88518.html
>
> But it is more secure to not depend on specific jvm version, so it is more
> appropriate to use StringBuilder when necessary.
>
>
> On Wed, Mar 3, 2010 at 5:19 PM, Christopher Schultz <
> chris@christopherschultz.net> wrote:
>
>> -----BEGIN PGP SIGNED MESSAGE-----
>> Hash: SHA1
>>
>> Jason,
>>
>> On 3/2/2010 7:21 PM, Jason Brittain wrote:
>> >> Why does the request have to be an HTTP request in order to have the
>> >> access log run?
>> >
>> > That does seem to be a bug.
>>
>> Note that this is not actually a part of the AccessLogValve, it's just
>> part of Xie's implementation.
>>
>> > By default, the access logger logs the common
>> > log web server
>> > format, but of course it doesn't have to, so it should log non-HTTP
>> requests
>> > as well, but maybe
>> > only if a non-default pattern is configured?
>>
>> Fair enough: most of the information you'd want to log is from HTTP
>> requests (like the URI, for instance). The only things that are
>> available for non-HTTP requests are:
>>
>> - - current date/time
>> - - transaction time
>> - - number of bytes read and sent
>> - - local address
>> - - remote address
>> - - request attributes
>> - - server name
>>
>> Actually, that's quite a bit, but I've never seen an HTTP log that
>> doesn't log the URI :)
>>
>> >>> long t2 = System.currentTimeMillis();
>> >>> long time = t2 - t1;
>> >>
>> >> This isn't your choice, it's in the original code, but why not just do:
>> >>
>> >> long elapsed = System.currentTimeMillis();
>> >> ...
>> >> elapsed = System.currentTimeMillis() - elapsed;
>> >>
>> >> ??
>> >>
>> >> Fewer items on the stack, etc.
>> >>
>> >
>> > Except that then it is more difficult to debug.  Right?  It isn't as
>> easy to
>> > inspect the value of
>> > the current time if you perform the subtraction without first assigning
>> the
>> > current time to a
>> > variable.
>>
>> Fair enough, though debugging this timing code shouldn't really be
>> required.
>>
>> >>  >     private Date getDate() {
>> >>>         // Only create a new Date once per second, max.
>> >>>         long systime = System.currentTimeMillis();
>> >>>         AccessDateStruct struct = currentDateStruct.get();
>> >>>         if ((systime - struct.currentDate.getTime()) > 1000) {
>> >>>             struct.currentDate.setTime(systime);
>> >>>             struct.currentDateString = null;
>> >>>         }
>> >>>         return struct.currentDate;
>> >>>     }
>> >>
>> >> I don't understand why this is ThreadLocal, instead of just
>> synchronized
>> >> across the object. Maybe it's slightly faster to avoid the
>> >> synchronization and just use ThreadLocals, but I'm not sure how many
>> >> requests per second a single Thread is going to process, so I'm not
>> >> convinced that caching this data is worth the complexity it requires in
>> >> this class. I'd love to hear from a Tomcat dev about this.
>> >>
>> >
>> > Tomcat can (hopefully) answer a larger number of requests per second
>> > every year on decently modern hardware.  Benchmark it both ways on
>> > a reasonably good/wide machine and you'll see why avoiding the sync
>> > is helpful.  I don't think it muddies the code very badly here.
>>
>> Okay. Certainly avoiding object creation is a good idea, and avoiding
>> highly-contended synchronization is a good idea, too. I'd like to see a
>> performance comparison between these strategies, though. Maybe I'll run
>> one :)
>>
>> > The %b portion of the Combined Log Format is documented to be the "size
>> of
>> > the object returned to the client, not including the response headers."
>> > http://httpd.apache.org/docs/1.3/logs.html#common
>>
>> Right. Presumably, the Content-Length is synonymous with the above, but
>> it might not be. Also, Content-Length is not always set, so you'll get a
>> lot of "-" written in the log even when response bodies actually has
>> content. In this implementation, "%b" is equivalent to
>> "%{Content-Length}o".
>>
>> Counting bytes isn't that big of a deal, either. I'll submit a patch at
>> some point... maybe using a different pattern character.
>>
>> - -chris
>> -----BEGIN PGP SIGNATURE-----
>> Version: GnuPG v1.4.10 (MingW32)
>> Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org/
>>
>> iEYEARECAAYFAkuOjCAACgkQ9CaO5/Lv0PAAmgCgt9QVocFjXVNB3t4ib6fWOUIL
>> ++YAoKdpBuT1uhobAIxasRsdw/PaK00e
>> =zS1q
>> -----END PGP SIGNATURE-----
>>
>> ---------------------------------------------------------------------
>> To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
>> For additional commands, e-mail: users-help@tomcat.apache.org
>>
>>
>
>
> --
> Sincerely yours and Best Regards,
> Xie Xiaodong
>



-- 
Sincerely yours and Best Regards,
Xie Xiaodong

Re: Access Log /Filter/?

Posted by Xie Xiaodong <xx...@gmail.com>.
Hello,


I think log.debug() method should first check current logging levels, or our
code will have those if() {} template everywhere.

I checked java.util.logging.Logger, and found this:

    public void log(Level level, String msg, Object param1) {
if (level.intValue() < levelValue || levelValue == offValue) {
    return;
}
LogRecord lr = new LogRecord(level, msg);
Object params[] = { param1 };
lr.setParameters(params);
doLog(lr);
    }


Java 6 hotspot can determine that the StringBuffer synchronization isn't
actually used across threads in many cases, and thus it doesn't bother
synchronizing. Thus, the performance of the two classes becomes identical.

http://www.javalobby.org/java/forums/t88518.html

But it is more secure to not depend on specific jvm version, so it is more
appropriate to use StringBuilder when necessary.


On Wed, Mar 3, 2010 at 5:19 PM, Christopher Schultz <
chris@christopherschultz.net> wrote:

> -----BEGIN PGP SIGNED MESSAGE-----
> Hash: SHA1
>
> Jason,
>
> On 3/2/2010 7:21 PM, Jason Brittain wrote:
> >> Why does the request have to be an HTTP request in order to have the
> >> access log run?
> >
> > That does seem to be a bug.
>
> Note that this is not actually a part of the AccessLogValve, it's just
> part of Xie's implementation.
>
> > By default, the access logger logs the common
> > log web server
> > format, but of course it doesn't have to, so it should log non-HTTP
> requests
> > as well, but maybe
> > only if a non-default pattern is configured?
>
> Fair enough: most of the information you'd want to log is from HTTP
> requests (like the URI, for instance). The only things that are
> available for non-HTTP requests are:
>
> - - current date/time
> - - transaction time
> - - number of bytes read and sent
> - - local address
> - - remote address
> - - request attributes
> - - server name
>
> Actually, that's quite a bit, but I've never seen an HTTP log that
> doesn't log the URI :)
>
> >>> long t2 = System.currentTimeMillis();
> >>> long time = t2 - t1;
> >>
> >> This isn't your choice, it's in the original code, but why not just do:
> >>
> >> long elapsed = System.currentTimeMillis();
> >> ...
> >> elapsed = System.currentTimeMillis() - elapsed;
> >>
> >> ??
> >>
> >> Fewer items on the stack, etc.
> >>
> >
> > Except that then it is more difficult to debug.  Right?  It isn't as easy
> to
> > inspect the value of
> > the current time if you perform the subtraction without first assigning
> the
> > current time to a
> > variable.
>
> Fair enough, though debugging this timing code shouldn't really be
> required.
>
> >>  >     private Date getDate() {
> >>>         // Only create a new Date once per second, max.
> >>>         long systime = System.currentTimeMillis();
> >>>         AccessDateStruct struct = currentDateStruct.get();
> >>>         if ((systime - struct.currentDate.getTime()) > 1000) {
> >>>             struct.currentDate.setTime(systime);
> >>>             struct.currentDateString = null;
> >>>         }
> >>>         return struct.currentDate;
> >>>     }
> >>
> >> I don't understand why this is ThreadLocal, instead of just synchronized
> >> across the object. Maybe it's slightly faster to avoid the
> >> synchronization and just use ThreadLocals, but I'm not sure how many
> >> requests per second a single Thread is going to process, so I'm not
> >> convinced that caching this data is worth the complexity it requires in
> >> this class. I'd love to hear from a Tomcat dev about this.
> >>
> >
> > Tomcat can (hopefully) answer a larger number of requests per second
> > every year on decently modern hardware.  Benchmark it both ways on
> > a reasonably good/wide machine and you'll see why avoiding the sync
> > is helpful.  I don't think it muddies the code very badly here.
>
> Okay. Certainly avoiding object creation is a good idea, and avoiding
> highly-contended synchronization is a good idea, too. I'd like to see a
> performance comparison between these strategies, though. Maybe I'll run
> one :)
>
> > The %b portion of the Combined Log Format is documented to be the "size
> of
> > the object returned to the client, not including the response headers."
> > http://httpd.apache.org/docs/1.3/logs.html#common
>
> Right. Presumably, the Content-Length is synonymous with the above, but
> it might not be. Also, Content-Length is not always set, so you'll get a
> lot of "-" written in the log even when response bodies actually has
> content. In this implementation, "%b" is equivalent to
> "%{Content-Length}o".
>
> Counting bytes isn't that big of a deal, either. I'll submit a patch at
> some point... maybe using a different pattern character.
>
> - -chris
> -----BEGIN PGP SIGNATURE-----
> Version: GnuPG v1.4.10 (MingW32)
> Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org/
>
> iEYEARECAAYFAkuOjCAACgkQ9CaO5/Lv0PAAmgCgt9QVocFjXVNB3t4ib6fWOUIL
> ++YAoKdpBuT1uhobAIxasRsdw/PaK00e
> =zS1q
> -----END PGP SIGNATURE-----
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
> For additional commands, e-mail: users-help@tomcat.apache.org
>
>


-- 
Sincerely yours and Best Regards,
Xie Xiaodong

Re: Access Log /Filter/?

Posted by Christopher Schultz <ch...@christopherschultz.net>.
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Jason,

On 3/2/2010 7:21 PM, Jason Brittain wrote:
>> Why does the request have to be an HTTP request in order to have the
>> access log run?
> 
> That does seem to be a bug.

Note that this is not actually a part of the AccessLogValve, it's just
part of Xie's implementation.

> By default, the access logger logs the common
> log web server
> format, but of course it doesn't have to, so it should log non-HTTP requests
> as well, but maybe
> only if a non-default pattern is configured?

Fair enough: most of the information you'd want to log is from HTTP
requests (like the URI, for instance). The only things that are
available for non-HTTP requests are:

- - current date/time
- - transaction time
- - number of bytes read and sent
- - local address
- - remote address
- - request attributes
- - server name

Actually, that's quite a bit, but I've never seen an HTTP log that
doesn't log the URI :)

>>> long t2 = System.currentTimeMillis();
>>> long time = t2 - t1;
>>
>> This isn't your choice, it's in the original code, but why not just do:
>>
>> long elapsed = System.currentTimeMillis();
>> ...
>> elapsed = System.currentTimeMillis() - elapsed;
>>
>> ??
>>
>> Fewer items on the stack, etc.
>>
> 
> Except that then it is more difficult to debug.  Right?  It isn't as easy to
> inspect the value of
> the current time if you perform the subtraction without first assigning the
> current time to a
> variable.

Fair enough, though debugging this timing code shouldn't really be required.

>>  >     private Date getDate() {
>>>         // Only create a new Date once per second, max.
>>>         long systime = System.currentTimeMillis();
>>>         AccessDateStruct struct = currentDateStruct.get();
>>>         if ((systime - struct.currentDate.getTime()) > 1000) {
>>>             struct.currentDate.setTime(systime);
>>>             struct.currentDateString = null;
>>>         }
>>>         return struct.currentDate;
>>>     }
>>
>> I don't understand why this is ThreadLocal, instead of just synchronized
>> across the object. Maybe it's slightly faster to avoid the
>> synchronization and just use ThreadLocals, but I'm not sure how many
>> requests per second a single Thread is going to process, so I'm not
>> convinced that caching this data is worth the complexity it requires in
>> this class. I'd love to hear from a Tomcat dev about this.
>>
> 
> Tomcat can (hopefully) answer a larger number of requests per second
> every year on decently modern hardware.  Benchmark it both ways on
> a reasonably good/wide machine and you'll see why avoiding the sync
> is helpful.  I don't think it muddies the code very badly here.

Okay. Certainly avoiding object creation is a good idea, and avoiding
highly-contended synchronization is a good idea, too. I'd like to see a
performance comparison between these strategies, though. Maybe I'll run
one :)

> The %b portion of the Combined Log Format is documented to be the "size of
> the object returned to the client, not including the response headers."
> http://httpd.apache.org/docs/1.3/logs.html#common

Right. Presumably, the Content-Length is synonymous with the above, but
it might not be. Also, Content-Length is not always set, so you'll get a
lot of "-" written in the log even when response bodies actually has
content. In this implementation, "%b" is equivalent to "%{Content-Length}o".

Counting bytes isn't that big of a deal, either. I'll submit a patch at
some point... maybe using a different pattern character.

- -chris
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.10 (MingW32)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org/

iEYEARECAAYFAkuOjCAACgkQ9CaO5/Lv0PAAmgCgt9QVocFjXVNB3t4ib6fWOUIL
++YAoKdpBuT1uhobAIxasRsdw/PaK00e
=zS1q
-----END PGP SIGNATURE-----

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
For additional commands, e-mail: users-help@tomcat.apache.org


Re: Access Log /Filter/?

Posted by Jason Brittain <ja...@mulesource.com>.
Hi Christopher.

On Tue, Mar 2, 2010 at 2:51 PM, Christopher Schultz <
chris@christopherschultz.net> wrote:

> -----BEGIN PGP SIGNED MESSAGE-----
> Hash: SHA1
>
> Xie,
>
> [snip]

> >  if (!isHttpServlet(request, response)) {
> >             chain.doFilter(request, response);
> >             return;
> >         }
>
> Why does the request have to be an HTTP request in order to have the
> access log run?


That does seem to be a bug.  By default, the access logger logs the common
log web server
format, but of course it doesn't have to, so it should log non-HTTP requests
as well, but maybe
only if a non-default pattern is configured?


> > long t2 = System.currentTimeMillis();
> > long time = t2 - t1;
>
> This isn't your choice, it's in the original code, but why not just do:
>
> long elapsed = System.currentTimeMillis();
> ...
> elapsed = System.currentTimeMillis() - elapsed;
>
> ??
>
> Fewer items on the stack, etc.
>

Except that then it is more difficult to debug.  Right?  It isn't as easy to
inspect the value of
the current time if you perform the subtraction without first assigning the
current time to a
variable.


>  > Date date = getDate();
> > StringBuffer result = new StringBuffer();
>
> The original default size for the StringBuffer was 128 characters.
> Presumably, this was done to avoid the cost of re-sizing the
> StringBuffer whenever it grew too large. The default initial capacity of
> a StringBuffer is 16 characters, which is almost certainly too small for
> a reasonable access log entry. You should put the larger size back in.
>
> While you're at it, use StringBuilder instead to avoid the overhead of
> synchronization for an object in a single-threaded environment.
>

Yes, using StringBuilder would probably make people worry less.  :)


>  >     public void log(String message) {
>
> Maybe re-write this method to avoid having to convert from
> StringBuffer/StringBuilder to String: why do the work if you don't have to?
>
> >     private static String lookup(String month) {
> >         int index;
> >         try {
> >             index = Integer.parseInt(month) - 1;
>
> Why not have a dummy "month" at index 0 and /not/ subtract? Come on...
> we're smarter than the Sun engineers, right? 0 = January? Stupid...
>

There might be a small opportunity for optimization/cleanup there.


>  >     private Date getDate() {
> >         // Only create a new Date once per second, max.
> >         long systime = System.currentTimeMillis();
> >         AccessDateStruct struct = currentDateStruct.get();
> >         if ((systime - struct.currentDate.getTime()) > 1000) {
> >             struct.currentDate.setTime(systime);
> >             struct.currentDateString = null;
> >         }
> >         return struct.currentDate;
> >     }
>
> I don't understand why this is ThreadLocal, instead of just synchronized
> across the object. Maybe it's slightly faster to avoid the
> synchronization and just use ThreadLocals, but I'm not sure how many
> requests per second a single Thread is going to process, so I'm not
> convinced that caching this data is worth the complexity it requires in
> this class. I'd love to hear from a Tomcat dev about this.
>

Tomcat can (hopefully) answer a larger number of requests per second
every year on decently modern hardware.  Benchmark it both ways on
a reasonably good/wide machine and you'll see why avoiding the sync
is helpful.  I don't think it muddies the code very badly here.


> >     protected static class ByteSentElement implements AccessLogElement {
> >         private boolean conversion;
> >
> >         /**
> >          * if conversion is true, write '-' instead of 0 - %b
> >          */
> >         public ByteSentElement(boolean conversion) {
> >             this.conversion = conversion;
> >         }
> >
> >         public void addElement(StringBuffer buf, Date date,
> > HttpServletRequest request, HttpServletResponse response,
> > long time) {
> >             long length = 0;
> >             if (response.getHeader("Content-Length") != null
> >                     && !"".equals(response.getHeader("Content-Length")))
> {
> >                 length =
> > Long.parseLong(response.getHeader("Content-Length"));
> >             }
> >             if (length <= 0 && response.getHeader("content-length") !=
> null
> >                     && !"".equals(response.getHeader("content-length")))
> {
> >                 length =
> > Long.parseLong(response.getHeader("content-length"));
> >             }
> > if (length <= 0 && conversion) {
> > buf.append('-');
> > } else {
> > buf.append(length);
> > }
> >         }
> >     }
>
> Note that this class will only report what was sent with the
> Content-Length header, rather than actually counting the bytes that were
> sent. Fixing this requires an architectural change: the BytesSentElement
> must be able to observe the OutputStream/Writer used by the servlet and
> count the bytes that were sent.
>
> Also, this method can cause an error if the Content-Type exceeds 2^31-1,
> which is bad. :( Why bother parsing the Content-Length in this case?
>
> This leads to another question: if the class is "improved" to actually
> count bytes, how will you count higher than 2^31 - 1?
>

The %b portion of the Combined Log Format is documented to be the "size of
the object returned to the client, not including the response headers."
http://httpd.apache.org/docs/1.3/logs.html#common

Thanks.
--
Jason Brittain
Mulesoft <http://www.mulesoft.com>

Re: Access Log /Filter/?

Posted by Christopher Schultz <ch...@christopherschultz.net>.
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Xie,

On 3/2/2010 3:58 PM, Xie Xiaodong wrote:
> public class AccessLogFilter
>     extends FilterBase {

For the most part, you've just replaced the invoke() method with a
doFilter() method and introduced init(), which calls start(). Then, you
removed all the Lifecycle stuff.

How will Filters be configured in TC7? If they will use the same

<filter>
  <init-param>

...style of configuration, then you'll need to have your init() method
fetch all the init-param values and set those values on the
AccessLogFilter before calling start().

I might just re-name start() to init() and make whatever changes are
necessary.

>     private static Log log = LogFactory.getLog(AccessLogFilter.class);

Oh, the irony.

> public void doFilter(ServletRequest request, ServletResponse response,
> FilterChain chain) throws IOException, ServletException {
> 
> log.info("In AccessLogFilter doFilter. ");

Obviously, you can't have this log message at the INFO level. I'll
assume that all logging currently in the code is for development
purposes and will be removed for a final version.

>  if (!isHttpServlet(request, response)) {
>             chain.doFilter(request, response);
>             return;
>         }

Why does the request have to be an HTTP request in order to have the
access log run? I would flip the logic around to only do the logging if
the request is an HTTP request, rather than having this mid-method return.

>  HttpServletRequest httpRequest = ((HttpServletRequest) request);
> 
> HttpServletResponse httpResponse = ((HttpServletResponse) response);
> 
> if (started && getEnabled()) {
> // Pass this request on to the next filter in the filterChain
> long t1 = System.currentTimeMillis();
> 
> chain.doFilter(request, response);
> 
> long t2 = System.currentTimeMillis();
> long time = t2 - t1;

This isn't your choice, it's in the original code, but why not just do:

long elapsed = System.currentTimeMillis();
...
elapsed = System.currentTimeMillis() - elapsed;

??

Fewer items on the stack, etc.

> Date date = getDate();
> StringBuffer result = new StringBuffer();

The original default size for the StringBuffer was 128 characters.
Presumably, this was done to avoid the cost of re-sizing the
StringBuffer whenever it grew too large. The default initial capacity of
a StringBuffer is 16 characters, which is almost certainly too small for
a reasonable access log entry. You should put the larger size back in.

While you're at it, use StringBuilder instead to avoid the overhead of
synchronization for an object in a single-threaded environment.

>     public void log(String message) {

Maybe re-write this method to avoid having to convert from
StringBuffer/StringBuilder to String: why do the work if you don't have to?

>     private static String lookup(String month) {
>         int index;
>         try {
>             index = Integer.parseInt(month) - 1;

Why not have a dummy "month" at index 0 and /not/ subtract? Come on...
we're smarter than the Sun engineers, right? 0 = January? Stupid...

>     private Date getDate() {
>         // Only create a new Date once per second, max.
>         long systime = System.currentTimeMillis();
>         AccessDateStruct struct = currentDateStruct.get();
>         if ((systime - struct.currentDate.getTime()) > 1000) {
>             struct.currentDate.setTime(systime);
>             struct.currentDateString = null;
>         }
>         return struct.currentDate;
>     }

I don't understand why this is ThreadLocal, instead of just synchronized
across the object. Maybe it's slightly faster to avoid the
synchronization and just use ThreadLocals, but I'm not sure how many
requests per second a single Thread is going to process, so I'm not
convinced that caching this data is worth the complexity it requires in
this class. I'd love to hear from a Tomcat dev about this.

>     protected static class ByteSentElement implements AccessLogElement {
>         private boolean conversion;
> 
>         /**
>          * if conversion is true, write '-' instead of 0 - %b
>          */
>         public ByteSentElement(boolean conversion) {
>             this.conversion = conversion;
>         }
> 
>         public void addElement(StringBuffer buf, Date date,
> HttpServletRequest request, HttpServletResponse response,
> long time) {
>             long length = 0;
>             if (response.getHeader("Content-Length") != null
>                     && !"".equals(response.getHeader("Content-Length"))) {
>                 length =
> Long.parseLong(response.getHeader("Content-Length"));
>             }
>             if (length <= 0 && response.getHeader("content-length") != null
>                     && !"".equals(response.getHeader("content-length"))) {
>                 length =
> Long.parseLong(response.getHeader("content-length"));
>             }
> if (length <= 0 && conversion) {
> buf.append('-');
> } else {
> buf.append(length);
> }
>         }
>     }

Note that this class will only report what was sent with the
Content-Length header, rather than actually counting the bytes that were
sent. Fixing this requires an architectural change: the BytesSentElement
must be able to observe the OutputStream/Writer used by the servlet and
count the bytes that were sent.

Also, this method can cause an error if the Content-Type exceeds 2^31-1,
which is bad. :( Why bother parsing the Content-Length in this case?

This leads to another question: if the class is "improved" to actually
count bytes, how will you count higher than 2^31 - 1?

>     protected AccessLogElement[] createLogElements() {

This method (historical, I know) seems way more complicated than it
really needs to be.

For instance, there's no need to keep a separate "buf" buffer just to
keep track of non-patterned text: just keep an index to the start and
end positions of the text, then create a "StringElement" out of that: no
need to create lots of StringBuffer objects for this.

Also, if desired, it would be almost trivial to create a pluggable
LogElement capability into this class with a set of defaults equal to
the current behavior: instead of using hard-coded switch statements to
determine which pattern letter corresponds to which LogElement class, a
registry could be used which would shorten-up the code a bit, too.

So, those are my thoughts on this class.

- -chris
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.10 (MingW32)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org/

iEYEARECAAYFAkuNloUACgkQ9CaO5/Lv0PD2IQCgtLggELEMQw8iNYf+gXOnnm9B
3cAAoKZL2qIu+LxsYHUoCC3N1L3cA50s
=aLcF
-----END PGP SIGNATURE-----

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
For additional commands, e-mail: users-help@tomcat.apache.org


Re: Access Log /Filter/?

Posted by Xie Xiaodong <xx...@gmail.com>.
/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */


package org.apache.catalina.filters;


import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.net.InetAddress;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import java.util.TimeZone;

import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.juli.logging.Log;
import org.apache.juli.logging.LogFactory;
import org.apache.tomcat.util.res.StringManager;


/**
 * <p>Implementation of the <b>Filter</b> interface that generates a web
server
 * access log with the detailed line contents matching a configurable
pattern.
 * The syntax of the available patterns is similar to that supported by the
 * Apache <code>mod_log_config</code> module.  As an additional feature,
 * automatic rollover of log files when the date changes is also
supported.</p>
 *
 * <p>Patterns for the logged message may include constant text or any of
the
 * following replacement strings, for which the corresponding information
 * from the specified Response is substituted:</p>
 * <ul>
 * <li><b>%a</b> - Remote IP address
 * <li><b>%A</b> - Local IP address
 * <li><b>%b</b> - Bytes sent, excluding HTTP headers, or '-' if no bytes
 *     were sent
 * <li><b>%B</b> - Bytes sent, excluding HTTP headers
 * <li><b>%h</b> - Remote host name
 * <li><b>%H</b> - Request protocol
 * <li><b>%l</b> - Remote logical username from identd (always returns '-')
 * <li><b>%m</b> - Request method
 * <li><b>%p</b> - Local port
 * <li><b>%q</b> - Query string (prepended with a '?' if it exists,
otherwise
 *     an empty string
 * <li><b>%r</b> - First line of the request
 * <li><b>%s</b> - HTTP status code of the response
 * <li><b>%S</b> - User session ID
 * <li><b>%t</b> - Date and time, in Common Log Format format
 * <li><b>%u</b> - Remote user that was authenticated
 * <li><b>%U</b> - Requested URL path
 * <li><b>%v</b> - Local server name
 * <li><b>%D</b> - Time taken to process the request, in millis
 * <li><b>%T</b> - Time taken to process the request, in seconds
 * <li><b>%I</b> - current Request thread name (can compare later with
stacktraces)
 * </ul>
 * <p>In addition, the caller can specify one of the following aliases for
 * commonly utilized patterns:</p>
 * <ul>
 * <li><b>common</b> - <code>%h %l %u %t "%r" %s %b</code>
 * <li><b>combined</b> -
 *   <code>%h %l %u %t "%r" %s %b "%{Referer}i" "%{User-Agent}i"</code>
 * </ul>
 *
 * <p>
 * There is also support to write information from the cookie, incoming
 * header, the Session or something else in the ServletRequest.<br>
 * It is modeled after the apache syntax:
 * <ul>
 * <li><code>%{xxx}i</code> for incoming headers
 * <li><code>%{xxx}o</code> for outgoing response headers
 * <li><code>%{xxx}c</code> for a specific cookie
 * <li><code>%{xxx}r</code> xxx is an attribute in the ServletRequest
 * <li><code>%{xxx}s</code> xxx is an attribute in the HttpSession
 * </ul>
 * </p>
 *
 * <p>
 * Conditional logging is also supported. This can be done with the
 * <code>condition</code> property.
 * If the value returned from ServletRequest.getAttribute(condition)
 * yields a non-null value. The logging will be skipped.
 * </p>
 *
 * @author Craig R. McClanahan
 * @author Jason Brittain
 * @author Remy Maucherat
 * @author Takayuki Kaneko
 * @author Peter Rossbach
 *
 */

public class AccessLogFilter
    extends FilterBase {

    private static Log log = LogFactory.getLog(AccessLogFilter.class);

    // ----------------------------------------------------- Instance
Variables


    /**
     * The as-of date for the currently open log file, or a zero-length
     * string if there is no open log file.
     */
    private volatile String dateStamp = "";


    /**
     * The directory in which log files are created.
     */
    private String directory = "logs";


    /**
     * The set of month abbreviations for log messages.
     */
    protected static final String months[] =
    { "Jan", "Feb", "Mar", "Apr", "May", "Jun",
      "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" };


    /**
     * enabled this component
     */
    protected boolean enabled = true;

    /**
     * The pattern used to format our access log lines.
     */
    protected String pattern = null;


    /**
     * The prefix that is added to log file filenames.
     */
    protected String prefix = "access_log.";


    /**
     * Should we rotate our log file? Default is true (like old behavior)
     */
    protected boolean rotatable = true;


    /**
     * Buffered logging.
     */
    private boolean buffered = true;


    /**
     * The string manager for this package.
     */
    protected StringManager sm =
        StringManager.getManager(Constants.Package);


    /**
     * Has this component been started yet?
     */
    protected boolean started = false;


    /**
     * The suffix that is added to log file filenames.
     */
    protected String suffix = "";


    /**
     * The PrintWriter to which we are currently logging, if any.
     */
    protected PrintWriter writer = null;


    /**
     * A date formatter to format a Date into a date in the format
     * "yyyy-MM-dd".
     */
    protected SimpleDateFormat fileDateFormatter = null;


    /**
     * The system timezone.
     */
    private TimeZone timezone = null;


    /**
     * The time zone offset relative to GMT in text form when daylight
saving
     * is not in operation.
     */
    private String timeZoneNoDST = null;


    /**
     * The time zone offset relative to GMT in text form when daylight
saving
     * is in operation.
     */
    private String timeZoneDST = null;


    /**
     * The current log file we are writing to. Helpful when checkExists
     * is true.
     */
    protected File currentLogFile = null;
    private static class AccessDateStruct {
        private Date currentDate = new Date();
        private String currentDateString = null;
        private SimpleDateFormat dayFormatter = new SimpleDateFormat("dd");
        private SimpleDateFormat monthFormatter = new
SimpleDateFormat("MM");
        private SimpleDateFormat yearFormatter = new
SimpleDateFormat("yyyy");
        private SimpleDateFormat timeFormatter = new
SimpleDateFormat("HH:mm:ss");
        public AccessDateStruct() {
            TimeZone tz = TimeZone.getDefault();
            dayFormatter.setTimeZone(tz);
            monthFormatter.setTimeZone(tz);
            yearFormatter.setTimeZone(tz);
            timeFormatter.setTimeZone(tz);
        }
    }

    /**
     * The system time when we last updated the Date that this filter
     * uses for log lines.
     */
    private static final ThreadLocal<AccessDateStruct> currentDateStruct =
        new ThreadLocal<AccessDateStruct>() {
        protected AccessDateStruct initialValue() {
            return new AccessDateStruct();
        }
    };
    /**
     * Resolve hosts.
     */
    private boolean resolveHosts = false;


    /**
     * Instant when the log daily rotation was last checked.
     */
    private volatile long rotationLastChecked = 0L;

    /**
     * Do we check for log file existence? Helpful if an external
     * agent renames the log file so we can automagically recreate it.
     */
    private boolean checkExists = false;


    /**
     * Are we doing conditional logging. default false.
     */
    protected String condition = null;


    /**
     * Date format to place in log file name. Use at your own risk!
     */
    protected String fileDateFormat = null;

    /**
     * Array of AccessLogElement, they will be used to make log message.
     */
    protected AccessLogElement[] logElements = null;

    // -------------------------------------------------------------
Properties

    /**
     * @return Returns the enabled.
     */
    public boolean getEnabled() {
        return enabled;
    }

    /**
     * @param enabled
     *            The enabled to set.
     */
    public void setEnabled(boolean enabled) {
        this.enabled = enabled;
    }

    /**
     * Return the directory in which we create log files.
     */
    public String getDirectory() {
        return (directory);
    }


    /**
     * Set the directory in which we create log files.
     *
     * @param directory The new log file directory
     */
    public void setDirectory(String directory) {
        this.directory = directory;
    }


    /**
     * Return the format pattern.
     */
    public String getPattern() {
        return (this.pattern);
    }


    /**
     * Set the format pattern, first translating any recognized alias.
     *
     * @param pattern The new pattern
     */
    public void setPattern(String pattern) {
        if (pattern == null)
            pattern = "";
        if (pattern.equals(Constants.AccessLog.COMMON_ALIAS))
            pattern = Constants.AccessLog.COMMON_PATTERN;
        if (pattern.equals(Constants.AccessLog.COMBINED_ALIAS))
            pattern = Constants.AccessLog.COMBINED_PATTERN;
        this.pattern = pattern;
        logElements = createLogElements();
    }


    /**
     * Check for file existence before logging.
     */
    public boolean isCheckExists() {

        return checkExists;

    }


    /**
     * Set whether to check for log file existence before logging.
     *
     * @param checkExists true meaning to check for file existence.
     */
    public void setCheckExists(boolean checkExists) {

        this.checkExists = checkExists;

    }


    /**
     * Return the log file prefix.
     */
    public String getPrefix() {
        return (prefix);
    }


    /**
     * Set the log file prefix.
     *
     * @param prefix The new log file prefix
     */
    public void setPrefix(String prefix) {
        this.prefix = prefix;
    }


    /**
     * Should we rotate the logs
     */
    public boolean isRotatable() {
        return rotatable;
    }


    /**
     * Set the value is we should we rotate the logs
     *
     * @param rotatable true is we should rotate.
     */
    public void setRotatable(boolean rotatable) {
        this.rotatable = rotatable;
    }


    /**
     * Is the logging buffered
     */
    public boolean isBuffered() {
        return buffered;
    }


    /**
     * Set the value if the logging should be buffered
     *
     * @param buffered true if buffered.
     */
    public void setBuffered(boolean buffered) {
        this.buffered = buffered;
    }


    /**
     * Return the log file suffix.
     */
    public String getSuffix() {
        return (suffix);
    }


    /**
     * Set the log file suffix.
     *
     * @param suffix The new log file suffix
     */
    public void setSuffix(String suffix) {
        this.suffix = suffix;
    }


    /**
     * Set the resolve hosts flag.
     *
     * @param resolveHosts The new resolve hosts value
     */
    public void setResolveHosts(boolean resolveHosts) {
        this.resolveHosts = resolveHosts;
    }


    /**
     * Get the value of the resolve hosts flag.
     */
    public boolean isResolveHosts() {
        return resolveHosts;
    }


    /**
     * Return whether the attribute name to look for when
     * performing conditional loggging. If null, every
     * request is logged.
     */
    public String getCondition() {
        return condition;
    }


    /**
     * Set the ServletRequest.attribute to look for to perform
     * conditional logging. Set to null to log everything.
     *
     * @param condition Set to null to log everything
     */
    public void setCondition(String condition) {
        this.condition = condition;
    }

    /**
     *  Return the date format date based log rotation.
     */
    public String getFileDateFormat() {
        return fileDateFormat;
    }


    /**
     *  Set the date format date based log rotation.
     */
    public void setFileDateFormat(String fileDateFormat) {
        this.fileDateFormat =  fileDateFormat;
    }

    // --------------------------------------------------------- Public
Methods

    /**
     * Execute a periodic task, such as reloading, etc. This method will be
     * invoked inside the classloading context of this container. Unexpected
     * throwables will be caught and logged.
     */
    public void backgroundProcess() {
        if (started && getEnabled() && writer != null && buffered) {
            writer.flush();
        }
    }

/**
 * Log a message summarizing the specified request and response, according
to the
 * format specified by the <code>pattern</code> property.
 *
 * @param request
 *            Request being processed
 * @param response
 *            Response being processed
 *
 * @exception IOException
 *                if an input/output error has occurred
 * @exception ServletException
 *                if a servlet error has occurred
 */
@Override
public void doFilter(ServletRequest request, ServletResponse response,
FilterChain chain) throws IOException, ServletException {

log.info("In AccessLogFilter doFilter. ");
 if (!isHttpServlet(request, response)) {
            chain.doFilter(request, response);
            return;
        }
 HttpServletRequest httpRequest = ((HttpServletRequest) request);

HttpServletResponse httpResponse = ((HttpServletResponse) response);

if (started && getEnabled()) {
// Pass this request on to the next filter in the filterChain
long t1 = System.currentTimeMillis();

chain.doFilter(request, response);

long t2 = System.currentTimeMillis();
long time = t2 - t1;

if (logElements == null
|| condition != null
&& null != httpRequest.getAttribute(
condition)) {
return;
}

log.debug("logElements: " + logElements.length + " ; "
+ Arrays.toString(logElements));

Date date = getDate();
StringBuffer result = new StringBuffer();

for (int i = 0; i < logElements.length; i++) {
logElements[i].addElement(result, date, httpRequest,
httpResponse, time);
}

log(result.toString());
} else
chain.doFilter(request, response);
}

@Override
public void init(FilterConfig filterConfig) throws ServletException {
super.init(filterConfig);
start();
}

@Override
public void destroy() {
try {
stop();
} catch (ServletException e) {
log.error(e);
throw new RuntimeException(e);
}
}


    /**
     * Rename the existing log file to something else. Then open the
     * old log file name up once again. Intended to be called by a JMX
     * agent.
     *
     *
     * @param newFileName The file name to move the log file entry to
     * @return true if a file was rotated with no error
     */
    public synchronized boolean rotate(String newFileName) {

        if (currentLogFile != null) {
            File holder = currentLogFile;
            close();
            try {
                holder.renameTo(new File(newFileName));
            } catch (Throwable e) {
                log.error("rotate failed", e);
            }

            /* Make sure date is correct */
            dateStamp = fileDateFormatter.format(
                    new Date(System.currentTimeMillis()));

            open();
            return true;
        } else {
            return false;
        }

    }

    protected Log getLogger() {
        return log;
    }

    // -------------------------------------------------------- Private
Methods


    /**
     * Close the currently open log file (if any)
     */
    private synchronized void close() {
        if (writer == null) {
            return;
        }
        writer.flush();
        writer.close();
        writer = null;
        dateStamp = "";
        currentLogFile = null;
    }


    /**
     * Log the specified message to the log file, switching files if the
date
     * has changed since the previous log call.
     *
     * @param message Message to be logged
     */
    public void log(String message) {
        if (rotatable) {
            // Only do a logfile switch check once a second, max.
            long systime = System.currentTimeMillis();
            if ((systime - rotationLastChecked) > 1000) {
                synchronized(this) {
                    if ((systime - rotationLastChecked) > 1000) {
                        rotationLastChecked = systime;

                        String tsDate;
                        // Check for a change of date
                        tsDate = fileDateFormatter.format(new
Date(systime));

                        // If the date has changed, switch log files
                        if (!dateStamp.equals(tsDate)) {
                            close();
                            dateStamp = tsDate;
                            open();
                        }
                    }
                }
            }
        }

        /* In case something external rotated the file instead */
        if (checkExists) {
            synchronized (this) {
                if (currentLogFile != null && !currentLogFile.exists()) {
                    try {
                        close();
                    } catch (Throwable e) {
                        log.info("at least this wasn't swallowed", e);
                    }

                    /* Make sure date is correct */
                    dateStamp = fileDateFormatter.format(
                            new Date(System.currentTimeMillis()));

                    open();
                }
            }
        }

        // Log this message
        synchronized(this) {
            if (writer != null) {
                writer.println(message);
                if (!buffered) {
                    writer.flush();
                }
            }
        }

    }


    /**
     * Return the month abbreviation for the specified month, which must
     * be a two-digit String.
     *
     * @param month Month number ("01" .. "12").
     */
    private static String lookup(String month) {
        int index;
        try {
            index = Integer.parseInt(month) - 1;
        } catch (Throwable t) {
            index = 0;  // Can not happen, in theory
        }
        return (months[index]);
    }


    /**
     * Open the new log file for the date specified by
<code>dateStamp</code>.
     */
    protected synchronized void open() {
        // Create the directory if necessary
        File dir = new File(directory);
        if (!dir.isAbsolute())
            dir = new File(System.getProperty("catalina.base"), directory);
        dir.mkdirs();

        // Open the current log file
        try {
            String pathname;
            // If no rotate - no need for dateStamp in fileName
            if (rotatable) {
                pathname = dir.getAbsolutePath() + File.separator + prefix
                        + dateStamp + suffix;
            } else {
                pathname = dir.getAbsolutePath() + File.separator + prefix
                        + suffix;
            }
            writer = new PrintWriter(new BufferedWriter(new FileWriter(
                    pathname, true), 128000), false);

            currentLogFile = new File(pathname);
        } catch (IOException e) {
            writer = null;
            currentLogFile = null;
        }
    }

    /**
     * This method returns a Date object that is accurate to within one
second.
     * If a thread calls this method to get a Date and it's been less than 1
     * second since a new Date was created, this method simply gives out the
     * same Date again so that the system doesn't spend time creating Date
     * objects unnecessarily.
     *
     * @return Date
     */
    private Date getDate() {
        // Only create a new Date once per second, max.
        long systime = System.currentTimeMillis();
        AccessDateStruct struct = currentDateStruct.get();
        if ((systime - struct.currentDate.getTime()) > 1000) {
            struct.currentDate.setTime(systime);
            struct.currentDateString = null;
        }
        return struct.currentDate;
    }


    private String getTimeZone(Date date) {
        if (timezone.inDaylightTime(date)) {
            return timeZoneDST;
        } else {
            return timeZoneNoDST;
        }
    }


    private String calculateTimeZoneOffset(long offset) {
        StringBuffer tz = new StringBuffer();
        if ((offset < 0)) {
            tz.append("-");
            offset = -offset;
        } else {
            tz.append("+");
        }

        long hourOffset = offset / (1000 * 60 * 60);
        long minuteOffset = (offset / (1000 * 60)) % 60;

        if (hourOffset < 10)
            tz.append("0");
        tz.append(hourOffset);

        if (minuteOffset < 10)
            tz.append("0");
        tz.append(minuteOffset);

        return tz.toString();
    }

    /**
     * Prepare for the beginning of active use of the public methods of this
     * component.  This method should be called after
<code>configure()</code>,
     * and before any of the public methods of the component are utilized.
     *
     * @exception ServletException if this component detects a fatal error
     *  that prevents this component from being used
     */
    public void start() throws ServletException {

        // Validate and update our current component state
        if (started)
            throw new ServletException(sm
                    .getString("accessLogFilter.alreadyStarted"));
        started = true;

        // Initialize the timeZone, Date formatters, and currentDate
        timezone = TimeZone.getDefault();
        timeZoneNoDST = calculateTimeZoneOffset(timezone.getRawOffset());
        int offset = timezone.getDSTSavings();
        timeZoneDST = calculateTimeZoneOffset(timezone.getRawOffset() +
offset);

        if (fileDateFormat == null || fileDateFormat.length() == 0)
            fileDateFormat = "yyyy-MM-dd";
        fileDateFormatter = new SimpleDateFormat(fileDateFormat);
        fileDateFormatter.setTimeZone(timezone);
        dateStamp =
fileDateFormatter.format(currentDateStruct.get().currentDate);
        open();
    }


    /**
     * Gracefully terminate the active use of the public methods of this
     * component.  This method should be the last one called on a given
     * instance of this component.
     *
     * @exception ServletException if this component detects a fatal error
     *  that needs to be reported
     */
    public void stop() throws ServletException {

        // Validate and update our current component state
        if (!started)
            throw new ServletException(sm
                    .getString("accessLogFilter.notStarted"));
        started = false;

        close();
    }

    /**
     * AccessLogElement writes the partial message into the buffer.
     */
    protected interface AccessLogElement {
        public void addElement(StringBuffer buf, Date date,
HttpServletRequest request,
         HttpServletResponse response, long time);

    }

    /**
     * write thread name - %I
     */
    protected static class ThreadNameElement implements AccessLogElement {
        public void addElement(StringBuffer buf, Date date,
HttpServletRequest request, HttpServletResponse response,
long time) {
            // I found that the workerThreadName is set using current
thread's
            // name, and I think that the thread executing event() or
service() should be
            // the same thread as this one, so I modified this method like
this.
            buf.append(Thread.currentThread().getName());
        }
    }

    /**
     * write local IP address - %A
     */
    protected static class LocalAddrElement implements AccessLogElement {

        private static final String LOCAL_ADDR_VALUE;

        static {
            String init;
            try {
                init = InetAddress.getLocalHost().getHostAddress();
            } catch (Throwable e) {
                init = "127.0.0.1";
            }
            LOCAL_ADDR_VALUE = init;
        }

        public void addElement(StringBuffer buf, Date date,
HttpServletRequest request, HttpServletResponse response,
long time) {
            buf.append(LOCAL_ADDR_VALUE);
        }
    }

    /**
     * write remote IP address - %a
     */
    protected static class RemoteAddrElement implements AccessLogElement {
        public void addElement(StringBuffer buf, Date date,
HttpServletRequest request, HttpServletResponse response,
long time) {
            buf.append(request.getRemoteAddr());
        }
    }

    /**
     * write remote host name - %h
     */
    protected static class HostElement implements AccessLogElement {
        public void addElement(StringBuffer buf, Date date,
HttpServletRequest request, HttpServletResponse response,
long time) {
            buf.append(request.getRemoteHost());
        }
    }

    /**
     * write remote logical username from identd (always returns '-') - %l
     */
    protected static class LogicalUserNameElement implements
AccessLogElement {
        public void addElement(StringBuffer buf, Date date,
HttpServletRequest request, HttpServletResponse response,
long time) {
            buf.append('-');
        }
    }

    /**
     * write request protocol - %H
     */
    protected static class ProtocolElement implements AccessLogElement {
        public void addElement(StringBuffer buf, Date date,
HttpServletRequest request, HttpServletResponse response,
long time) {
            buf.append(request.getProtocol());
        }
    }

    /**
     * write remote user that was authenticated (if any), else '-' - %u
     */
    protected static class UserElement implements AccessLogElement {
        public void addElement(StringBuffer buf, Date date,
HttpServletRequest request, HttpServletResponse response,
long time) {
            if (request != null) {
                String value = request.getRemoteUser();
                if (value != null) {
                    buf.append(value);
                } else {
                    buf.append('-');
                }
            } else {
                buf.append('-');
            }
        }
    }

    /**
     * write date and time, in Common Log Format - %t
     */
    protected class DateAndTimeElement implements AccessLogElement {

        public void addElement(StringBuffer buf, Date date,
HttpServletRequest request, HttpServletResponse response,
long time) {
            AccessDateStruct struct = currentDateStruct.get();
            if (struct.currentDateString == null) {
                StringBuffer current = new StringBuffer(32);
                current.append('[');
                current.append(struct.dayFormatter.format(date));
                current.append('/');
                current.append(lookup(struct.monthFormatter.format(date)));
                current.append('/');
                current.append(struct.yearFormatter.format(date));
                current.append(':');
                current.append(struct.timeFormatter.format(date));
                current.append(' ');
                current.append(getTimeZone(date));
                current.append(']');
                struct.currentDateString = current.toString();
            }
            buf.append(struct.currentDateString);
        }
    }

    /**
     * write first line of the request (method and request URI) - %r
     */
    protected static class RequestElement implements AccessLogElement {
        public void addElement(StringBuffer buf, Date date,
HttpServletRequest request, HttpServletResponse response,
long time) {
            if (request != null) {
                buf.append(request.getMethod());
                buf.append(' ');
                buf.append(request.getRequestURI());
                if (request.getQueryString() != null) {
                    buf.append('?');
                    buf.append(request.getQueryString());
                }
                buf.append(' ');
                buf.append(request.getProtocol());
            } else {
                buf.append("- - ");
            }
        }
    }

    /**
     * write HTTP status code of the response - %s
     */
    protected static class HttpStatusCodeElement implements AccessLogElement
{
        public void addElement(StringBuffer buf, Date date,
HttpServletRequest request, HttpServletResponse response,
long time) {
            if (response != null) {
                buf.append(response.getStatus());
            } else {
                buf.append('-');
            }
        }
    }

    /**
     * write local port on which this request was received - %p
     */
    protected static class LocalPortElement implements AccessLogElement {
        public void addElement(StringBuffer buf, Date date,
HttpServletRequest request, HttpServletResponse response,
long time) {
            buf.append(request.getServerPort());
        }
    }

    /**
     * write bytes sent, excluding HTTP headers - %b, %B
     */
    protected static class ByteSentElement implements AccessLogElement {
        private boolean conversion;

        /**
         * if conversion is true, write '-' instead of 0 - %b
         */
        public ByteSentElement(boolean conversion) {
            this.conversion = conversion;
        }

        public void addElement(StringBuffer buf, Date date,
HttpServletRequest request, HttpServletResponse response,
long time) {
            long length = 0;
            if (response.getHeader("Content-Length") != null
                    && !"".equals(response.getHeader("Content-Length"))) {
                length =
Long.parseLong(response.getHeader("Content-Length"));
            }
            if (length <= 0 && response.getHeader("content-length") != null
                    && !"".equals(response.getHeader("content-length"))) {
                length =
Long.parseLong(response.getHeader("content-length"));
            }
if (length <= 0 && conversion) {
buf.append('-');
} else {
buf.append(length);
}
        }
    }

    /**
     * write request method (GET, POST, etc.) - %m
     */
    protected static class MethodElement implements AccessLogElement {
        public void addElement(StringBuffer buf, Date date,
HttpServletRequest request, HttpServletResponse response,
long time) {
            if (request != null) {
                buf.append(request.getMethod());
            }
        }
    }

    /**
     * write time taken to process the request - %D, %T
     */
    protected static class ElapsedTimeElement implements AccessLogElement {
        private boolean millis;

        /**
         * if millis is true, write time in millis - %D
         * if millis is false, write time in seconds - %T
         */
        public ElapsedTimeElement(boolean millis) {
            this.millis = millis;
        }

        public void addElement(StringBuffer buf, Date date,
HttpServletRequest request, HttpServletResponse response,
long time) {
            if (millis) {
                buf.append(time);
            } else {
                // second
                buf.append(time / 1000);
                buf.append('.');
                int remains = (int) (time % 1000);
                buf.append(remains / 100);
                remains = remains % 100;
                buf.append(remains / 10);
                buf.append(remains % 10);
            }
        }
    }

    /**
     * write Query string (prepended with a '?' if it exists) - %q
     */
    protected static class QueryElement implements AccessLogElement {
        public void addElement(StringBuffer buf, Date date,
HttpServletRequest request, HttpServletResponse response,
long time) {
            String query = null;
            if (request != null)
                query = request.getQueryString();
            if (query != null) {
                buf.append('?');
                buf.append(query);
            }
        }
    }

    /**
     * write user session ID - %S
     */
    protected static class SessionIdElement implements AccessLogElement {
        public void addElement(StringBuffer buf, Date date,
HttpServletRequest request, HttpServletResponse response,
long time) {
            if (request != null) {
                if (request.getSession(false) != null) {
                    buf.append(request.getSession(false).getId());
                } else {
                    buf.append('-');
                }
            } else {
                buf.append('-');
            }
        }
    }

    /**
     * write requested URL path - %U
     */
    protected static class RequestURIElement implements AccessLogElement {
        public void addElement(StringBuffer buf, Date date,
HttpServletRequest request, HttpServletResponse response,
long time) {
            if (request != null) {
                buf.append(request.getRequestURI());
            } else {
                buf.append('-');
            }
        }
    }

    /**
     * write local server name - %v
     */
    protected static class LocalServerNameElement implements
AccessLogElement {
        public void addElement(StringBuffer buf, Date date,
HttpServletRequest request, HttpServletResponse response,
long time) {
            buf.append(request.getServerName());
        }
    }

    /**
     * write any string
     */
    protected static class StringElement implements AccessLogElement {
        private String str;

        public StringElement(String str) {
            this.str = str;
        }

        public void addElement(StringBuffer buf, Date date,
HttpServletRequest request, HttpServletResponse response,
long time) {
            buf.append(str);
        }
    }

    /**
     * write incoming headers - %{xxx}i
     */
    protected static class HeaderElement implements AccessLogElement {
        private String header;

        public HeaderElement(String header) {
            this.header = header;
        }

        public void addElement(StringBuffer buf, Date date,
HttpServletRequest request, HttpServletResponse response,
long time) {
            String value = request.getHeader(header);
            if (value == null) {
                buf.append('-');
            } else {
                buf.append(value);
            }
        }
    }

    /**
     * write a specific cookie - %{xxx}c
     */
    protected static class CookieElement implements AccessLogElement {
        private String header;

        public CookieElement(String header) {
            this.header = header;
        }

        public void addElement(StringBuffer buf, Date date,
HttpServletRequest request, HttpServletResponse response,
long time) {
            String value = "-";
            Cookie[] c = request.getCookies();
            if (c != null) {
                for (int i = 0; i < c.length; i++) {
                    if (header.equals(c[i].getName())) {
                        value = c[i].getValue();
                        break;
                    }
                }
            }
            buf.append(value);
        }
    }

    /**
     * write a specific response header - %{xxx}o
     */
    protected static class ResponseHeaderElement implements AccessLogElement
{
        private String header;

        public ResponseHeaderElement(String header) {
            this.header = header;
        }

        public void addElement(StringBuffer buf, Date date,
HttpServletRequest request, HttpServletResponse response,
long time) {
           if (null != response) {
                Iterator<String> iter =
response.getHeaders(header).iterator();
                boolean first = true;
                while (iter.hasNext()) {
                    if (!first) {
                        buf.append(",");
                    }
                    buf.append(iter.next());
                }
                return ;
            }
            buf.append("-");
        }
    }

    /**
     * write an attribute in the ServletRequest - %{xxx}r
     */
    protected static class RequestAttributeElement implements
AccessLogElement {
        private String header;

        public RequestAttributeElement(String header) {
            this.header = header;
        }

        public void addElement(StringBuffer buf, Date date,
HttpServletRequest request, HttpServletResponse response,
long time) {
            Object value = null;
            if (request != null) {
                value = request.getAttribute(header);
            } else {
                value = "??";
            }
            if (value != null) {
                if (value instanceof String) {
                    buf.append((String) value);
                } else {
                    buf.append(value.toString());
                }
            } else {
                buf.append('-');
            }
        }
    }

    /**
     * write an attribute in the HttpSession - %{xxx}s
     */
    protected static class SessionAttributeElement implements
AccessLogElement {
        private String header;

        public SessionAttributeElement(String header) {
            this.header = header;
        }

        public void addElement(StringBuffer buf, Date date,
HttpServletRequest request, HttpServletResponse response,
long time) {
            Object value = null;
            if (null != request) {
                HttpSession sess = request.getSession(false);
                if (null != sess)
                    value = sess.getAttribute(header);
            } else {
                value = "??";
            }
            if (value != null) {
                if (value instanceof String) {
                    buf.append((String) value);
                } else {
                    buf.append(value.toString());
                }
            } else {
                buf.append('-');
            }
        }
    }




    /**
     * parse pattern string and create the array of AccessLogElement
     */
    protected AccessLogElement[] createLogElements() {
        List<AccessLogElement> list = new ArrayList<AccessLogElement>();
        boolean replace = false;
        StringBuffer buf = new StringBuffer();
        for (int i = 0; i < pattern.length(); i++) {
            char ch = pattern.charAt(i);
            if (replace) {
                /*
                 * For code that processes {, the behavior will be ... if I
do
                 * not enounter a closing } - then I ignore the {
                 */
                if ('{' == ch) {
                    StringBuffer name = new StringBuffer();
                    int j = i + 1;
                    for (; j < pattern.length() && '}' != pattern.charAt(j);
j++) {
                        name.append(pattern.charAt(j));
                    }
                    if (j + 1 < pattern.length()) {
                        /* the +1 was to account for } which we increment
now */
                        j++;
                        list.add(createAccessLogElement(name.toString(),
                                pattern.charAt(j)));
                        i = j; /* Since we walked more than one character */
                    } else {
                        // D'oh - end of string - pretend we never did this
                        // and do processing the "old way"
                        list.add(createAccessLogElement(ch));
                    }
                } else {
                    list.add(createAccessLogElement(ch));
                }
                replace = false;
            } else if (ch == '%') {
                replace = true;
                list.add(new StringElement(buf.toString()));
                buf = new StringBuffer();
            } else {
                buf.append(ch);
            }
        }
        if (buf.length() > 0) {
            list.add(new StringElement(buf.toString()));
        }
        return list.toArray(new AccessLogElement[0]);
    }

    /**
     * create an AccessLogElement implementation which needs header string
     */
    private AccessLogElement createAccessLogElement(String header, char
pattern) {
        switch (pattern) {
        case 'i':
            return new HeaderElement(header);
        case 'c':
            return new CookieElement(header);
        case 'o':
            return new ResponseHeaderElement(header);
        case 'r':
            return new RequestAttributeElement(header);
        case 's':
            return new SessionAttributeElement(header);
        default:
            return new StringElement("???");
        }
    }

    /**
     * create an AccessLogElement implementation
     */
    private AccessLogElement createAccessLogElement(char pattern) {
        switch (pattern) {
        case 'a':
            return new RemoteAddrElement();
        case 'A':
            return new LocalAddrElement();
        case 'b':
            return new ByteSentElement(true);
        case 'B':
            return new ByteSentElement(false);
        case 'D':
            return new ElapsedTimeElement(true);
        case 'h':
            return new HostElement();
        case 'H':
            return new ProtocolElement();
        case 'l':
            return new LogicalUserNameElement();
        case 'm':
            return new MethodElement();
        case 'p':
            return new LocalPortElement();
        case 'q':
            return new QueryElement();
        case 'r':
            return new RequestElement();
        case 's':
            return new HttpStatusCodeElement();
        case 'S':
            return new SessionIdElement();
        case 't':
            return new DateAndTimeElement();
        case 'T':
            return new ElapsedTimeElement(false);
        case 'u':
            return new UserElement();
        case 'U':
            return new RequestURIElement();
        case 'v':
            return new LocalServerNameElement();
        case 'I':
            return new ThreadNameElement();
        default:
            return new StringElement("???" + pattern + "???");
        }
    }
}


On Tue, Mar 2, 2010 at 9:45 PM, Christopher Schultz <
chris@christopherschultz.net> wrote:

> -----BEGIN PGP SIGNED MESSAGE-----
> Hash: SHA1
>
> Xie (or is it Xiaodong?),
>
> On 3/2/2010 2:57 PM, Xie Xiaodong wrote:
> > I submitted some code but not including the AccessLogFilter since I had
> > something question about the implementation of it. Here I give you the
> > version I wrote last summer. Hope you could check it and submit a patch.
>
> Your attachment must have been stripped from the list. Please consider
> re-posting inline.
>
> - -chris
> -----BEGIN PGP SIGNATURE-----
> Version: GnuPG v1.4.10 (MingW32)
> Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org/
>
> iEYEARECAAYFAkuNeOkACgkQ9CaO5/Lv0PBqXQCfUCrW3JCxOfU7tL1mnuJjwumC
> oRQAnRKH+Xizgh5iimVvQjq8rGZ1wTte
> =tCKO
> -----END PGP SIGNATURE-----
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
> For additional commands, e-mail: users-help@tomcat.apache.org
>
>


-- 
Sincerely yours and Best Regards,
Xie Xiaodong

Re: Access Log /Filter/?

Posted by Christopher Schultz <ch...@christopherschultz.net>.
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Xie (or is it Xiaodong?),

On 3/2/2010 2:57 PM, Xie Xiaodong wrote:
> I submitted some code but not including the AccessLogFilter since I had
> something question about the implementation of it. Here I give you the
> version I wrote last summer. Hope you could check it and submit a patch. 

Your attachment must have been stripped from the list. Please consider
re-posting inline.

- -chris
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.10 (MingW32)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org/

iEYEARECAAYFAkuNeOkACgkQ9CaO5/Lv0PBqXQCfUCrW3JCxOfU7tL1mnuJjwumC
oRQAnRKH+Xizgh5iimVvQjq8rGZ1wTte
=tCKO
-----END PGP SIGNATURE-----

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
For additional commands, e-mail: users-help@tomcat.apache.org


Re: Access Log /Filter/?

Posted by Xie Xiaodong <xx...@gmail.com>.
Hello, Christopher,


I submitted some code but not including the AccessLogFilter since I had
something question about the implementation of it. Here I give you the
version I wrote last summer. Hope you could check it and submit a patch.


On Tue, Mar 2, 2010 at 6:41 PM, Christopher Schultz <
chris@christopherschultz.net> wrote:

> -----BEGIN PGP SIGNED MESSAGE-----
> Hash: SHA1
>
> Xie,
>
> On 2/26/2010 4:33 PM, Xie Xiaodong wrote:
> > No, there is no AccessLogFilter in Tomcat 7 for now. I've got my
> > version of AccessLogFilter during Google Summer Code 2009, but has
> > not yet submit it for some reason.
>
> Uh, why not submit it and get paid?
>
> > Please attach your version so we could discuss it and make an
> > AccessLogFilter in Tomcat 7.
>
> I don't have a version, yet. I was checking to see if someone had done
> it already. But if I give you my code so Google can pay you, I'd like
> some of that money :)
>
> - -chris
> -----BEGIN PGP SIGNATURE-----
> Version: GnuPG v1.4.10 (MingW32)
> Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org/
>
> iEYEARECAAYFAkuNTeUACgkQ9CaO5/Lv0PCOOwCeM78myOdUfyyHxTTrbQloeb3W
> Ja8AnA4gDir14UAIIoG9hoc8YcKMEeJ6
> =nst1
> -----END PGP SIGNATURE-----
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
> For additional commands, e-mail: users-help@tomcat.apache.org
>
>


-- 
Sincerely yours and Best Regards,
Xie Xiaodong