You are viewing a plain text version of this content. The canonical link for it is here.
Posted to log4j-user@logging.apache.org by Chris Cheshire <ch...@gmail.com> on 2006/11/09 03:40:57 UTC

dynamically adding to a log file name?

Hi,

I have a process that sends an xml request to an external server and
receives an xml response back. For purposes of debugging, I need to
capture the request and response in separate files but with similar
names so that I can pair them up easily.

Currently I am using James Stauffer's DateFormatFileAppender (very
useful James!!) with the filename set to
'${log4j.log.dir}/'yyyy/MM/dd/'qresponse.'yyyyMMdd-HHmmss-S'.xml' (and
similar for the request) to log each request and response to a
separate file each time. (I do understand that there is potential for
2 requests at the same millisecond, but this is really for debugging
only and not production, so I can pretty much guarantee that won't
happen).

By using the milliseconds in the filename I can get unique requests or
responses, but each request and subsequent response will have slightly
different timestamps. I might get two requests processed before I get
the response back for the first, which makes it slightly difficult to
match them up.

I don't want to log the request and response to the same file, because
then it is no longer a well formed XML document that I can look at in
an XML browser.

Is there some way I can dynamically add something (like my internal
requestid) to the file names at log time such that I can pair the log
files up, without forcefully finding the appender and reconfiguring it
each time (which would be rather slow)?

If all else fails I can add an XML comment to the end of the file via
the pattern layout, but I am looking for something that will help for
scan value in matching the files up.

Thanks

Chris

---------------------------------------------------------------------
To unsubscribe, e-mail: log4j-user-unsubscribe@logging.apache.org
For additional commands, e-mail: log4j-user-help@logging.apache.org


Re: dynamically adding to a log file name?

Posted by Chris Cheshire <ch...@gmail.com>.
Because the request is generated in a library not an application. The
library has no concept of what container it is running in or where it
should save files. I felt log4j was the best solution for this as it
allows me to turn this on and off at will and goes with the trace info
I have in there already.

On 11/9/06, Maarten Bosteels <mb...@gmail.com> wrote:
> Why don't you just save the request and the response to a file ?
> You do not need log4j for that, right ?
>
>       DateFormat df = new SimpleDateFormat("yyyyMMdd-HHmmss-S");
>       String filename = "/tmp/" + df.format(new Date()) + "-request.xml";
>       FileWriter writer = new FileWriter(filename);
>       writer.write(request);
>       writer.close();
>
> If you need to specify the charset use
>
>     OutputStreamWriter writer = new OutputStreamWriter(new
> FileOutputStream(filename), "UTF-8");
>
>
> Maarten
>

---------------------------------------------------------------------
To unsubscribe, e-mail: log4j-user-unsubscribe@logging.apache.org
For additional commands, e-mail: log4j-user-help@logging.apache.org


Re: dynamically adding to a log file name?

Posted by Maarten Bosteels <mb...@gmail.com>.
Why don't you just save the request and the response to a file ?
You do not need log4j for that, right ?

      DateFormat df = new SimpleDateFormat("yyyyMMdd-HHmmss-S");
      String filename = "/tmp/" + df.format(new Date()) + "-request.xml";
      FileWriter writer = new FileWriter(filename);
      writer.write(request);
      writer.close();

If you need to specify the charset use

    OutputStreamWriter writer = new OutputStreamWriter(new
FileOutputStream(filename), "UTF-8");


Maarten

