Class RequestLogImpl
- java.lang.Object
-
- ch.qos.logback.core.ContextBase
-
- ch.qos.logback.access.jetty.RequestLogImpl
-
- All Implemented Interfaces:
Context,AppenderAttachable<IAccessEvent>,FilterAttachable<IAccessEvent>,LifeCycle,PropertyContainer,org.eclipse.jetty.server.RequestLog,org.eclipse.jetty.util.component.LifeCycle
public class RequestLogImpl extends ContextBase implements org.eclipse.jetty.server.RequestLog, org.eclipse.jetty.util.component.LifeCycle, AppenderAttachable<IAccessEvent>, FilterAttachable<IAccessEvent>
This class is logback's implementation of jetty's RequestLog interface.It can be seen as logback classic's LoggerContext. Appenders can be attached directly to RequestLogImpl and RequestLogImpl uses the same StatusManager as LoggerContext does. It also provides containers for properties.
Supported Jetty Versions
This
RequestLogImplonly supports Jetty 7.0.0 through Jetty 10. If you are using Jetty 11 with the new Jakarta Servlets (namespacejakarta.servlet) then you will need a more modern version oflogback-access.Configuring for Jetty 9.4.x through to Jetty 10.0.x
Jetty 9.4.x and Jetty 10.x use a modern @{code server.setRequestLog(RequestLog)} interface that is based on a Server level RequestLog behavior. This means all requests are logged, even bad requests, and context-less requests. The internals of the Jetty Request and Response objects track the state of the object at the time they are committed (the actual state during the application when an action on the network commits the request/response exchange). This prevents behaviors from 3rd party libraries that change the state of the request / response before the RequestLog gets a chance to log the details. This differs from Jetty 9.3.x and older in that those versions used a (now deprecated)
RequestLogHandlerand would never see bad requests, or context-less requests, and if a 3rd party library modifies the the response (for example by settingresponse.setStatus(200)after the response has been initiated on the network) this change in status would be logged, instead of the actual status that was sent.First, you must be using the proper
${jetty.home}and${jetty.base}directory split. Configure your${jetty.base}with at least the `resources` module enabled (so that your configuration can be found).Next, create a
${jetty.base}/etc/logback-access-requestlog.xmlfile with the following content.<?xml version="1.0"?> <!DOCTYPE Configure PUBLIC "-//Jetty//Configure//EN" "http://www.eclipse.org/jetty/configure_9_3.dtd"> <Configure id="Server" class="org.eclipse.jetty.server.Server"> <Set name="requestLog"> <New id="LogbackAccess" class="ch.qos.logback.access.jetty.RequestLogImpl"> <Set name="resource">logback-access.xml</Set> </New> </Set> </Configure>Now you'll need a
By default,${jetty.base}/resources/logback-access.xmlconfiguration file.RequestLogImpllooks for a logback configuration file calledetc/logback-access.xml, in the${jetty.base}directory, then the older${jetty.home}directory. Thelogback-access.xmlfile is slightly different than the usual logback classic configuration file. Most of it is the same: Appenders and Layouts are declared the exact same way. However, loggers elements are not allowed.It is possible to put the logback configuration file anywhere, as long as it's path is specified. Here is another example, with a path to the logback-access.xml file.
<?xml version="1.0"?> <!DOCTYPE Configure PUBLIC "-//Jetty//Configure//EN" "http://www.eclipse.org/jetty/configure_9_3.dtd"> <Configure id="Server" class="org.eclipse.jetty.server.Server"> <Set name="requestLog"> <New id="LogbackAccess" class="ch.qos.logback.access.jetty.RequestLogImpl"> <Set name="fileName">/path/to/logback-access.xml</Set> </New> </Set> </Configure>Configuring for Jetty 7.x thru to Jetty 9.3.x
To configure these older Jetty instances to use
RequestLogImpl, the use of theRequestLogHandleris the technique available to you. Modify youretc/jetty-requestlog.xml<?xml version="1.0"?> <!DOCTYPE Configure PUBLIC "-//Jetty//Configure//EN" "http://www.eclipse.org/jetty/configure.dtd"> <Configure id="Server" class="org.eclipse.jetty.server.Server"> <Ref id="Handlers"> <Call name="addHandler"> <Arg> <New id="RequestLog" class="org.eclipse.jetty.server.handler.RequestLogHandler"> <Set name="requestLog"> <New id="RequestLogImpl" class="ch.qos.logback.access.jetty.RequestLogImpl"/> </Set> </New> </Arg> </Call> </Ref> </Configure>By default, RequestLogImpl looks for a logback configuration file called logback-access.xml, in the same folder where jetty.xml is located, that is etc/logback-access.xml. The logback-access.xml file is slightly different than the usual logback classic configuration file. Most of it is the same: Appenders and Layouts are declared the exact same way. However, loggers elements are not allowed.It is possible to put the logback configuration file anywhere, as long as it's path is specified. Here is another example, with a path to the logback-access.xml file.
<?xml version="1.0"?> <!DOCTYPE Configure PUBLIC "-//Jetty//Configure//EN" "http://www.eclipse.org/jetty/configure.dtd"> <Configure id="Server" class="org.eclipse.jetty.server.Server"> <Ref id="Handlers"> <Call name="addHandler"> <Arg> <New id="RequestLog" class="org.eclipse.jetty.server.handler.RequestLogHandler"> <Set name="requestLog"> <New id="RequestLogImpl" class="ch.qos.logback.access.jetty.RequestLogImpl"> <Set name="fileName">path/to/logback-access.xml</Set> </New> </Set> </New> </Arg> </Call> </Ref> </Configure>Here is a sample logback-access.xml file that can be used right away:
<configuration> <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender"> <layout class="ch.qos.logback.access.PatternLayout"> <param name="Pattern" value="%date %server %remoteIP %clientHost %user %requestURL" /> </layout> </appender> <appender-ref ref="STDOUT" /> </configuration>Another configuration file, using SMTPAppender, could be:
<configuration> <appender name="SMTP" class="ch.qos.logback.access.net.SMTPAppender"> <layout class="ch.qos.logback.access.PatternLayout"> <param name="pattern" value="%remoteIP [%date] %requestURL %statusCode %bytesSent" /> </layout> <param name="From" value="sender@domaine.org" /> <param name="SMTPHost" value="mail.domain.org" /> <param name="Subject" value="Last Event: %statusCode %requestURL" /> <param name="To" value="server_admin@domain.org" /> </appender> <appender-ref ref="SMTP" /> </configuration>
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description (package private) static classRequestLogImpl.State
-
Field Summary
Fields Modifier and Type Field Description (package private) AppenderAttachableImpl<IAccessEvent>aaistatic java.lang.StringDEFAULT_CONFIG_FILE(package private) FilterAttachableImpl<IAccessEvent>fai(package private) java.lang.StringfileName(package private) booleanmodernJettyRequestLog(package private) booleanquiet(package private) java.lang.Stringresource(package private) RequestLogImpl.Statestate-
Fields inherited from class ch.qos.logback.core.ContextBase
scheduledFutures
-
-
Constructor Summary
Constructors Constructor Description RequestLogImpl()
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description voidaddAppender(Appender<IAccessEvent> newAppender)Add an appender.private voidaddError(java.lang.String msg)voidaddFilter(Filter<IAccessEvent> newFilter)Add a filter.protected voidaddInfo(java.lang.String msg)voidaddLifeCycleListener(org.eclipse.jetty.util.component.LifeCycle.Listener listener)private booleanclassIsPresent(java.lang.String className)voidclearAllFilters()protected voidconfigure()voiddetachAndStopAllAppenders()Detach and processPriorToRemoval all previously added appenders.booleandetachAppender(Appender<IAccessEvent> appender)Detach the appender passed as parameter from the list of appenders.booleandetachAppender(java.lang.String name)Detach the appender with the name passed as parameter from the list of appenders.Appender<IAccessEvent>getAppender(java.lang.String name)Get an appender by name.protected java.net.URLgetConfigurationFileURL()java.util.List<Filter<IAccessEvent>>getCopyOfAttachedFiltersList()Get a copy of all the filters contained within this FilterAttachable object.FilterReplygetFilterChainDecision(IAccessEvent event)Loop through the filters in the chain.booleanisAttached(Appender<IAccessEvent> appender)Returnstrueif the specified appender is in list of attached attached,falseotherwise.booleanisFailed()booleanisQuiet()booleanisRunning()booleanisStarted()booleanisStarting()booleanisStopped()booleanisStopping()java.util.Iterator<Appender<IAccessEvent>>iteratorForAppenders()Get an iterator for appenders contained in the parent object.voidlog(org.eclipse.jetty.server.Request jettyRequest, org.eclipse.jetty.server.Response jettyResponse)private JettyServerAdapternewJettyServerAdapter(org.eclipse.jetty.server.Request jettyRequest, org.eclipse.jetty.server.Response jettyResponse)voidremoveLifeCycleListener(org.eclipse.jetty.util.component.LifeCycle.Listener listener)private voidrunJoranOnFile(java.net.URL configURL)voidsetFileName(java.lang.String fileName)voidsetQuiet(boolean quiet)voidsetResource(java.lang.String resource)voidstart()voidstop()-
Methods inherited from class ch.qos.logback.core.ContextBase
addScheduledFuture, getBirthTime, getConfigurationLock, getCopyOfPropertyMap, getExecutorService, getName, getObject, getProperty, getScheduledExecutorService, getScheduledFutures, getStatusManager, initCollisionMaps, putObject, putProperty, register, removeObject, reset, setName, setStatusManager, toString
-
-
-
-
Field Detail
-
DEFAULT_CONFIG_FILE
public static final java.lang.String DEFAULT_CONFIG_FILE
-
state
RequestLogImpl.State state
-
aai
AppenderAttachableImpl<IAccessEvent> aai
-
fai
FilterAttachableImpl<IAccessEvent> fai
-
fileName
java.lang.String fileName
-
resource
java.lang.String resource
-
modernJettyRequestLog
boolean modernJettyRequestLog
-
quiet
boolean quiet
-
-
Method Detail
-
classIsPresent
private boolean classIsPresent(java.lang.String className)
-
log
public void log(org.eclipse.jetty.server.Request jettyRequest, org.eclipse.jetty.server.Response jettyResponse)- Specified by:
login interfaceorg.eclipse.jetty.server.RequestLog
-
newJettyServerAdapter
private JettyServerAdapter newJettyServerAdapter(org.eclipse.jetty.server.Request jettyRequest, org.eclipse.jetty.server.Response jettyResponse)
-
addInfo
protected void addInfo(java.lang.String msg)
-
addError
private void addError(java.lang.String msg)
-
start
public void start()
- Specified by:
startin interfaceLifeCycle- Specified by:
startin interfaceorg.eclipse.jetty.util.component.LifeCycle- Overrides:
startin classContextBase
-
configure
protected void configure()
-
getConfigurationFileURL
protected java.net.URL getConfigurationFileURL()
-
runJoranOnFile
private void runJoranOnFile(java.net.URL configURL)
-
stop
public void stop()
- Specified by:
stopin interfaceLifeCycle- Specified by:
stopin interfaceorg.eclipse.jetty.util.component.LifeCycle- Overrides:
stopin classContextBase
-
isRunning
public boolean isRunning()
- Specified by:
isRunningin interfaceorg.eclipse.jetty.util.component.LifeCycle
-
setFileName
public void setFileName(java.lang.String fileName)
-
setResource
public void setResource(java.lang.String resource)
-
isStarted
public boolean isStarted()
- Specified by:
isStartedin interfaceLifeCycle- Specified by:
isStartedin interfaceorg.eclipse.jetty.util.component.LifeCycle- Overrides:
isStartedin classContextBase
-
isStarting
public boolean isStarting()
- Specified by:
isStartingin interfaceorg.eclipse.jetty.util.component.LifeCycle
-
isStopping
public boolean isStopping()
- Specified by:
isStoppingin interfaceorg.eclipse.jetty.util.component.LifeCycle
-
isStopped
public boolean isStopped()
- Specified by:
isStoppedin interfaceorg.eclipse.jetty.util.component.LifeCycle
-
isFailed
public boolean isFailed()
- Specified by:
isFailedin interfaceorg.eclipse.jetty.util.component.LifeCycle
-
isQuiet
public boolean isQuiet()
-
setQuiet
public void setQuiet(boolean quiet)
-
addAppender
public void addAppender(Appender<IAccessEvent> newAppender)
Description copied from interface:AppenderAttachableAdd an appender.- Specified by:
addAppenderin interfaceAppenderAttachable<IAccessEvent>
-
iteratorForAppenders
public java.util.Iterator<Appender<IAccessEvent>> iteratorForAppenders()
Description copied from interface:AppenderAttachableGet an iterator for appenders contained in the parent object.- Specified by:
iteratorForAppendersin interfaceAppenderAttachable<IAccessEvent>
-
getAppender
public Appender<IAccessEvent> getAppender(java.lang.String name)
Description copied from interface:AppenderAttachableGet an appender by name.- Specified by:
getAppenderin interfaceAppenderAttachable<IAccessEvent>
-
isAttached
public boolean isAttached(Appender<IAccessEvent> appender)
Description copied from interface:AppenderAttachableReturnstrueif the specified appender is in list of attached attached,falseotherwise.- Specified by:
isAttachedin interfaceAppenderAttachable<IAccessEvent>
-
detachAndStopAllAppenders
public void detachAndStopAllAppenders()
Description copied from interface:AppenderAttachableDetach and processPriorToRemoval all previously added appenders.- Specified by:
detachAndStopAllAppendersin interfaceAppenderAttachable<IAccessEvent>
-
detachAppender
public boolean detachAppender(Appender<IAccessEvent> appender)
Description copied from interface:AppenderAttachableDetach the appender passed as parameter from the list of appenders.- Specified by:
detachAppenderin interfaceAppenderAttachable<IAccessEvent>
-
detachAppender
public boolean detachAppender(java.lang.String name)
Description copied from interface:AppenderAttachableDetach the appender with the name passed as parameter from the list of appenders.- Specified by:
detachAppenderin interfaceAppenderAttachable<IAccessEvent>
-
addFilter
public void addFilter(Filter<IAccessEvent> newFilter)
Description copied from interface:FilterAttachableAdd a filter.- Specified by:
addFilterin interfaceFilterAttachable<IAccessEvent>
-
clearAllFilters
public void clearAllFilters()
- Specified by:
clearAllFiltersin interfaceFilterAttachable<IAccessEvent>
-
getCopyOfAttachedFiltersList
public java.util.List<Filter<IAccessEvent>> getCopyOfAttachedFiltersList()
Description copied from interface:FilterAttachableGet a copy of all the filters contained within this FilterAttachable object.- Specified by:
getCopyOfAttachedFiltersListin interfaceFilterAttachable<IAccessEvent>- Returns:
- all attached filters as a list
-
getFilterChainDecision
public FilterReply getFilterChainDecision(IAccessEvent event)
Description copied from interface:FilterAttachableLoop through the filters in the chain. As soon as a filter decides on ACCEPT or DENY, then that value is returned. If all of the filters return NEUTRAL, then NEUTRAL is returned.- Specified by:
getFilterChainDecisionin interfaceFilterAttachable<IAccessEvent>
-
addLifeCycleListener
public void addLifeCycleListener(org.eclipse.jetty.util.component.LifeCycle.Listener listener)
- Specified by:
addLifeCycleListenerin interfaceorg.eclipse.jetty.util.component.LifeCycle
-
removeLifeCycleListener
public void removeLifeCycleListener(org.eclipse.jetty.util.component.LifeCycle.Listener listener)
- Specified by:
removeLifeCycleListenerin interfaceorg.eclipse.jetty.util.component.LifeCycle
-
-