Notice: this Wiki will be going read only early in 2024 and edits will no longer be possible. Please see: https://gitlab.eclipse.org/eclipsefdn/helpdesk/-/wikis/Wiki-shutdown-plan for the plan.
Difference between revisions of "Jetty/Tutorial/RequestLog"
(New page: {{Jetty Tutorial | introduction = Request logs are a record of the requests that the server has processed. There is one entry per request received, and commonly in the standard [http://hoo...) |
|||
(16 intermediate revisions by 5 users not shown) | |||
Line 1: | Line 1: | ||
{{Jetty Tutorial | {{Jetty Tutorial | ||
| introduction = | | introduction = | ||
+ | |||
+ | {{Jetty Redirect|http://www.eclipse.org/jetty/documentation/current/configuring-jetty-request-logs.html}} | ||
+ | |||
Request logs are a record of the requests that the server has processed. There is one entry per request received, and commonly in the standard [http://hoohoo.ncsa.uiuc.edu/docs/setup/httpd/LogOptions.html NCSA format] so they can be conveniently analysed by tools like [http://www.mrunix.net/webalizer/ webalizer]. | Request logs are a record of the requests that the server has processed. There is one entry per request received, and commonly in the standard [http://hoohoo.ncsa.uiuc.edu/docs/setup/httpd/LogOptions.html NCSA format] so they can be conveniently analysed by tools like [http://www.mrunix.net/webalizer/ webalizer]. | ||
| details = | | details = | ||
Line 14: | Line 17: | ||
</pre> | </pre> | ||
− | === | + | ===Implementations=== |
Jetty provides an implementation called NCSARequestLog which supports the NCSA format in files that can be rolled over on a daily basis. | Jetty provides an implementation called NCSARequestLog which supports the NCSA format in files that can be rolled over on a daily basis. | ||
The [http://logback.qos.ch logback project] offers [http://logback.qos.ch/access.html another implementation] of RequestLog interface providing very rich and powerful HTTP-access log functionality, | The [http://logback.qos.ch logback project] offers [http://logback.qos.ch/access.html another implementation] of RequestLog interface providing very rich and powerful HTTP-access log functionality, | ||
− | Alternatively, if neither of these options suits you, you can implement a custom request logger by implementing Jetty's [http:// | + | Alternatively, if neither of these options suits you, you can implement a custom request logger by implementing Jetty's [http://git.eclipse.org/c/jetty/org.eclipse.jetty.project.git/tree/jetty-server/src/main/java/org/eclipse/jetty/server/RequestLog.java RequestLog.java] interface and plugging it in in similar fashion to the NCSARequestLog, as shown below. |
− | ===Configuring | + | ===Configuring Request Log=== |
To configure a single request log for the entire Jetty Server instance: | To configure a single request log for the entire Jetty Server instance: | ||
Line 27: | Line 30: | ||
<source lang="xml"> | <source lang="xml"> | ||
<Set name="handler"> | <Set name="handler"> | ||
− | <New id="Handlers" class="org. | + | <New id="Handlers" class="org.eclipse.jetty.server.handler.HandlerCollection"> |
<Set name="handlers"> | <Set name="handlers"> | ||
− | <Array type="org. | + | <Array type="org.eclipse.jetty.server.Handler"> |
<Item> | <Item> | ||
− | <New id="Contexts" class="org. | + | <New id="Contexts" class="org.eclipse.jetty.server.handler.ContextHandlerCollection"/> |
</Item> | </Item> | ||
<Item> | <Item> | ||
− | <New id="DefaultHandler" class="org. | + | <New id="DefaultHandler" class="org.eclipse.jetty.server.handler.DefaultHandler"/> |
</Item> | </Item> | ||
<Item> | <Item> | ||
− | <New id="RequestLog" class="org. | + | <New id="RequestLog" class="org.eclipse.jetty.server.handler.RequestLogHandler"/> |
</Item> | </Item> | ||
</Array> | </Array> | ||
Line 45: | Line 48: | ||
<Ref id="RequestLog"> | <Ref id="RequestLog"> | ||
<Set name="requestLog"> | <Set name="requestLog"> | ||
− | <New id="RequestLogImpl" class="org. | + | <New id="RequestLogImpl" class="org.eclipse.jetty.server.NCSARequestLog"> |
<Arg><SystemProperty name="jetty.logs" default="./logs"/>/yyyy_mm_dd.request.log</Arg> | <Arg><SystemProperty name="jetty.logs" default="./logs"/>/yyyy_mm_dd.request.log</Arg> | ||
<Set name="retainDays">90</Set> | <Set name="retainDays">90</Set> | ||
Line 54: | Line 57: | ||
</Set> | </Set> | ||
</Ref> | </Ref> | ||
+ | </source> | ||
The equivalent code is: | The equivalent code is: | ||
− | <source | + | <source lang="java"> |
− | + | HandlerCollection handlers = new HandlerCollection(); | |
− | + | ContextHandlerCollection contexts = new ContextHandlerCollection(); | |
− | + | RequestLogHandler requestLogHandler = new RequestLogHandler(); | |
− | + | handlers.setHandlers(new Handler[]{contexts,new DefaultHandler(),requestLogHandler}); | |
− | + | server.setHandler(handlers); | |
− | + | NCSARequestLog requestLog = new NCSARequestLog("./logs/jetty-yyyy_mm_dd.request.log"); | |
− | + | requestLog.setRetainDays(90); | |
− | + | requestLog.setAppend(true); | |
− | + | requestLog.setExtended(false); | |
− | + | requestLog.setLogTimeZone("GMT"); | |
− | + | requestLogHandler.setRequestLog(requestLog); | |
</source> | </source> | ||
This configures a request log in $JETTY_HOME/logs with filenames including the date. Old log files are kept for 90 days before being deleted. Existing log files are appended to and the extended NCSA format is used in the GMT timezone. | This configures a request log in $JETTY_HOME/logs with filenames including the date. Old log files are kept for 90 days before being deleted. Existing log files are appended to and the extended NCSA format is used in the GMT timezone. | ||
− | There are many more configuration options available - see [http:// | + | There are many more configuration options available - see [http://download.eclipse.org/jetty/stable-7/apidocs/org/eclipse/jetty/server/NCSARequestLog.html NCSARequestLog.java] |
| more = | | more = | ||
− | ===Configuring | + | ===Configuring Separate Request Log For Web Application=== |
+ | |||
+ | The following configuration should be added to the <context>.xml file in order to configure a separate request log for a web application. | ||
<source lang="xml"> | <source lang="xml"> | ||
− | <Configure class="org. | + | <Configure class="org.eclipse.jetty.webapp.WebAppContext"> |
... | ... | ||
− | < | + | <Set name="handler"> |
− | + | <New id="RequestLog" class="org.eclipse.jetty.server.handler.RequestLogHandler"> | |
− | + | <Set name="requestLog"> | |
− | + | <New id="RequestLogImpl" class="org.eclipse.jetty.server.NCSARequestLog"> | |
− | + | <Set name="filename"><Property name="jetty.logs" default="./logs"/>/test-yyyy_mm_dd.request.log</Set> | |
− | + | <Set name="filenameDateFormat">yyyy_MM_dd</Set> | |
− | + | <Set name="LogTimeZone">GMT</Set> | |
− | + | <Set name="retainDays">90</Set> | |
− | + | <Set name="append">true</Set> | |
− | + | </New> | |
− | + | </Set> | |
− | + | </New> | |
− | + | </Set> | |
− | + | ||
− | </ | + | |
... | ... | ||
</Configure> | </Configure> | ||
</source> | </source> | ||
+ | |||
+ | ===Configuring time format in CustomRequestLog=== | ||
+ | If you are using [https://www.eclipse.org/jetty/javadoc/jetty-11/org/eclipse/jetty/server/CustomRequestLog.html CustomRequestLog] in embedded jetty. CustomRequestLog takes a format string as documented in the javadocs. | ||
+ | Some examples can be gleaned from CustomRequestLog source codes: | ||
+ | <source lang="java"> | ||
+ | public static final String DEFAULT_DATE_FORMAT = "dd/MMM/yyyy:HH:mm:ss ZZZ"; | ||
+ | public static final String NCSA_FORMAT = "%{client}a - %u %t \"%r\" %s %O"; | ||
+ | public static final String EXTENDED_NCSA_FORMAT = NCSA_FORMAT + " \"%{Referer}i\" \"%{User-Agent}i\""; | ||
+ | </source> | ||
+ | |||
+ | What is a little less obvious is how to use the %t time format spec. The time format spec takes a form of | ||
+ | <pre>%{format|timeZone|locale}t</pre> | ||
+ | Those '|' characters and the braces around the parameter are necessary. For example, to specify a different timezone. the format string looks this way: | ||
+ | <pre> | ||
+ | "%{client}a - %u %{dd/MMM/yyyy:HH:mm:ss ZZZ|GMT+5}t \"%r\" %s %O" | ||
+ | </pre> | ||
+ | |||
| category = [[Category:Jetty Tutorial]] | | category = [[Category:Jetty Tutorial]] | ||
}} | }} |
Latest revision as of 08:51, 12 September 2021
Contents
Introduction
Request logs are a record of the requests that the server has processed. There is one entry per request received, and commonly in the standard NCSA format so they can be conveniently analysed by tools like webalizer.
Details
Request Log Entry
A standard request log entry includes the client IP, date, method, URL, result, size, referrer and user agent. eg:
123.4.5.6 - - [27/Aug/2004:10:16:17 +0000] "GET /jetty/tut/XmlConfiguration.html HTTP/1.1" 200 76793 "http://localhost:8080/jetty/tut/logging.html" "Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.6) Gecko/20040614 Firefox/0.8"
Implementations
Jetty provides an implementation called NCSARequestLog which supports the NCSA format in files that can be rolled over on a daily basis.
The logback project offers another implementation of RequestLog interface providing very rich and powerful HTTP-access log functionality,
Alternatively, if neither of these options suits you, you can implement a custom request logger by implementing Jetty's RequestLog.java interface and plugging it in in similar fashion to the NCSARequestLog, as shown below.
Configuring Request Log
To configure a single request log for the entire Jetty Server instance:
<Set name="handler"> <New id="Handlers" class="org.eclipse.jetty.server.handler.HandlerCollection"> <Set name="handlers"> <Array type="org.eclipse.jetty.server.Handler"> <Item> <New id="Contexts" class="org.eclipse.jetty.server.handler.ContextHandlerCollection"/> </Item> <Item> <New id="DefaultHandler" class="org.eclipse.jetty.server.handler.DefaultHandler"/> </Item> <Item> <New id="RequestLog" class="org.eclipse.jetty.server.handler.RequestLogHandler"/> </Item> </Array> </Set> </New> </Set> <Ref id="RequestLog"> <Set name="requestLog"> <New id="RequestLogImpl" class="org.eclipse.jetty.server.NCSARequestLog"> <Arg><SystemProperty name="jetty.logs" default="./logs"/>/yyyy_mm_dd.request.log</Arg> <Set name="retainDays">90</Set> <Set name="append">true</Set> <Set name="extended">false</Set> <Set name="LogTimeZone">GMT</Set> </New> </Set> </Ref>
The equivalent code is:
HandlerCollection handlers = new HandlerCollection(); ContextHandlerCollection contexts = new ContextHandlerCollection(); RequestLogHandler requestLogHandler = new RequestLogHandler(); handlers.setHandlers(new Handler[]{contexts,new DefaultHandler(),requestLogHandler}); server.setHandler(handlers); NCSARequestLog requestLog = new NCSARequestLog("./logs/jetty-yyyy_mm_dd.request.log"); requestLog.setRetainDays(90); requestLog.setAppend(true); requestLog.setExtended(false); requestLog.setLogTimeZone("GMT"); requestLogHandler.setRequestLog(requestLog);
This configures a request log in $JETTY_HOME/logs with filenames including the date. Old log files are kept for 90 days before being deleted. Existing log files are appended to and the extended NCSA format is used in the GMT timezone.
There are many more configuration options available - see NCSARequestLog.java
Additional Resources
Configuring Separate Request Log For Web Application
The following configuration should be added to the <context>.xml file in order to configure a separate request log for a web application.
<Configure class="org.eclipse.jetty.webapp.WebAppContext"> ... <Set name="handler"> <New id="RequestLog" class="org.eclipse.jetty.server.handler.RequestLogHandler"> <Set name="requestLog"> <New id="RequestLogImpl" class="org.eclipse.jetty.server.NCSARequestLog"> <Set name="filename"><Property name="jetty.logs" default="./logs"/>/test-yyyy_mm_dd.request.log</Set> <Set name="filenameDateFormat">yyyy_MM_dd</Set> <Set name="LogTimeZone">GMT</Set> <Set name="retainDays">90</Set> <Set name="append">true</Set> </New> </Set> </New> </Set> ... </Configure>
Configuring time format in CustomRequestLog
If you are using CustomRequestLog in embedded jetty. CustomRequestLog takes a format string as documented in the javadocs. Some examples can be gleaned from CustomRequestLog source codes:
public static final String DEFAULT_DATE_FORMAT = "dd/MMM/yyyy:HH:mm:ss ZZZ"; public static final String NCSA_FORMAT = "%{client}a - %u %t \"%r\" %s %O"; public static final String EXTENDED_NCSA_FORMAT = NCSA_FORMAT + " \"%{Referer}i\" \"%{User-Agent}i\"";
What is a little less obvious is how to use the %t time format spec. The time format spec takes a form of
%{format|timeZone|locale}t
Those '|' characters and the braces around the parameter are necessary. For example, to specify a different timezone. the format string looks this way:
"%{client}a - %u %{dd/MMM/yyyy:HH:mm:ss ZZZ|GMT+5}t \"%r\" %s %O"