On 11/9/06, Chris Cheshire <ch...@gmail.com> wrote:
>
> Hi,
>
> I have a process that sends an xml request to an external server and
> receives an xml response back. For purposes of debugging, I need to
> capture the request and response in separate files but with similar
> names so that I can pair them up easily.
>
> Currently I am using James Stauffer's DateFormatFileAppender (very
> useful James!!) with the filename set to
> '${log4j.log.dir}/'yyyy/MM/dd/'qresponse.'yyyyMMdd-HHmmss-S'.xml' (and
> similar for the request) to log each request and response to a
> separate file each time. (I do understand that there is potential for
> 2 requests at the same millisecond, but this is really for debugging
> only and not production, so I can pretty much guarantee that won't
> happen).
>
> By using the milliseconds in the filename I can get unique requests or
> responses, but each request and subsequent response will have slightly
> different timestamps. I might get two requests processed before I get
> the response back for the first, which makes it slightly difficult to
> match them up.
>
> I don't want to log the request and response to the same file, because
> then it is no longer a well formed XML document that I can look at in
> an XML browser.
>
> Is there some way I can dynamically add something (like my internal
> requestid) to the file names at log time such that I can pair the log
> files up, without forcefully finding the appender and reconfiguring it
> each time (which would be rather slow)?
>
> If all else fails I can add an XML comment to the end of the file via
> the pattern layout, but I am looking for something that will help for
> scan value in matching the files up.
>
> Thanks
>
> Chris
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: log4j-user-unsubscribe@logging.apache.org
> For additional commands, e-mail: log4j-user-help@logging.apache.org
>
>

Re: dynamically adding to a log file name?

Posted by Chris Cheshire <ch...@gmail.com>.
Doh....I got this solved, I used &lt; &gt; entities in the layout
specification and it worked.

Thanks for the help James, the MDC helps with the logging of a comment
at the end of the file, and that combined with your
DateFormatFileAppender is good enough for what I need.

Cheers

Chris

On 11/9/06, Chris Cheshire <ch...@gmail.com> wrote:
> On 11/9/06, James Stauffer <st...@gmail.com> wrote:
> > Search the archives for people who wanted a separate log per MDC value
> > (usually login name).
> >
>
> OK I found the MDC section in the manual and it looks to be what I
> need *except* I cannot create XML style comments in the log file to
> contain the MDC value.
>

---------------------------------------------------------------------
To unsubscribe, e-mail: log4j-user-unsubscribe@logging.apache.org
For additional commands, e-mail: log4j-user-help@logging.apache.org


Re: dynamically adding to a log file name?

Posted by Chris Cheshire <ch...@gmail.com>.
On 11/9/06, James Stauffer <st...@gmail.com> wrote:
> Search the archives for people who wanted a separate log per MDC value
> (usually login name).
>

OK I found the MDC section in the manual and it looks to be what I
need *except* I cannot create XML style comments in the log file to
contain the MDC value.

If the pattern layout is :
    <layout class="org.apache.log4j.PatternLayout">
      <param name="ConversionPattern" value="%m%n\<!-- %X{reqid} --\>%n"/>
    </layout>

(and without the < and >  quoted with the backslash)

I get this exception when the config is parsed :

