/** * Levels used for identifying the severity of an event. Levels are organized from most specific to least: * <ul> * <li>{@link #OFF} (most specific, no logging)</li> * <li>{@link #FATAL} (most specific, little data)</li> * <li>{@link #ERROR}</li> * <li>{@link #WARN}</li> * <li>{@link #INFO}</li> * <li>{@link #DEBUG}</li> * <li>{@link #TRACE} (least specific, a lot of data)</li> * <li>{@link #ALL} (least specific, all data)</li> * </ul> * * Typically, configuring a level in a filter or on a logger will cause logging events of that level and those that are * more specific to pass through the filter. A special level, {@link #ALL}, is guaranteed to capture all levels when * used in logging configurations. */ publicfinalclassLevelimplementsComparable<Level>, Serializable {
// No events will be logged. publicstaticfinal Level OFF;
// A severe error that will prevent the application from continuing. publicstaticfinal Level FATAL;
// An error in the application, possibly recoverable. publicstaticfinal Level ERROR;
// An event that might possible lead to an error. publicstaticfinal Level WARN;
// An event for informational purposes. publicstaticfinal Level INFO;
// A general debugging event. publicstaticfinal Level DEBUG;
// A fine-grained debug message, typically capturing the flow through the application. publicstaticfinal Level TRACE;
// All events should be logged. publicstaticfinal Level ALL;
privateLevel(final String name, finalint intLevel) { if (Strings.isEmpty(name)) { thrownewIllegalArgumentException("Illegal null or empty Level name."); } if (intLevel < 0) { thrownewIllegalArgumentException("Illegal Level int less than zero."); } this.name = name; this.intLevel = intLevel; this.standardLevel = StandardLevel.getStandardLevel(intLevel); if (LEVELS.putIfAbsent(name, this) != null) { thrownewIllegalStateException("Level " + name + " has already been defined."); } }
/** * Gets the integral value of this Level. * * @return the value of this Level. */ publicintintLevel() { returnthis.intLevel; }
/** * Gets the standard Level values as an enum. * * @return an enum of the standard Levels. */ public StandardLevel getStandardLevel() { return standardLevel; }
/** * Returns the integer value of the Level. * * @return the integer value of the Level. */ publicintintLevel() { return intLevel; }
/** * Method to convert custom Levels into a StandardLevel for conversion to other systems. * * @param intLevel The integer value of the Level. * @return The StandardLevel. */ publicstatic StandardLevel getStandardLevel(finalint intLevel) { StandardLevellevel= StandardLevel.OFF; for (final StandardLevel lvl : LEVELSET) { if (lvl.intLevel() > intLevel) { break; } level = lvl; } return level; } }
// Formats an event into a string buffer. @Override publicvoidformat(final LogEvent event, final StringBuilder toAppendTo) { finalMessagemsg= event.getMessage(); if (msg instanceof StringBuilderFormattable) {
/** * LOG4J2-2109 if {@code true}, MessagePatternConverter will always operate as though * <pre>%m{nolookups}</pre> is configured. * * @since 2.10 */ publicstaticfinalbooleanFORMAT_MESSAGES_PATTERN_DISABLE_LOOKUPS= PropertiesUtil.getProperties().getBooleanProperty( "log4j2.formatMsgNoLookups", false);
/** * Internal method that resolves the value of a variable. * <p> * Most users of this class do not need to call this method. This method is * called automatically by the substitution process. * </p> * <p> * Writers of subclasses can override this method if they need to alter * how each substitution occurs. The method is passed the variable's name * and must return the corresponding value. This implementation uses the * {@link #getVariableResolver()} with the variable's name as the key. * </p> * * @param event The LogEvent, if there is one. * @param variableName the name of the variable, not null * @param buf the buffer where the substitution is occurring, not null * @param startPos the start position of the variable including the prefix, valid * @param endPos the end position of the variable including the suffix, valid * @return the variable's value or <b>null</b> if the variable is unknown */ protected String resolveVariable(final LogEvent event, final String variableName, final StringBuilder buf, finalint startPos, finalint endPos) { finalStrLookupresolver= getVariableResolver(); if (resolver == null) { returnnull; } return resolver.lookup(event, variableName); }
/** * Resolves the specified variable. This implementation will try to extract * a variable prefix from the given variable name (the first colon (':') is * used as prefix separator). It then passes the name of the variable with * the prefix stripped to the lookup object registered for this prefix. If * no prefix can be found or if the associated lookup object cannot resolve * this variable, the default lookup object will be used. * * @param event The current LogEvent or null. * @param var the name of the variable whose value is to be looked up * @return the value of this variable or <b>null</b> if it cannot be * resolved */ @Override public String lookup(final LogEvent event, String var) { if (var == null) { returnnull; }
finalintprefixPos=var.indexOf(PREFIX_SEPARATOR); if (prefixPos >= 0) { finalStringprefix=var.substring(0, prefixPos).toLowerCase(Locale.US); finalStringname=var.substring(prefixPos + 1); finalStrLookuplookup= strLookupMap.get(prefix); if (lookup instanceof ConfigurationAware) { ((ConfigurationAware) lookup).setConfiguration(configuration); } Stringvalue=null; if (lookup != null) { value = event == null ? lookup.lookup(name) : lookup.lookup(event, name); }
/** * Looks up a named object through this JNDI context. * * @param name name of the object to look up. * @param <T> the type of the object. * @return the named object if it could be located. * @throws NamingException if a naming exception is encountered */ @SuppressWarnings("unchecked") public <T> T lookup(final String name)throws NamingException { return (T) this.context.lookup(name); }