001/* 002 * Licensed to the Apache Software Foundation (ASF) under one or more 003 * contributor license agreements. See the NOTICE file distributed with 004 * this work for additional information regarding copyright ownership. 005 * The ASF licenses this file to You under the Apache License, Version 2.0 006 * (the "License"); you may not use this file except in compliance with 007 * the License. You may obtain a copy of the License at 008 * 009 * http://www.apache.org/licenses/LICENSE-2.0 010 * 011 * Unless required by applicable law or agreed to in writing, software 012 * distributed under the License is distributed on an "AS IS" BASIS, 013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 014 * See the License for the specific language governing permissions and 015 * limitations under the License. 016 */ 017package org.apache.juneau.utils; 018 019import java.text.*; 020 021import org.apache.juneau.marshaller.*; 022 023/** 024 * A utility for logging formatted messages to the console. 025 * Uses the {@link Json5} marshaller for serializing objects so any 026 * POJOs can be used as format arguments. 027 */ 028public class Console { 029 030 /** 031 * Prints a message with arguments to {@link System#out}. 032 * 033 * <p> 034 * Arguments are automatically converted to strings using the {@link Json5} marshaller. 035 * 036 * <p> 037 * Useful for debug messages. 038 * 039 * <h5 class='figure'>Example:</h5> 040 * <p class='bjava'> 041 * Console.<jsm>out</jsm>(<js>"myPojo={0}"</js>, <jv>myPojo</jv>); 042 * </p> 043 * 044 * @param msg The {@link MessageFormat}-styled message. 045 * @param args The arguments sent to the the formatter after running them through the {@link Json5} marshaller. 046 */ 047 public static final void out(String msg, Object...args) { 048 System.out.println(format(msg, args)); 049 } 050 051 /** 052 * Prints a message with arguments to {@link System#err}. 053 * 054 * <p> 055 * Arguments are automatically converted to strings using the {@link Json5} marshaller. 056 * 057 * <p> 058 * Useful for debug messages. 059 * 060 * <h5 class='figure'>Example:</h5> 061 * <p class='bjava'> 062 * Console.<jsm>err</jsm>(<js>"myPojo={0}"</js>, <jv>myPojo</jv>); 063 * </p> 064 * 065 * @param msg The {@link MessageFormat}-styled message. 066 * @param args The arguments sent to the the formatter after running them through the {@link Json5} marshaller. 067 */ 068 public static final void err(String msg, Object...args) { 069 System.err.println(format(msg, args)); // NOT DEBUG 070 } 071 072 /** 073 * Formats a message with arguments. 074 * 075 * <p> 076 * Arguments are automatically converted to strings using the {@link Json5} marshaller. 077 * 078 * <p> 079 * Useful for debug messages. 080 * 081 * <h5 class='figure'>Example:</h5> 082 * <p class='bjava'> 083 * String <jv>msg</jv> = Console.<jsm>format</jsm>(<js>"myPojo={0}"</js>, <jv>myPojo</jv>); 084 * </p> 085 * 086 * @param msg The {@link MessageFormat}-styled message. 087 * @param args The arguments sent to the the formatter after running them through the {@link Json5} marshaller. 088 * @return This object. 089 */ 090 public static final String format(String msg, Object...args) { 091 for (int i = 0; i < args.length; i++) 092 args[i] = Json5.of(args[i]); 093 return MessageFormat.format(msg, args); 094 } 095}