private static void warnOnWrongVisibility(Method method) { if (!Modifier.isProtected(method.getModifiers())) { Mixing.LOG.WARN("Handler %s.%s is not declared protected!", method.getDeclaringClass().getName(), method.getName()); } }
/** * Can be used to mark an exception as ignored. * <p> * Instead of leading a try / catch block empty, the method can be invoked. Therefore it is known, that the * exception is wanted to be ignored. Additionally, the <tt>ignoredExceptions</tt> logger can be turned on, * to still see those exceptions. * * @param t the exception to be ignored. This exception will be discarded unless the <tt>ignoredExceptions</tt> * logger is set to INFO. */ public static void ignore(Throwable t) { IGNORED_EXCEPTIONS_LOG.INFO(t); }
/** * Used to log the given message <tt>msg</tt> at <b>INFO</b> level if debug mode is enabled * ({@link sirius.kernel.Sirius#isDev()}). Otherwise the message will be logged as <b>FINE</b>. * * @param msg the message to log */ public void DEBUG_INFO(Object msg) { if (Sirius.isDev()) { INFO(msg); } else { FINE(msg); } }
private static void stopLifecycleParticipants() { LOG.INFO("Stopping lifecycles..."); LOG.INFO(SEPARATOR_LINE); for (int i = lifecycleStopParticipants.size() - 1; i >= 0; i--) { Stoppable stoppable = lifecycleStopParticipants.get(i); Future future = tasks.defaultExecutor().fork(() -> stopLifecycle(stoppable)); if (!future.await(Duration.ofSeconds(10))) { LOG.WARN("Lifecycle '%s' did not stop within 10 seconds....", stoppable.getClass().getName()); } } LOG.INFO(SEPARATOR_LINE); }
private void awaitContainerStart(Container container) { LOG.INFO("Waiting for '%s' to become ready...", container.getContainerName()); try { int retries = MAX_WAIT_SECONDS; while (container.areAllPortsOpen().failed()) { Wait.seconds(1); if (retries-- <= 0) { LOG.WARN("Failed to start '%s' - Ports: %s", container.getContainerName(), container.ports().stream().map(Object::toString).collect(Collectors.joining(", "))); return; } } LOG.INFO("Container '%s' is ONLINE - Ports: %s", container.getContainerName(), container.ports().stream().map(Object::toString).collect(Collectors.joining(", "))); } catch (Exception e) { LOG.SEVERE(e); } }
LOG.FINE("Did not process the exception %s because its root (%s) was already handled", ex.getMessage(), Exceptions.getRootCause(ex).getMessage()); HandledException result = new HandledException(message, ex); if (processError) { log.SEVERE(result); notifyHandlers(result); } else { IGNORED_EXCEPTIONS_LOG.INFO(result);
private static void setupFrameworks() { Config frameworkConfig = config.getConfig("sirius.frameworks"); Map<String, Boolean> frameworkStatus = Maps.newHashMap(); int total = 0; int numEnabled = 0; LOG.DEBUG_INFO("Scanning framework status (sirius.frameworks):"); for (Map.Entry<String, com.typesafe.config.ConfigValue> entry : frameworkConfig.entrySet()) { String framework = entry.getKey(); try { boolean enabled = Value.of(entry.getValue().unwrapped()).asBoolean(false); frameworkStatus.put(framework, enabled); total++; numEnabled += enabled ? 1 : 0; LOG.DEBUG_INFO(Strings.apply(" * %s: %b", framework, enabled)); } catch (Exception e) { Exceptions.ignore(e); LOG.WARN("Cannot convert status '%s' of framework '%s' to a boolean! Framework will be disabled.", entry.getValue().render(), framework); frameworkStatus.put(framework, false); } } LOG.INFO("Enabled %d of %d frameworks...", numEnabled, total); // Although customizations are loaded in setupConfiguration, we output the status here, // as this seems more intiutive for the customer (the poor guy reading the logs...) LOG.INFO("Active Customizations: %s", customizations); frameworks = frameworkStatus; }
private Property createProperty(Class<?> rootClass, List<Property> props, Field field) { for (PropertyFactory f : Schema.factories.getParts()) { if (f.accepts(field)) { Property p = f.create(field); if (propertyAlreadyExists(props, p)) { IndexAccess.LOG.SEVERE(Strings.apply( "A property named '%s' already exists for the type '%s'. Cannot transform field %s", p.getName(), rootClass.getSimpleName(), field)); return null; } props.add(p); if (!p.acceptsSetter() && hasSetter(field)) { IndexAccess.LOG.WARN("Property %s in type %s does not accept a setter method to be present", field.getName(), rootClass.getSimpleName()); } return p; } } return null; }
/** * Returns all detected component roots * * @return a list of URLs pointing to the component roots */ public List<URL> getComponentRoots() { if (componentRoots == null) { try { componentRoots = Collections.list(loader.getResources(componentName)); componentRoots.sort(Comparator.comparing(URL::toString)); } catch (IOException e) { LOG.SEVERE(e); } } return componentRoots; }
private void blockUnitExecutorTerminates(String name, AsyncExecutor exec) { LOG.INFO("Waiting for async executor '%s' to terminate...", name); try { if (!exec.awaitTermination(EXECUTOR_SHUTDOWN_WAIT.getSeconds(), TimeUnit.SECONDS)) { LOG.SEVERE(Strings.apply("Executor '%s' did not terminate within 60s. Interrupting " + "tasks...", name)); exec.shutdownNow(); if (!exec.awaitTermination(EXECUTOR_TERMINATION_WAIT.getSeconds(), TimeUnit.SECONDS)) { LOG.SEVERE(Strings.apply("Executor '%s' did not terminate after another 30s!", name)); } } } catch (InterruptedException ex) { Exceptions.ignore(ex); Thread.currentThread().interrupt(); LOG.SEVERE(Strings.apply("Interrupted while waiting for '%s' to terminate!", name)); } }
/** * Sets UTF-8 as default encoding */ protected void setupEncoding() { Sirius.LOG.FINE("Setting " + Charsets.UTF_8.name() + " as default encoding (file.encoding)"); System.setProperty("file.encoding", Charsets.UTF_8.name()); Sirius.LOG.FINE("Setting " + Charsets.UTF_8.name() + " as default mime encoding (mail.mime.charset)"); System.setProperty("mail.mime.charset", Charsets.UTF_8.name()); }
/** * Generates a new logger with the given name * <p> * The given name should be short and simple. It is not recommended to create a logger per class but one for * each framework or subsystem. * * @param name the name of the logger. This should be a simple name, completely lowercase, without any dots * @return a new logger logging with the given name. */ @SuppressWarnings("squid:S2250") @Explain("Loggers are only created once, so there is no performance hotspot") public static synchronized Log get(String name) { Log result = new Log(Logger.getLogger(name)); all.add(result); if (!name.matches("[a-z0-9\\-]+")) { result.WARN("Invalid logger name: %s. Only numbers, lowercase letters and - are allowed!%n", name); } return result; }
private void notifyHandlers(HandledException result) { // Injector might not have run yet if (handlers == null || Boolean.TRUE.equals(frozen.get())) { return; } try { frozen.set(Boolean.TRUE); String location = computeLocation(result); for (ExceptionHandler handler : handlers) { try { handler.handle(new Incident(log.getName(), location, CallContext.getCurrent().getMDC(), result)); } catch (Exception e) { // Just log the exception - anything else might call a rather long infinite loop LOG.SEVERE(new Exception(Strings.apply( "An error occurred while calling the ExceptionHandler: %s - %s (%s)", handler, e.getMessage(), e.getClass().getName()), e)); } } } finally { frozen.set(Boolean.FALSE); } }
private void waitForElasticsearchToBecomReady() { int retries = 15; while (retries-- > 0) { try { if (client.getRestClient().performRequest("GET", "/_cat/indices").getStatusLine().getStatusCode() == 200) { return; } } catch (Exception e) { Exceptions.ignore(e); } Elastic.LOG.INFO("Sleeping two seconds to wait until Elasticsearch is ready..."); Wait.seconds(2); } Elastic.LOG.WARN("Elasticsearch was not ready after waiting 30s..."); }
private Stream<String> scan(URL url) { List<String> result = new ArrayList<>(); if ("file".equals(url.getProtocol())) { try { File file = new File(url.toURI().getPath()); if (!file.isDirectory()) { file = file.getParentFile(); } addFiles(file, result, file); } catch (URISyntaxException e) { LOG.SEVERE(e); } } else if ("jar".equals(url.getProtocol())) { try { JarFile jar = ((JarURLConnection) url.openConnection()).getJarFile(); Enumeration<JarEntry> e = jar.entries(); while (e.hasMoreElements()) { JarEntry entry = e.nextElement(); result.add(entry.getName()); } } catch (IOException e) { LOG.SEVERE(e); } } return result.stream(); }
/** * Sets the DNS cache to a sane value. * <p> * By default java infinitely caches all DNS entries. Will be changed to 10 seconds... */ protected void setupDNSCache() { Sirius.LOG.FINE("Setting DNS-Cache to 10 seconds..."); java.security.Security.setProperty("networkaddress.cache.ttl", "10"); }
@Override public ResultSet executeQuery(String sql) throws SQLException { if (Databases.LOG.isFINE()) { Databases.LOG.FINE(sql); } Watch w = Watch.start(); try (Operation op = new Operation(() -> sql, Duration.ofSeconds(30))) { return stmt.executeQuery(sql); } finally { updateStatistics(sql, w); } }
private static void outputThreadState() { LOG.INFO("System halted! - Thread State"); LOG.INFO(SEPARATOR_LINE); LOG.INFO("%-15s %10s %53s", "STATE", "ID", "NAME"); for (ThreadInfo info : ManagementFactory.getThreadMXBean().dumpAllThreads(false, false)) { LOG.INFO("%-15s %10s %53s", info.getThreadState().name(), info.getThreadId(), info.getThreadName()); } LOG.INFO(SEPARATOR_LINE); }