/** * Retrieves current access log file name from AccessLogValve to avoid accidental * exclusion. * * @param accessLogValve the access log valve * @param currentLogFileField the current log file field * @return A Supplier that knows how to retrieve the file name */ private static Supplier<String> createCurrentLogFileNameSupplier( final AccessLogValve accessLogValve, final Field currentLogFileField) { return () -> { String fileName = null; try { final File currentLogFile = (File) currentLogFileField .get(accessLogValve); if (currentLogFile != null) { fileName = currentLogFile.toPath().getFileName().toString(); } } catch (final Exception e) { log.warn(e.getMessage(), e); } return fileName; }; }
/** * Purge access log customizer embedded servlet container customizer. * * @param serverProperties the server properties * @param purgeProperties the purge properties * @return the embedded servlet container customizer */ @Bean public EmbeddedServletContainerCustomizer purgeAccessLogCustomizer( final ServerProperties serverProperties, final PurgeProperties purgeProperties) { return container -> { final TomcatEmbeddedServletContainerFactory factory = (TomcatEmbeddedServletContainerFactory) container; final Accesslog accesslog = serverProperties.getTomcat().getAccesslog(); factory.getEngineValves().stream() .filter(valve -> valve instanceof AccessLogValve) .map(valve -> (AccessLogValve) valve).findFirst() .ifPresent(valve -> { final TomcatPurgeAccessLogHolder accessLogHolder = new TomcatPurgeAccessLogHolder( purgeProperties, Paths.get(accesslog.getDirectory()), accesslog.getPrefix(), accesslog.getSuffix(), valve); factory.addContextCustomizers(accessLogHolder); }); }; }
private void addAccessLog(Tomcat httpServer, StandardContext context) { try { String accessLogLocation = serverData.getRootContext().getBean(AccessLogLocationBean.class).getAccessLogLocation(); accessLogLocation = accessLogLocation + "/" + replaceSlash(serverData.getModule().getContext()) + "-access.log"; AccessLogValve accessLogValve = new AccessLogValve(); accessLogValve.setDirectory(accessLogLocation); accessLogValve.setPattern(Constants.AccessLog.COMMON_ALIAS); accessLogValve.setSuffix(".log"); accessLogValve.setRotatable(true); context.getPipeline().addValve(accessLogValve); } catch (Exception e) { logger.error(InternalErrorCode.SERVER_STARTUP_FAILED_TO_CREATE_ACCESS_LOG.toString() + ": " + e.getMessage()); if (e.getCause() != null) logger.error("CAUSED BY: " + InternalErrorCode.SERVER_STARTUP_FAILED_TO_CREATE_ACCESS_LOG.toString() + ": " + e.getCause().getMessage()); } }
@Override public void setUpValve(Tomcat tomcat) { // clear AccessLogValve for (Valve vl : tomcat.getHost().getPipeline().getValves()) { if (vl.getClass().equals(AccessLogValve.class)) { tomcat.getHost().getPipeline().removeValve(vl); } } if (accessLogDirectory == null) { accessLogDirectory = new File(getBuildDirectory(), "logs").toString(); } AccessLogValve alv = new AccessLogValve(); alv.setDirectory(accessLogDirectory); alv.setPattern("combined"); tomcat.getHost().getPipeline().addValve(alv); }
@Override protected void setUpValve(Tomcat tomcat) throws UnknownHostException { // remove AccessLogValve for (Valve vl : tomcat.getHost().getPipeline().getValves()) { if (vl.getClass().equals(AccessLogValve.class)) { tomcat.getHost().getPipeline().removeValve(vl); } } mongoClient = new MongoClient(new MongoClientURI(url)); db = mongoClient.getDB(dbName); MongoAccessLogValve mavl = new MongoAccessLogValve(); mavl.setUri(url); mavl.setDbName(dbName); mavl.setCollName(collName); mavl.setPattern(pattern); tomcat.getHost().getPipeline().addValve(mavl); }
/** * アクセスログを構成する. * * @throws IOException */ protected void initAccessLog() throws IOException { // -------------------------------- // アクセスログを構成する. // -------------------------------- File accessLogDir = getLogsDir(); String logDir = accessLogDir.getAbsolutePath(); AccessLogValve accessLogValve = new AccessLogValve(); accessLogValve.setDirectory(logDir); accessLogValve.setPrefix("accesslog"); accessLogValve.setPattern( org.apache.catalina.valves.Constants.AccessLog.COMBINED_ALIAS ); accessLogValve.setFileDateFormat("yyyy-MM-dd"); accessLogValve.setSuffix(".log"); accessLogValve.setRenameOnRotate(true); accessLogValve.setRequestAttributesEnabled(true); accessLogValve.setBuffered(false); accessLogValve.setEnabled(true); ctx.addValve(accessLogValve); // ↓ エンジン単位に指定する場合 // ((StandardEngine) tomcat.getEngine()).addValve(accessLogValve); }
/** * Retrieves current log file field. * * @param accessLogValve the access log valve * @return the current log file field */ private static Field getCurrentLogFileField(final AccessLogValve accessLogValve) { final Field currentLogFileField = findField(accessLogValve.getClass(), CURRENT_LOG_FILE_FIELD, File.class); makeAccessible(currentLogFileField); return currentLogFileField; }
private AccessLogValve createMockedAccessLogValve(final Path currentAccessLogFile) { final AccessLogValve accessLogValve = new AccessLogValve(); final Field field = findField(AccessLogValve.class, CURRENT_LOG_FILE_FIELD); makeAccessible(field); setField(field, accessLogValve, currentAccessLogFile.toFile()); return accessLogValve; }
/** * Create a new AccessLoggerValve. * * @param parent MBean Name of the associated parent component * * @exception Exception if an MBean cannot be created or registered * * @deprecated Will be removed in Tomcat 8.0.x. Replaced by {@link * #createValve(String, String)}. */ @Deprecated public String createAccessLoggerValve(String parent) throws Exception { ObjectName pname = new ObjectName(parent); // Create a new AccessLogValve instance AccessLogValve accessLogger = new AccessLogValve(); ContainerBase containerBase = getParentContainerFromParent(pname); // Add the new instance to its parent component containerBase.getPipeline().addValve(accessLogger); ObjectName oname = accessLogger.getObjectName(); return (oname.toString()); }
/** * Create a new AccessLoggerValve. * * @param parent MBean Name of the associated parent component * * @exception Exception if an MBean cannot be created or registered */ public String createAccessLoggerValve(String parent) throws Exception { ObjectName pname = new ObjectName(parent); // Create a new AccessLogValve instance AccessLogValve accessLogger = new AccessLogValve(); ContainerBase containerBase = getParentContainerFromParent(pname); // Add the new instance to its parent component containerBase.addValve(accessLogger); ObjectName oname = accessLogger.getObjectName(); return (oname.toString()); }
/** * Create a new AccessLoggerValve. * * @param parent * MBean Name of the associated parent component * * @exception Exception * if an MBean cannot be created or registered * * @deprecated Will be removed in Tomcat 8.0.x. Replaced by * {@link #createValve(String, String)}. */ @Deprecated public String createAccessLoggerValve(String parent) throws Exception { ObjectName pname = new ObjectName(parent); // Create a new AccessLogValve instance AccessLogValve accessLogger = new AccessLogValve(); ContainerBase containerBase = getParentContainerFromParent(pname); // Add the new instance to its parent component containerBase.getPipeline().addValve(accessLogger); ObjectName oname = accessLogger.getObjectName(); return (oname.toString()); }
private void customizeAccessLog(TomcatEmbeddedServletContainerFactory factory) { AccessLogValve valve = new AccessLogValve(); valve.setPattern(this.accesslog.getPattern()); valve.setDirectory(this.accesslog.getDirectory()); valve.setPrefix(this.accesslog.getPrefix()); valve.setSuffix(this.accesslog.getSuffix()); valve.setRenameOnRotate(this.accesslog.isRenameOnRotate()); factory.addEngineValves(valve); }
private void customizeAccessLog(TomcatEmbeddedServletContainerFactory factory) { AccessLogValve valve = new AccessLogValve(); valve.setPattern(this.accesslog.getPattern()); valve.setDirectory(this.accesslog.getDirectory()); valve.setPrefix(this.accesslog.getPrefix()); valve.setSuffix(this.accesslog.getSuffix()); factory.addContextValves(valve); }
/** * Create a new AccessLoggerValve. * * @param parent MBean Name of the associated parent component * * @exception Exception if an MBean cannot be created or registered */ public String createAccessLoggerValve(String parent) throws Exception { ObjectName pname = new ObjectName(parent); // Create a new AccessLogValve instance AccessLogValve accessLogger = new AccessLogValve(); ContainerBase containerBase = getParentContainerFromParent(pname); // Add the new instance to its parent component containerBase.getPipeline().addValve(accessLogger); ObjectName oname = accessLogger.getObjectName(); return (oname.toString()); }
@Before @Override public void setUp() throws Exception { super.setUp(); // Trigger loading of catalina.properties CatalinaProperties.getProperty("foo"); File appBase = new File(getTemporaryDirectory(), "webapps"); if (!appBase.exists() && !appBase.mkdir()) { fail("Unable to create appBase for test"); } tomcat = new TomcatWithFastSessionIDs(); String protocol = getProtocol(); Connector connector = new Connector(protocol); // Listen only on localhost connector.setAttribute("address", InetAddress.getByName("localhost").getHostAddress()); // Use random free port connector.setPort(0); // Mainly set to reduce timeouts during async tests connector.setAttribute("connectionTimeout", "3000"); tomcat.getService().addConnector(connector); tomcat.setConnector(connector); // Add AprLifecycleListener if we are using the Apr connector if (protocol.contains("Apr")) { StandardServer server = (StandardServer) tomcat.getServer(); AprLifecycleListener listener = new AprLifecycleListener(); listener.setSSLRandomSeed("/dev/urandom"); server.addLifecycleListener(listener); connector.setAttribute("pollerThreadCount", Integer.valueOf(1)); } File catalinaBase = getTemporaryDirectory(); tomcat.setBaseDir(catalinaBase.getAbsolutePath()); tomcat.getHost().setAppBase(appBase.getAbsolutePath()); accessLogEnabled = Boolean.parseBoolean( System.getProperty("tomcat.test.accesslog", "false")); if (accessLogEnabled) { String accessLogDirectory = System .getProperty("tomcat.test.reports"); if (accessLogDirectory == null) { accessLogDirectory = new File(getBuildDirectory(), "logs") .toString(); } AccessLogValve alv = new AccessLogValve(); alv.setDirectory(accessLogDirectory); alv.setPattern("%h %l %u %t \"%r\" %s %b %I %D"); tomcat.getHost().getPipeline().addValve(alv); } // Cannot delete the whole tempDir, because logs are there, // but delete known subdirectories of it. addDeleteOnTearDown(new File(catalinaBase, "webapps")); addDeleteOnTearDown(new File(catalinaBase, "work")); }
@Before @Override public void setUp() throws Exception { super.setUp(); // Trigger loading of catalina.properties CatalinaProperties.getProperty("foo"); File appBase = new File(getTemporaryDirectory(), "webapps"); if (!appBase.exists() && !appBase.mkdir()) { fail("Unable to create appBase for test"); } tomcat = new TomcatWithFastSessionIDs(); String protocol = getProtocol(); Connector connector = new Connector(protocol); // Listen only on localhost connector.setAttribute("address", InetAddress.getByName("localhost").getHostAddress()); // Use random free port connector.setPort(0); // Mainly set to reduce timeouts during async tests connector.setAttribute("connectionTimeout", "3000"); tomcat.getService().addConnector(connector); tomcat.setConnector(connector); // Add AprLifecycleListener if we are using the Apr connector if (protocol.contains("Apr")) { StandardServer server = (StandardServer) tomcat.getServer(); AprLifecycleListener listener = new AprLifecycleListener(); listener.setSSLRandomSeed("/dev/urandom"); server.addLifecycleListener(listener); connector.setAttribute("pollerThreadCount", Integer.valueOf(1)); } File catalinaBase = getTemporaryDirectory(); tomcat.setBaseDir(catalinaBase.getAbsolutePath()); tomcat.getHost().setAppBase(appBase.getAbsolutePath()); accessLogEnabled = Boolean.parseBoolean( System.getProperty("tomcat.test.accesslog", "false")); if (accessLogEnabled) { AccessLogValve alv = new AccessLogValve(); alv.setDirectory(getBuildDirectory() + "/logs"); alv.setPattern("%h %l %u %t \"%r\" %s %b %I %D"); tomcat.getHost().getPipeline().addValve(alv); } // Cannot delete the whole tempDir, because logs are there, // but delete known subdirectories of it. addDeleteOnTearDown(new File(catalinaBase, "webapps")); addDeleteOnTearDown(new File(catalinaBase, "work")); }
@Before public void setUp() throws Exception { // Need to use JULI so log messages from the tests are visible System.setProperty("java.util.logging.manager", "org.apache.juli.ClassLoaderLogManager"); System.setProperty("java.util.logging.config.file", new File( getBuildDirectory(), "conf/logging.properties").toString()); tempDir = new File(System.getProperty("tomcat.test.temp", "output/tmp")); if (!tempDir.mkdirs() && !tempDir.isDirectory()) { fail("Unable to create temporary directory for test"); } System.setProperty("catalina.base", tempDir.getAbsolutePath()); // Trigger loading of catalina.properties CatalinaProperties.getProperty("foo"); File appBase = new File(tempDir, "webapps"); if (!appBase.exists() && !appBase.mkdir()) { fail("Unable to create appBase for test"); } tomcat = new TomcatWithFastSessionIDs(); String protocol = getProtocol(); Connector connector = new Connector(protocol); // If each test is running on same port - they // may interfere with each other connector.setPort(getNextPort()); // Mainly set to reduce timeouts during async tests connector.setAttribute("connectionTimeout", "3000"); tomcat.getService().addConnector(connector); tomcat.setConnector(connector); // Add AprLifecycleListener if we are using the Apr connector if (protocol.contains("Apr")) { StandardServer server = (StandardServer) tomcat.getServer(); AprLifecycleListener listener = new AprLifecycleListener(); listener.setSSLRandomSeed("/dev/urandom"); server.addLifecycleListener(listener); connector.setAttribute("pollerThreadCount", Integer.valueOf(1)); } tomcat.setBaseDir(tempDir.getAbsolutePath()); tomcat.getHost().setAppBase(appBase.getAbsolutePath()); accessLogEnabled = Boolean.parseBoolean( System.getProperty("tomcat.test.accesslog", "false")); if (accessLogEnabled) { AccessLogValve alv = new AccessLogValve(); alv.setDirectory(getBuildDirectory() + "/logs"); alv.setPattern("%h %l %u %t \"%r\" %s %b %I %D"); tomcat.getHost().getPipeline().addValve(alv); } }
/** * Instantiates a new Tomcat purge access log holder. * * @param purgeProperties the purge properties * @param directory the directory * @param prefix the prefix * @param suffix the suffix * @param accessLogValve the access log valve */ public TomcatPurgeAccessLogHolder(final PurgeProperties purgeProperties, final Path directory, final String prefix, final String suffix, final AccessLogValve accessLogValve) { super(purgeProperties, directory, prefix, suffix, createCurrentLogFileNameSupplier(accessLogValve, getCurrentLogFileField(accessLogValve))); }