Skip to main content

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [List Home]
Re: [jetty-users] NCSA request logging does not work for me

Thanks a million for the super-helpful response!

On Tue, Jul 1, 2014 at 7:38 PM, Joakim Erdfelt <joakim@xxxxxxxxxxx> wrote:
> Can't put the RequestLogHandler after the DefaultHandler.
> It will never be called, as DefaultHandler is the fallback for all unhandled
> prior requests. :-)
>
> Best if you setup a hierarchy for your handlers instead.
>
> server
>   + -- RequestLogHandler
>           + -- ResourceHandler
>           + -- DefaultHandler
>
> Like this ...
> https://gist.github.com/joakime/27c42782c9a2712a1010
>
> package jetty;
>
> import java.io.File;
> import java.net.InetSocketAddress;
>
> import org.eclipse.jetty.server.Handler;
> import org.eclipse.jetty.server.NCSARequestLog;
> import org.eclipse.jetty.server.Server;
> import org.eclipse.jetty.server.ServerConnector;
> import org.eclipse.jetty.server.handler.DefaultHandler;
> import org.eclipse.jetty.server.handler.HandlerList;
> import org.eclipse.jetty.server.handler.RequestLogHandler;
> import org.eclipse.jetty.server.handler.ResourceHandler;
>
> public class RequestLogging
> {
>     public static void main(String[] args) throws Exception
>     {
>         Server server = new Server();
>         ServerConnector connector = new ServerConnector(server);
>         connector.setHost("localhost");
>         connector.setPort(8765);
>         server.addConnector(connector);
>
>         // Setup directories and file references
>         File userHomeDir = new File(System.getProperty("user.home"));
>         File tmpDir = new File(userHomeDir, "tmp");
>         File baseResourceDir = new File(tmpDir, "docroot");
>         baseResourceDir.mkdirs();
>         File logFile = new File(tmpDir, "access.log");
>         logFile.getParentFile().mkdirs();
>
>         // Create resource handler (for serving static content)
>         ResourceHandler resourceHandler = new ResourceHandler();
>         resourceHandler.setDirectoriesListed(true);
>         resourceHandler.setWelcomeFiles(new String[] { "index.html" });
>         resourceHandler.setResourceBase(baseResourceDir.getAbsolutePath());
>
>         // Create request log handler (access log)
>         NCSARequestLog requestLog = new
> NCSARequestLog(logFile.getAbsolutePath());
>         requestLog.setExtended(true);
>         RequestLogHandler requestLogHandler = new RequestLogHandler();
>         requestLogHandler.setRequestLog(requestLog);
>         requestLog.setAppend(true);
>
>         // Create main handler list (the meat of what is being done)
>         HandlerList mainHandlers = new HandlerList();
>         mainHandlers.addHandler(resourceHandler);
>         // default for all prior unhandled requests
>         mainHandlers.addHandler(new DefaultHandler());
>
>         // Make main handler list be tracked by request logging
>         requestLogHandler.setHandler(mainHandlers);
>
>         // Setup top level handler list, what the server uses
>         HandlerList topLevelHandlers = new HandlerList();
>         // NOTE: If you want some handlers to exist, and not be tracked by
>         // the request log handler (such as rewrites), then declare them
>         // outside of the scop of the requestLogHandler.
>         // Example: baseHandler.addHandler(rewriteHandler);
>         topLevelHandlers.addHandler(requestLogHandler);
>
>         // Add top level handler list to server
>         server.setHandler(topLevelHandlers);
>
>         server.start();
>         server.join();
>     }
> }
>
>
>
>
> --
> Joakim Erdfelt <joakim@xxxxxxxxxxx>
> webtide.com - intalio.com/jetty
> Expert advice, services and support from from the Jetty & CometD experts
> eclipse.org/jetty - cometd.org
>
>
> On Tue, Jul 1, 2014 at 3:23 AM, Robert Krüger <krueger@xxxxxxxxxxx> wrote:
>>
>> I am trying to get a jetty instance, that I set up programmatically,
>> to write an access log. This is the code I use:
>>
>> public class FileServer
>> {
>>   public static void main(String[] args) throws Exception
>>   {
>>     final InetSocketAddress serverAddress = new
>> InetSocketAddress("localhost", 8765);
>>     Server server = new Server(serverAddress);
>>
>>     ResourceHandler resourceHandler = new ResourceHandler();
>>     resourceHandler.setDirectoriesListed(true);
>>     resourceHandler.setWelcomeFiles(new String[]{"index.html"});
>>
>>     resourceHandler.setResourceBase("/Users/krueger/tmp");
>>
>>     final File logFile = new
>> File("/Users/krueger/Library/Logs/StreamingTest/access.log");
>>     logFile.getParentFile().mkdirs();
>>     final NCSARequestLog requestLog = new
>> NCSARequestLog(logFile.getAbsolutePath());
>>     requestLog.setExtended(true);
>>     final RequestLogHandler requestLogHandler = new RequestLogHandler();
>>     requestLogHandler.setRequestLog(requestLog);
>>     requestLog.setAppend(true);
>>
>>     HandlerList handlers = new HandlerList();
>>     handlers.setHandlers(new Handler[] { resourceHandler, new
>> DefaultHandler(), requestLogHandler });
>>     server.setHandler(handlers);
>>
>>     server.start();
>>     server.join();
>>   }
>> }
>>
>> The server works as expected and the log file is created but no
>> requests are logged. The file remains empty. What am I missing?
>>
>> Thanks in advance,
>>
>> Robert
>> _______________________________________________
>> jetty-users mailing list
>> jetty-users@xxxxxxxxxxx
>> To change your delivery options, retrieve your password, or unsubscribe
>> from this list, visit
>> https://dev.eclipse.org/mailman/listinfo/jetty-users
>
>
>
> _______________________________________________
> jetty-users mailing list
> jetty-users@xxxxxxxxxxx
> To change your delivery options, retrieve your password, or unsubscribe from
> this list, visit
> https://dev.eclipse.org/mailman/listinfo/jetty-users



-- 
Robert Krüger
Managing Partner
Lesspain GmbH & Co. KG

www.lesspain-software.com


Back to the top