/** * Returns a new logger instance wrapping the specified logger. * * @param log Logger to wrap. * @return A new logger instance or, if the original logger was already an instance of Logger, * that instance is returned. */ public static Logger create(Log log) { return log instanceof Logger ? (Logger) log : new Logger(log); }
/** * Creates a logger for the given name. * * @param name Name to associate with logger. * @return A new logger instance. */ public static Logger create(String name) { return create(LogFactory.getLog(name)); }
/** * Logs at trace level the request headers */ public static void logHeaderNames() { HttpServletRequest request = getRequest(); if (request == null) { log.debug("logHeaderNames() invoked outside the scope of a servlet request"); } else if (log.isTraceEnabled()) { Enumeration<?> enumeration = request.getHeaderNames(); while (enumeration.hasMoreElements()) { String headerName = (String) enumeration.nextElement(); log.trace(String.format("HeaderName: %s", headerName)); } } }
@Override public void addViewControllers(ViewControllerRegistry registry) { try { Resource[] resources = applicationContext.getResources("/index.*"); if (resources.length > 0) { if (resources.length > 1) { log.warn("Multiple home page candidates detected - only one will be selected."); } String file = "/" + resources[0].getFilename(); registry.addRedirectViewController("/", file); log.info(() -> "Default home page set to: " + file); } else { log.info("No default home page detected."); } } catch (Exception e) { log.error("Error while attempting to detect default home page.", e); } }
/** * Calls all registered destruction callbacks and removes all bean references from the * container. */ public void destroy() { for (Entry<String, Runnable> entry : destructionCallbacks.entrySet()) { try { entry.getValue().run(); } catch (Throwable t) { log.error(() -> "Error during destruction callback for bean " + entry.getKey(), t); } } beans.clear(); destructionCallbacks.clear(); }
log.debug(() -> "Parsing configuration data for web jar: " + resource); WebJar webjar = new WebJar(resource); String name = webjar.getName(); log.warn("Duplicate webjar was ignored: " + webjar); continue; JSONUtil.merge(config, webjar.getConfig(), true); } else { log.warn(() -> "No configuration information found for web jar: " + webjar.getName()); log.error(() -> "Error extracting configuration information from web jar: " + resource, e);
/** * Displays a warning if a mapping is being overwritten. * * @param pattern Pattern to check. */ private void dupCheck(String pattern) { if (urlMap.containsKey(pattern)) { log.warn(() -> String.format("Overwriting URL pattern \"%s\" in theme \"%s\"", pattern, name)); } }
/** * Process FSPs marked for pre-compilation. */ @Override public void onApplicationEvent(ContextRefreshedEvent event) { if (precompiled != null) { Set<String> fsps = precompiled; precompiled = null; for (String fsp : fsps) { String url = fsp.trim(); if (url.isEmpty()) { continue; } if (!"fsp".equals(FilenameUtils.getExtension(url))) { url += ".fsp"; } try { log.info(() -> "Precompiling " + fsp); get(url); } catch (Exception e) { log.warn(() -> "Error precompiling " + fsp, e); } } } }
/** * Locates, parses, and registers all type libraries (files with the <code>tld</code> extension) * discovered on the class path. */ @Override public void setApplicationContext(ApplicationContext applicationContext) throws BeansException { try { Resource[] resources = applicationContext.getResources("classpath*:**/*.tld"); for (Resource resource : resources) { log.info(() -> "Found tag library at " + resource); try { register(TagLibraryParser.getInstance().parse(resource)); } catch (Exception e) { log.error("Error parsing tag library", e); } } } catch (Exception e) { throw MiscUtil.toUnchecked(e); } }
@Override public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException { if (bean instanceof IIconLibrary) { IIconLibrary lib = (IIconLibrary) bean; register(lib); log.info(() -> "Registered icon library: " + lib.getId()); } return bean; }
/** * Return client's ip address. Returns null if invoked outside scope of a servlet request. * <p> * This considers header X-FORWARDED-FOR (i.e. useful if behind a proxy) * * @return the client's IP */ public static String getRemoteAddress() { HttpServletRequest request = getRequest(); String ipAddress = null; if (request != null) { ipAddress = request.getHeader("x-forwarded-for"); boolean ipFromHeader = true; if (isEmpty(ipAddress)) { ipAddress = request.getHeader("X_FORWARDED_FOR"); if (isEmpty(ipAddress)) { ipFromHeader = false; ipAddress = request.getRemoteAddr(); } logHeaderNames(); } //log headers in case we find a case where above logic doesn't return correct ip if (log.isTraceEnabled()) { logHeaderNames(); log.trace( String.format("Remote address: %s , obtained from X-FORWARDED_FOR header?", ipAddress, ipFromHeader)); } } return ipAddress; }
/** * Return local host IP. Note: HttpServletRequest#getLocalAddr() doesn't seem to be consistent. * This method uses java.net.InetAddress. * * @see InetAddress#getHostAddress() * @return server IP */ public static String getLocalHostAddress() { try { return InetAddress.getLocalHost().getHostAddress(); } catch (Exception e) { log.debug("Exception occurred obtaining localhost IP address", e); return null; } }
/** * Creates and registers a new session. * * @param servletContext The servlet context. * @param socket The web socket session. * @return The newly created session. */ protected Session createSession(ServletContext servletContext, WebSocketSession socket) { Session session = new Session(servletContext, socket); sessions.put(session.getId(), session); if (log.isDebugEnabled()) { logSessionEvent(session, "established"); } return session; }
/** * Perform an action. * * @param e Exception resulting from the failure. */ void doAction(Exception e) { switch (this) { case IGNORE: return; case EXCEPTION: throw MiscUtil.toUnchecked(e); case LOG: log.error(() -> e.getMessage(), e); return; } } }
/** * Evict entries older than the specified eviction time. */ private void evict() { long threshold = System.currentTimeMillis() - evictionInterval; synchronized (timestamps) { Iterator<Entry<Integer, Long>> iter = timestamps.entrySet().iterator(); while (iter.hasNext()) { Entry<Integer, Long> entry = iter.next(); if (entry.getValue() < threshold) { iter.remove(); callbacks.remove(entry.getKey()); log.warn(() -> "Evicted expired callback #" + entry.getKey()); } else { break; } } } } }
/** * Registers a processing instruction parser. * * @param piParser A processing instruction parser. */ private void registerPIParser(PIParserBase piParser) { piParsers.put(piParser.getTarget(), piParser); log.info(() -> "Registered processing instruction parser for target '" + piParser.getTarget() + "'."); }
/** * Logs a session event. * * @param session The session. * @param event The text describing the event. */ private void logSessionEvent(Session session, String event) { log.debug(() -> "Session #" + session.getId() + " " + event + "."); }