/** * Creates a logger with the given name, level, and log message format, that will write all its messages * to the given OutputStream. * @param os The output stream. * @param loggerName The name of the logger. * @param level The level below which messages will be ignored. * @param format The log format. See {@link LogFormatter}. * @see LogFormatter */ public OSLogger(OutputStream os, String loggerName, @Nullable LogLevel level, @Nullable String format) { this(os, os, loggerName, level, format); }
protected LogEvent(LogLevel level, String message, @Nullable Throwable throwable) { this.level = level; this.throwable = throwable; this.message = message; }
/** * Returns the level that corresponds to the given string. * The string is taken as case insensitive, and may be the whole name of the level, * or the first letter. * @param level The level to parse. * @return The level equivalent to the given string, or null, if none is found. */ @Nullable public static LogLevel getLevel(String level) { return levels.containsKey(level.toLowerCase()) ? levels.get(level.toLowerCase()) : null; }
/** * Creates a logger with the given name, level, and log message format, that will write messages with level * {@link LogLevel#INFO INFO} and below to the first output stream, and messages with levels * {@link LogLevel#WARN WARN} and {@link LogLevel#ERROR ERROR} to the second one. * @param outputStream The output stream for messages with level {@link LogLevel#INFO INFO} and below. * @param errorStream The output stream for messages with level above {@link LogLevel#INFO INFO}. * @param loggerName The name of the logger. * @param level The level below which messages will be ignored. * @param format The log format. See {@link LogFormatter}. * @see LogFormatter */ public OSLogger(OutputStream outputStream, OutputStream errorStream, String loggerName, @Nullable LogLevel level, @Nullable String format) { this.level = level != null ? level : LogLevel.ALL; this.loggerName = loggerName; this.format = format == null ? DEFAULT_FORMAT : format; this.outputStream = new PrintStream(outputStream, true); this.errorStream = new PrintStream(errorStream, true); OS_LOGGERS.add(this); }
/** * @return the throwable to log. May be null. */ @API @Nullable public Throwable getThrowable() { return throwable; } }
protected LogEvent(LogLevel level, String format, @Nullable Throwable throwable, Object... args) { this(level, SF.f(format, args), throwable); }
/** * Formats the given log message, with {@code null} as the throwable. * * @param level The level of the message. * @param format The format string. * @param message The log message. * @param loggerName The name of the logger. * @return The formatted log message. * @see LogFormatter */ public static String formatLogMessage(@NotNull LogLevel level, @NotNull String format, @Nullable String message, @Nullable String loggerName) { return LogFormatter.formatLogMessage(level, format, message, null, loggerName); }
/** * Formats the given string by replacing <i>{}</i> by the given arguments. * Order is preserved: the first occurrence of <i>{}</i> will be replaced by {@code args[0]}, and so on. * If there are more <i>{}</i> tokens than arguments given, the token will be replaced by <i>{NULL}</i>. * @param format The format string. * @param args The arguments to replace in the string. * @return The formatted string. * */ @NotNull public static String f(@Nullable String format, @Nullable Object... args) { if (format == null || format.length() == 0 || "".equals(format)) return ""; char[] chars = format.toCharArray(); StringBuilder b = new StringBuilder(format.length()); int token = 0; for (int i = 0; i < chars.length; i++) { if (i < chars.length - 1 && chars[i] == TOKEN_START && chars[i + 1] == TOKEN_END) { if (args == null || token >= args.length) b.append(NULL_STRING); else b.append(args[token++]); i++; } else { b.append(chars[i]); } } return b.toString(); } }
/** * If the provided logger is not null, it is returned, otherwise a new {@link NullLogger} is returned * * @param logger A logger, or null * @return Either the provided logger if it is not null, or a new {@link NullLogger} */ @NotNull public static ILogger getLogger(@Nullable ILogger logger) { return logger == null ? new NullLogger() : logger; }