/** * Formats a {@code LogRecord} object into a localized string * representation. This is a convenience method for subclasses of {@code * Formatter}. * <p> * The message string is firstly localized using the {@code ResourceBundle} * object associated with the supplied {@code LogRecord}. * <p> * Notice : if message contains "{0", then java.text.MessageFormat is used. * Otherwise no formatting is performed. * * @param r * the log record to be formatted. * @return the string resulted from the formatting. */ public String formatMessage(LogRecord r) { String pattern = r.getMessage(); ResourceBundle rb = null; // try to localize the message string first if (null != (rb = r.getResourceBundle())) { try { pattern = rb.getString(pattern); } catch (Exception e) { pattern = r.getMessage(); } } if (null != pattern) { Object[] paramsJ = r.getParameters(); /* * if the message contains "{0", use java.text.MessageFormat to * format the string */ if (pattern.indexOf("{0") >= 0 && null != paramsJ //$NON-NLS-1$ && paramsJ.Length > 0) { try { pattern = java.text.MessageFormat.format(pattern, paramsJ); } catch (java.lang.IllegalArgumentException e) { pattern = r.getMessage(); } } } return pattern; }
/** * Converts a {@link LogRecord} object into a human readable string * representation. * * @param r * the log record to be formatted into a string. * @return the formatted string. */ public override String format(LogRecord r) { java.lang.StringBuilder sb = new java.lang.StringBuilder(); sb.append(java.text.MessageFormat.format("{0, date} {0, time} ", //$NON-NLS-1$ new Object[] { new java.util.Date(r.getMillis()) })); sb.append(r.getSourceClassName()).append(" "); //$NON-NLS-1$ sb.append(r.getSourceMethodName()).append(java.lang.SystemJ.getProperty("line.separator")); sb.append(r.getLevel().getName()).append(": "); //$NON-NLS-1$ sb.append(formatMessage(r)).append(java.lang.SystemJ.getProperty("line.separator")); if (null != r.getThrown()) { sb.append("Throwable occurred: "); //$NON-NLS-1$ java.lang.Throwable t = r.getThrown(); java.io.PrintWriter pw = null; try { java.io.StringWriter sw = new java.io.StringWriter(); pw = new java.io.PrintWriter(sw); t.printStackTrace(pw); sb.append(sw.toString()); } finally { if (pw != null) { try { pw.close(); } catch (Exception e) { // ignore } } } } return sb.toString(); }
/* * Check if given {@code LogRecord} would be put into this * {@code MemoryHandler}'s internal buffer. * <p> * The given {@code LogRecord} is loggable if and only if it has appropriate * level and it pass any associated filter's check. * <p> * Note that the push level is not used for this check. * * @param record * the given {@code LogRecord} * @return the given {@code LogRecord} if it should be logged, {@code false} * if {@code LogRecord} is {@code null}. */ public override bool isLoggable(LogRecord record) { return(base.isLoggable(record)); }
/** * Logs a record if necessary. A flush operation will be done. * * @param record * the log record to be logged. */ public override void publish(LogRecord record) { base.publish(record); base.flush(); }
/** * Converts a {@link LogRecord} object into a string representation. The * resulted string is usually localized and includes the message field of * the record. * * @param r * the log record to be formatted into a string. * @return the formatted string. */ public abstract String format(LogRecord r);
/* * Converts a {@link LogRecord} object into a string representation. The * resulted string is usually localized and includes the message field of * the record. * * @param r * the log record to be formatted into a string. * @return the formatted string. */ public abstract String format(LogRecord r);
/** * Put a given {@code LogRecord} into internal buffer. If given record is * not loggable, just return. Otherwise it is stored in the buffer. * Furthermore if the record's level is not less than the push level, the * push action is triggered to output all the buffered records to the target * handler, and the target handler will publish them. * * @param record * the log record */ public override void publish(LogRecord record) { lock(this) { if (!isLoggable(record)) { return; } if (cursor >= size) { cursor = 0; } buffer[cursor++] = record; if (record.getLevel().intValue() >= pushJ.intValue()) { push(); } return;} }
/** * Determines whether the supplied log record needs to be logged. The * logging levels will be checked as well as the filter. * * @param record * the log record to be checked. * @return {@code true} if the supplied log record needs to be logged, * otherwise {@code false}. */ public virtual bool isLoggable(LogRecord record) { if (null == record) { throw new java.lang.NullPointerException(); } if (this.level.intValue() == Level.OFF.intValue()) { return false; } else if (record.getLevel().intValue() >= this.level.intValue()) { return null == this.filter || this.filter.isLoggable(record); } return false; }
/** * Accepts a logging request. The log record is formatted and written to the * output stream if the following three conditions are met: * <ul> * <li>the supplied log record has at least the required logging level; * <li>the supplied log record passes the filter associated with this * handler, if any; * <li>the output stream associated with this handler is not {@code null}. * </ul> * If it is the first time a log record is written out, the head string of * the formatter associated with this handler is written out first. * * @param record * the log record to be logged. */ public override void publish(LogRecord record) { lock(this){ try { if (this.isLoggable(record)) { if (this.writerNotInitialized) { initializeWritter(); } String msg = null; try { msg = getFormatter().format(record); } catch (java.lang.Exception e) { // logging.17=Exception occurred while formatting the log // record. getErrorManager().error("Exception occurred while formatting the log record.", //$NON-NLS-1$ e, ErrorManager.FORMAT_FAILURE); } write(msg); } } catch (java.lang.Exception e) { // logging.18=Exception occurred while logging the record. getErrorManager().error("Exception occurred while logging the record.", e, //$NON-NLS-1$ ErrorManager.GENERIC_FAILURE); } } }
/** * Sets the resource bundle and its name for a supplied LogRecord object. * This method first tries to use this logger's resource bundle if any, * otherwise try to inherit from this logger's parent, recursively up the * namespace. */ private void setResourceBundle(LogRecord record) { for (Logger p = this; p != null; p = p.parent) { String resourceBundleName = p.resourceBundleName; if (resourceBundleName != null) { record.setResourceBundle(p.resourceBundle); record.setResourceBundleName(resourceBundleName); return; } } }
/** * Determines whether the supplied log record needs to be logged. The * logging levels are checked as well as the filter. The output stream of * this handler is also checked. If it is {@code null}, this method returns * {@code false}. * <p> * Notice : Case of no output stream will return {@code false}. * * @param record * the log record to be checked. * @return {@code true} if {@code record} needs to be logged, {@code false} * otherwise. */ public override bool isLoggable(LogRecord record) { if (null == record) { return false; } if (null != this.os && base.isLoggable(record)) { return true; } return false; }
/** * Logs a given log record. Only records with a logging level that is equal * or greater than this logger's level will be submitted to this logger's * handlers for logging. If {@code getUseParentHandlers()} returns {@code * true}, the log record will also be submitted to the handlers of this * logger's parent, potentially recursively up the namespace. * <p> * Since all other log methods call this method to actually perform the * logging action, subclasses of this class can override this method to * catch all logging activities. * </p> * * @param record * the log record to be logged. */ public void log(LogRecord record) { if (!internalIsLoggable(record.getLevel())) { return; } // apply the filter if any Filter f = filter; if (f != null && !f.isLoggable(record)) { return; } /* * call the handlers of this logger, throw any exception that occurs */ Handler[] allHandlers = getHandlers(); foreach (Handler element in allHandlers) { element.publish(record); } // call the parent's handlers if set useParentHandlers Logger temp = this; Logger theParent = temp.parent; while (theParent != null && temp.getUseParentHandlers()) { Handler[] ha = theParent.getHandlers(); foreach (Handler element in ha) { element.publish(record); } temp = theParent; theParent = temp.parent; } }
/** * Logs a message of the specified level. The message is transmitted to all * subscribed handlers. * * @param logLevel * the level of the specified message. * @param msg * the message to log. */ public void log(Level logLevel, String msg) { if (!internalIsLoggable(logLevel)) { return; } LogRecord record = new LogRecord(logLevel, msg); record.setLoggerName(this.name); setResourceBundle(record); log(record); }
/** * Accepts a logging request and sends it to the the target. * * @param record * the log record to be logged; {@code null} records are ignored. */ public abstract void publish(LogRecord record);
/** * Check if given {@code LogRecord} would be put into this * {@code MemoryHandler}'s internal buffer. * <p> * The given {@code LogRecord} is loggable if and only if it has appropriate * level and it pass any associated filter's check. * <p> * Note that the push level is not used for this check. * * @param record * the given {@code LogRecord} * @return the given {@code LogRecord} if it should be logged, {@code false} * if {@code LogRecord} is {@code null}. */ public override bool isLoggable(LogRecord record) { return base.isLoggable(record); }
/** * Publish a {@code LogRecord}. * * @param record * the log record to publish. */ public override void publish(LogRecord record) { lock (this) { base.publish(record); flush(); findNextGeneration(); } }