Refine search
protected void setupAccessLogIfNeeds(Context ctx) { if (accessLogOption != null && ctx instanceof StandardContext) { // also check context type just in case final StandardContext stdctx = (StandardContext) ctx; final AccessLogValve valve = new AccessLogValve(); accessLogOption.getLogDir().ifPresent(dir -> valve.setDirectory(dir)); accessLogOption.getFilePrefix().ifPresent(prefix -> valve.setPrefix(prefix)); accessLogOption.getFileSuffix().ifPresent(suffix -> valve.setSuffix(suffix)); accessLogOption.getFileDateFormat().ifPresent(format -> valve.setFileDateFormat(format)); valve.setEncoding(accessLogOption.getFileEncoding().orElse("UTF-8")); valve.setPattern(accessLogOption.getFormatPattern().orElse("common")); accessLogOption.getConditionIf().ifPresent(cond -> valve.setConditionIf(cond)); accessLogOption.getConditionUnless().ifPresent(cond -> valve.setConditionUnless(cond)); stdctx.addValve(valve); } }
/** * Rotate the log file if necessary. */ public void rotate() { if (rotatable) { // Only do a logfile switch check once a second, max. long systime = System.currentTimeMillis(); if ((systime - rotationLastChecked) > 1000) { synchronized(this) { if ((systime - rotationLastChecked) > 1000) { rotationLastChecked = systime; String tsDate; // Check for a change of date tsDate = fileDateFormatter.format(new Date(systime)); // If the date has changed, switch log files if (!dateStamp.equals(tsDate)) { close(true); dateStamp = tsDate; open(); } } } } } }
@Override public void log(Request request, Response response, long time) { if (!getState().isAvailable() || !getEnabled() || logElements == null || condition != null && null != request.getRequest().getAttribute(condition)) { return; } Date date = getDate(); StringBuilder result = new StringBuilder(128); for (int i = 0; i < logElements.length; i++) { logElements[i].addElement(result, date, request, response, time); } log(result.toString()); }
/** * Stop this component and implement the requirements * of {@link org.apache.catalina.util.LifecycleBase#stopInternal()}. * * @exception LifecycleException if this component detects a fatal error * that prevents this component from being used */ @Override protected synchronized void stopInternal() throws LifecycleException { setState(LifecycleState.STOPPING); close(false); }
/** * Execute a periodic task, such as reloading, etc. This method will be * invoked inside the classloading context of this container. Unexpected * throwables will be caught and logged. */ @Override public synchronized void backgroundProcess() { if (getState().isAvailable() && getEnabled() && writer != null && buffered) { writer.flush(); } }
/** * Start this component and implement the requirements * of {@link org.apache.catalina.util.LifecycleBase#startInternal()}. * * @exception LifecycleException if this component detects a fatal error * that prevents this component from being used */ @Override protected synchronized void startInternal() throws LifecycleException { // Initialize the Date formatters String format = getFileDateFormat(); fileDateFormatter = new SimpleDateFormat(format, Locale.US); fileDateFormatter.setTimeZone(TimeZone.getDefault()); dateStamp = fileDateFormatter.format(new Date(System.currentTimeMillis())); if (rotatable && renameOnRotate) { restore(); } open(); setState(LifecycleState.STARTING); }
/** * Start this component and implement the requirements * of {@link org.apache.catalina.util.LifecycleBase#startInternal()}. * * @exception LifecycleException if this component detects a fatal error * that prevents this component from being used */ @Override protected synchronized void startInternal() throws LifecycleException { // Initialize the Date formatters String format = getFileDateFormat(); if (format == null || format.length() == 0) { format = "yyyy-MM-dd"; setFileDateFormat(format); } fileDateFormatter = new SimpleDateFormat(format, Locale.US); fileDateFormatter.setTimeZone(timezone); dateStamp = fileDateFormatter.format(new Date(System.currentTimeMillis())); open(); setState(LifecycleState.STARTING); }
/** * Prepare for the beginning of active use of the public methods of this * component. This method should be called after <code>configure()</code>, * and before any of the public methods of the component are utilized. * * @exception LifecycleException if this component detects a fatal error * that prevents this component from being used */ public void start() throws LifecycleException { // START CR 6411114 if (started) // Ignore multiple starts return; super.start(); // END CR 6411114 // Initialize the timeZone, Date formatters, and currentDate timeZone = calculateTimeZoneOffset(DEFAULT_TIME_ZONE.getRawOffset()); if (fileDateFormat==null || fileDateFormat.length()==0) fileDateFormat = "yyyy-MM-dd"; dateFormatter = new ThreadLocal<SimpleDateFormat>() { @Override protected SimpleDateFormat initialValue() { SimpleDateFormat f = new SimpleDateFormat(fileDateFormat); f.setTimeZone(DEFAULT_TIME_ZONE); return f; } }; currentDate = new Date(); dateStamp = dateFormatter.get().format(currentDate); open(); }
Date date = getDate(); StringBuilder result = new StringBuilder(); HttpServletRequest hreq = (HttpServletRequest) req; if (isResolveHosts()) result.append(req.getRemoteHost()); else result.append(dayFormatter.get().format(date)); // Day result.append('/'); result.append(lookup(monthFormatter.get().format(date))); // Month result.append('/'); result.append(yearFormatter.get().format(date)); // Year result.append(':'); result.append(timeFormatter.get().format(date)); // Time result.append(replace(name.toString(), pattern.charAt(j), request, result.append(replace(ch, date, request, response, time)); result.append(replace(ch, date, request, response,time )); log(result.toString(), date);
getNext().invoke(request, response); Date date = getDate(); StringBuffer result = new StringBuffer(); if (isResolveHosts()) result.append(request.getRemoteHost()); else result.append(dayFormatter.format(date)); // Day result.append('/'); result.append(lookup(monthFormatter.format(date))); // Month result.append('/'); result.append(yearFormatter.format(date)); // Year result.append(':'); result.append(timeFormatter.format(date)); // Time result.append(space); result.append(getTimeZone(date)); // Time Zone result.append("] \""); result.append(replace(name.toString(), pattern.charAt(j), request, result.append(replace(ch, date, request, response, time)); result.append(replace(ch, date, request, response,time )); log(result.toString(), date);
AccessLogValve alv = new AccessLogValve(); alv.setDirectory( new File( extractDirectory, "logs" ).getAbsolutePath() ); alv.setPattern( runtimeProperties.getProperty( Tomcat8Runner.ACCESS_LOG_VALVE_FORMAT_KEY ) ); tomcat.getHost().getPipeline().addValve( alv );
factory.addContextValves(remoteIpValve); AccessLogValve accessLogValve = new AccessLogValve(); accessLogValve.setEnabled(accessLogEnabled); accessLogValve.setBuffered(false); accessLogValve.setCheckExists(true); accessLogValve.setDirectory(dir.getAbsolutePath()); accessLogValve.setRotatable(true); accessLogValve.setRequestAttributesEnabled(true); accessLogValve.setPattern("%h %l %u %t \"%r\" %s %b \"%{Referer}i\" \"%{User-Agent}i\""); accessLogValve.setSuffix(".log"); accessLogValve.setPrefix("macgyver_access"); if (!Strings.isNullOrEmpty(conditionalLoggingAttribute)) { logger.info("configuring conditional access logging using attribute: {}",conditionalLoggingAttribute); accessLogValve.setConditionUnless(conditionalLoggingAttribute);
ServletException { if (started && getEnabled()) { getNext().invoke(request, response); Date date = getDate(); StringBuilder result = new StringBuilder(); log(result.toString()); } else getNext().invoke(request, response);
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()); } }
/** * 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()); }
AccessLogValve accessLogValve = new AccessLogValve(); accessLogValve.setDirectory(logFile.getAbsolutePath()); String accessLogPattern = ContainerConfig.getPropertyValue(engineConfig, "access-log-pattern", null); if (UtilValidate.isNotEmpty(accessLogPattern)) { accessLogValve.setPattern(accessLogPattern); accessLogValve.setPrefix(accessLogPrefix); accessLogValve.setRotatable(ContainerConfig.getPropertyValue(engineConfig, "access-log-rotate", false));
private void configEngine(Engine engine, TomcatConf tomcatConf) { engine.setBackgroundProcessorDelay(tomcatConf.getBackgroundProcessorDelay()); if (tomcatConf.isAccessLogEnabled()) { AccessLogValve value = new AccessLogValve(); value.setBuffered(tomcatConf.isAccessLogBuffered()); value.setFileDateFormat(tomcatConf.getAccessLogFileFormat()); value.setRequestAttributesEnabled(tomcatConf.isAccessLogRequestAttributesEnabled()); value.setPattern(tomcatConf.getAccessLogPattern()); engine.getPipeline().addValve(value); } }
/** * Open the new log file for the date specified by <code>dateStamp</code>. */ @Override protected synchronized void open() { super.open(); if (currentLogFile.length()==0) { writer.println("#Fields: " + pattern); writer.println("#Version: 2.0"); writer.println("#Software: " + ServerInfo.getServerInfo()); } }
@Configuration public class WebConfig extends WebMvcConfigurerAdapter implements EmbeddedServletContainerCustomizer { @Override public void customize(ConfigurableEmbeddedServletContainer container) { if (container instanceof TomcatEmbeddedServletContainerFactory) { TomcatEmbeddedServletContainerFactory factory = (TomcatEmbeddedServletContainerFactory) container; AccessLogValve accessLogValve = new Log4JAccessLogValve(); accessLogValve.setDirectory("/var/log/access_log"); accessLogValve.setPattern("%h %u %t "%r" %s %b - %T"); accessLogValve.setSuffix(".log"); factory.addContextValves(accessLogValve); } else { logger.error("WARNING! this customizer does not support a custom configured container"); } } }
/** * Stop this component and implement the requirements * of {@link org.apache.catalina.util.LifecycleBase#stopInternal()}. * * @exception LifecycleException if this component detects a fatal error * that prevents this component from being used */ @Override protected synchronized void stopInternal() throws LifecycleException { super.stopInternal(); close(false); } }