private void log(Level level, Object message, Throwable t, Object... params) { if (!logger.isLoggable(level)) { return; } String msg = (message == null) ? "NULL" : message.toString(); LogRecord record = new LogRecord(level, msg); record.setLoggerName(logger.getName()); if (t != null) { record.setThrown(t); } else if (params != null && params.length != 0 && params[params.length - 1] instanceof Throwable) { // The exception may be the last parameters (SLF4J uses this convention). record.setThrown((Throwable) params[params.length - 1]); } // This will disable stack trace lookup inside JUL. If someone wants location info, they can use their own formatter // or use a different logging framework like sl4j, or log4j record.setSourceClassName(null); record.setParameters(params); logger.log(record); }
/** * Closes the item and logs error to the log in the case of error. * Logging will be performed on the {@code WARNING} level. * @param toClose Item to close. Nothing will happen if it is {@code null} * @param logger Logger, which receives the error * @param closeableName Name of the closeable item * @param closeableOwner String representation of the closeable holder * @since 2.19, but TODO update once un-restricted */ @Restricted(NoExternalUse.class) public static void closeAndLogFailures(@CheckForNull Closeable toClose, @Nonnull Logger logger, @Nonnull String closeableName, @Nonnull String closeableOwner) { if (toClose == null) { return; } try { toClose.close(); } catch(IOException ex) { LogRecord record = new LogRecord(Level.WARNING, "Failed to close {0} of {1}"); record.setParameters(new Object[] { closeableName, closeableOwner }); record.setThrown(ex); logger.log(record); } }
private void log(java.util.logging.Level level, Object message, Throwable exception) { if (this.logger.isLoggable(level)) { LogRecord rec; if (message instanceof LogRecord) { rec = (LogRecord) message; } else { rec = new LocationResolvingLogRecord(level, String.valueOf(message)); rec.setLoggerName(this.name); rec.setResourceBundleName(logger.getResourceBundleName()); rec.setResourceBundle(logger.getResourceBundle()); rec.setThrown(exception); } logger.log(rec); } }
/** * Logs a given exception using the given <code>logger</code>. * * @param logger logger of the class where the exception occurred * @param level level to be used * @param msg message to be logged * @param t exception thrown */ public static void logException(Logger logger, Level level, String msg, Throwable t) { StackTraceElement callerStackTraceElement = getCallerStackTraceElement(); if (callerStackTraceElement != null) { // using this stackTraceElements allows to log the message using the caller's class / method name logger.logp(level, callerStackTraceElement.getClassName(), callerStackTraceElement.getMethodName(), msg, t); } else { // we haven't found the caller, so just log what we have LogRecord logRecord = new LogRecord(level, msg); logRecord.setThrown(t); logger.log(logRecord); } }
public static LogRecord logRecordFromJson(String jsonString) throws InvalidJsonLogRecordFormatException { try { JSONObject lro = new JSONObject(jsonString); String level = lro.getString("level"); String loggerName = lro.getString("loggerName"); String msg = lro.getString("msg"); long timestamp = Long.parseLong(lro.getString("timestamp")); Throwable thrown = JsonLogRecordThrowable.fromJsonString(lro.getString("thrown")); LogRecord lr = new LogRecord(Level.parse(level), msg); lr.setLoggerName(loggerName); lr.setThrown(thrown); lr.setMillis(timestamp); return lr; } catch (JSONException e) { throw new InvalidJsonLogRecordFormatException(e); } }
public static void deserialize(SerializationStreamReader reader, LogRecord instance) throws SerializationException { String loggerName = reader.readString(); Long millis = reader.readLong(); SerializableThrowable thrown = (SerializableThrowable) reader.readObject(); instance.setLoggerName(loggerName); instance.setMillis(millis); instance.setThrown(thrown); }
/** * Log the message at the specified level with the specified throwable if any. * This method creates a LogRecord and fills in caller date before calling * this instance's JDK14 logger. * * See bug report #13 for more details. */ private void log(String callerFQCN, Level level, String msg, Throwable t) { // millis and thread are filled by the constructor LogRecord record = new LogRecord(level, msg); record.setLoggerName(name()); record.setThrown(t); fillCallerData(callerFQCN, record); logger.log(record); }
/** Like {@link Logger#log(Level, String, Object)}, except with a throwable arg */ public void log(String msg, Object param1, @Nullable Throwable thrown) { if (!LOG.isLoggable(Level.FINE)) return; // fine level to not fill logs LogRecord lr = new LogRecord(Level.FINE, msg); Object params[] = {param1}; lr.setParameters(params); if (thrown != null) lr.setThrown(thrown); LOG.log(lr); }
public void log(Level level, String msg, Throwable thrown) { if (isLoggable(level)) { LogRecord record = new LogRecord(level, msg); record.setThrown(thrown); record.setLoggerName(getName()); log(record); } }
/** * Log the message at the specified level with the specified throwable if any. * This method creates a LogRecord and fills in caller date before calling * this instance's JDK14 logger. * * See bug report #13 for more details. */ private void log(String callerFQCN, Level level, String msg, Throwable t) { // millis and thread are filled by the constructor LogRecord record = new LogRecord(level, msg); record.setLoggerName(name()); record.setThrown(t); fillCallerData(callerFQCN, record); logger.log(record); }
try { if(thread!=null && thread.isAlive()) { logger.log(this.getSlowLoggingLevel(), "{0} thread is still running. Execution aborted.", name); return; thread.start(); } catch (Throwable t) { LogRecord lr = new LogRecord(this.getErrorLoggingLevel(), "{0} thread failed with error"); lr.setThrown(t); lr.setParameters(new Object[]{name}); logger.log(lr);
public void log(Level level, String msg, Throwable thrown) { if (isLoggable(level)) { LogRecord record = new LogRecord(level, msg); record.setThrown(thrown); record.setLoggerName(getName()); log(record); } }
private static void log(Level level, String msg, Throwable thrown, Object... params) { if (!LOGGER.isLoggable(level)) { return; } LogRecord rec = new LogRecord(level, msg); // Set the loggerName of the LogRecord with the current logger rec.setLoggerName(LOGGER.getName()); rec.setParameters(params); rec.setThrown(thrown); LOGGER.log(rec); }
InetAddress ia = InetAddress.getByName(address); if(!(ia instanceof Inet4Address)) { LOGGER.log(Level.FINE, "{0} is not an IPv4 address", address); continue; LOGGER.log(Level.FINE, "{0} didn't respond to ping", address); continue; } catch (IOException e) { LogRecord lr = new LogRecord(Level.FINE, "Failed to parse {0}"); lr.setThrown(e); lr.setParameters(new Object[]{address}); LOGGER.log(lr);
/** * To correctly attribute the source class/method name to that of the JGroups class, * we can't let JDK compute that. Instead, we do it on our own. */ private void log(Level lv, String msg, Throwable e) { if (logger.isLoggable(lv)) { LogRecord r = new LogRecord(lv, msg); r.setThrown(e); // find the nearest ancestor that doesn't belong to JDKLogImpl for (StackTraceElement frame : new Exception().getStackTrace()) { if (!frame.getClassName().equals(THIS_CLASS_NAME)) { r.setSourceClassName(frame.getClassName()); r.setSourceMethodName(frame.getMethodName()); break; } } logger.log(r); } }
public void log(final Level level, final int depth, final String msg, final Throwable thrown) { requireNonNull(level, "No log level provided"); if (!logger.isLoggable(level)) { return; } final LogRecord lr = new LogRecord(level, msg); updateSource(lr, depth); if (thrown != null) { lr.setThrown(thrown); } logger.log(lr); }
private void log( Level level, String msg, Throwable ex ) { if( getLogger().isLoggable(level) ) { LogRecord record = new LogRecord(level, msg); if( !classAndMethodFound ) { getClassAndMethod(); } record.setSourceClassName(sourceClassName); record.setSourceMethodName(sourceMethodName); if( ex != null ) { record.setThrown(ex); } getLogger().log(record); } }
public void log(final Level level, final int depth, final String msg, final Throwable thrown) { requireNonNull(level, "No log level provided"); if (!logger.isLoggable(level)) { return; } final LogRecord lr = new LogRecord(level, msg); updateSource(lr, depth); if (thrown != null) { lr.setThrown(thrown); } logger.log(lr); }