[#|2006-11-09T10:52:04.051-0800|WARNING|sun-appserver-pe9.0|javax.enterprise.system.stream.err|_ThreadID=44;_ThreadName=Thread-104;_RequestID=47ed9af3-8ced-4d48-894c-180e74673195;|
org.xml.sax.SAXParseException: The value of attribute "value"
associated with an element type "param" must not contain the '<'
character.
        at com.sun.org.apache.xerces.internal.parsers.DOMParser.parse(DOMParser.java:264)
        at com.sun.org.apache.xerces.internal.jaxp.DocumentBuilderImpl.parse(DocumentBuilderImpl.java:292)
        at javax.xml.parsers.DocumentBuilder.parse(DocumentBuilder.java:172)
        at org.apache.log4j.xml.DOMConfigurator$1.parse(DOMConfigurator.java:598)
        at org.apache.log4j.xml.DOMConfigurator.doConfigure(DOMConfigurator.java:711)
        at org.apache.log4j.xml.DOMConfigurator.doConfigure(DOMConfigurator.java:604)
        at org.apache.log4j.xml.DOMConfigurator.configure(DOMConfigurator.java:733)

Yet the API for PatternLayout specifies "You are free to insert any
literal text within the conversion pattern."

I can't use the XMLLayout because I don't want to create an XML
formatted log file, I just want to dump what I have (which happens to
be XML) to a file and append the XML comment to the end.

Any suggestions?

---------------------------------------------------------------------
To unsubscribe, e-mail: log4j-user-unsubscribe@logging.apache.org
For additional commands, e-mail: log4j-user-help@logging.apache.org


Re: dynamically adding to a log file name?

Posted by James Stauffer <st...@gmail.com>.
Search the archives for people who wanted a separate log per MDC value
(usually login name).

On 11/9/06, Chris Cheshire <ch...@gmail.com> wrote:
> Thanks James, I'll look into this. I think I can guarantee a unique id
> as the requests are unique and the logging for the request and
> response are done in the same method.
>
> On 11/9/06, James Stauffer <st...@gmail.com> wrote:
> > You could look into including some MDC value in the filename (assuming
> > you can assign a unique id to each request/response pair).
> >
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: log4j-user-unsubscribe@logging.apache.org
> For additional commands, e-mail: log4j-user-help@logging.apache.org
>
>


-- 
James Stauffer        http://www.geocities.com/stauffer_james/
Are you good? Take the test at http://www.livingwaters.com/good/

---------------------------------------------------------------------
To unsubscribe, e-mail: log4j-user-unsubscribe@logging.apache.org
For additional commands, e-mail: log4j-user-help@logging.apache.org


Re: dynamically adding to a log file name?

Posted by Chris Cheshire <ch...@gmail.com>.
Thanks James, I'll look into this. I think I can guarantee a unique id
as the requests are unique and the logging for the request and
response are done in the same method.

On 11/9/06, James Stauffer <st...@gmail.com> wrote:
> You could look into including some MDC value in the filename (assuming
> you can assign a unique id to each request/response pair).
>

---------------------------------------------------------------------
To unsubscribe, e-mail: log4j-user-unsubscribe@logging.apache.org
For additional commands, e-mail: log4j-user-help@logging.apache.org


Re: dynamically adding to a log file name?

Posted by James Stauffer <st...@gmail.com>.
You could look into including some MDC value in the filename (assuming
you can assign a unique id to each request/response pair).

On 11/8/06, Chris Cheshire <ch...@gmail.com> wrote:
> Hi,
>
> I have a process that sends an xml request to an external server and
> receives an xml response back. For purposes of debugging, I need to
> capture the request and response in separate files but with similar
> names so that I can pair them up easily.
>
> Currently I am using James Stauffer's DateFormatFileAppender (very
> useful James!!) with the filename set to
> '${log4j.log.dir}/'yyyy/MM/dd/'qresponse.'yyyyMMdd-HHmmss-S'.xml' (and
> similar for the request) to log each request and response to a
> separate file each time. (I do understand that there is potential for
> 2 requests at the same millisecond, but this is really for debugging
> only and not production, so I can pretty much guarantee that won't
> happen).
>
> By using the milliseconds in the filename I can get unique requests or
> responses, but each request and subsequent response will have slightly
> different timestamps. I might get two requests processed before I get
> the response back for the first, which makes it slightly difficult to
> match them up.
>
> I don't want to log the request and response to the same file, because
> then it is no longer a well formed XML document that I can look at in
> an XML browser.
>
> Is there some way I can dynamically add something (like my internal
> requestid) to the file names at log time such that I can pair the log
> files up, without forcefully finding the appender and reconfiguring it
> each time (which would be rather slow)?
>
> If all else fails I can add an XML comment to the end of the file via
> the pattern layout, but I am looking for something that will help for
> scan value in matching the files up.
>
> Thanks
>
> Chris
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: log4j-user-unsubscribe@logging.apache.org
> For additional commands, e-mail: log4j-user-help@logging.apache.org
>
>


-- 
James Stauffer        http://www.geocities.com/stauffer_james/
Are you good? Take the test at http://www.livingwaters.com/good/

---------------------------------------------------------------------
To unsubscribe, e-mail: log4j-user-unsubscribe@logging.apache.org
For additional commands, e-mail: log4j-user-help@logging.apache.org