001/* 002 * (C) Copyright 2006-2015 Nuxeo SA (http://nuxeo.com/) and others. 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 * 016 * Contributors: 017 * mguillaume 018 */ 019 020package org.nuxeo.launcher.info; 021 022import java.io.PrintWriter; 023import java.io.StringWriter; 024import java.io.Writer; 025import java.text.DateFormat; 026import java.util.ArrayList; 027import java.util.Date; 028import java.util.List; 029 030import org.apache.commons.logging.impl.SimpleLog; 031 032public class MessageInfoLogger { 033 034 private List<MessageInfo> messages = new ArrayList<>(); 035 036 public List<MessageInfo> getMessages() { 037 return messages; 038 } 039 040 public void printMessages() { 041 DateFormat dateFormat = DateFormat.getDateTimeInstance(DateFormat.SHORT, DateFormat.SHORT); 042 for (MessageInfo message : messages) { 043 System.out.println("[" + dateFormat.format(message.time) + "] " + message.level + " " + message.message); 044 } 045 } 046 047 public void log(String msg, int level) { 048 MessageInfo message = new MessageInfo(); 049 message.time = new Date(); 050 message.level = level; 051 message.message = msg; 052 messages.add(message); 053 } 054 055 public void debug(Object... args) { 056 for (Object arg : args) { 057 if (arg instanceof String) { 058 debug((String) arg); 059 } else if (arg instanceof Throwable) { 060 Writer stringWriter = new StringWriter(); 061 PrintWriter stackWriter = new PrintWriter(stringWriter); 062 ((Throwable) arg).printStackTrace(stackWriter); 063 debug(stringWriter.toString()); 064 } else { 065 debug(arg.toString()); 066 } 067 } 068 } 069 070 public void debug(String msg) { 071 log(msg, SimpleLog.LOG_LEVEL_DEBUG); 072 } 073 074 public void info(Object... args) { 075 for (Object arg : args) { 076 if (arg instanceof String) { 077 info((String) arg); 078 } else if (arg instanceof Throwable) { 079 Writer stringWriter = new StringWriter(); 080 PrintWriter stackWriter = new PrintWriter(stringWriter); 081 ((Throwable) arg).printStackTrace(stackWriter); 082 info(stringWriter.toString()); 083 } else { 084 info(arg.toString()); 085 } 086 } 087 } 088 089 public void info(String msg) { 090 log(msg, SimpleLog.LOG_LEVEL_INFO); 091 } 092 093 public void warn(Object... args) { 094 for (Object arg : args) { 095 if (arg instanceof String) { 096 warn((String) arg); 097 } else if (arg instanceof Throwable) { 098 Writer stringWriter = new StringWriter(); 099 PrintWriter stackWriter = new PrintWriter(stringWriter); 100 ((Throwable) arg).printStackTrace(stackWriter); 101 warn(stringWriter.toString()); 102 } else { 103 warn(arg.toString()); 104 } 105 } 106 } 107 108 public void warn(String msg) { 109 log(msg, SimpleLog.LOG_LEVEL_WARN); 110 } 111 112 public void error(Object... args) { 113 for (Object arg : args) { 114 if (arg instanceof String) { 115 error((String) arg); 116 } else if (arg instanceof Throwable) { 117 Writer stringWriter = new StringWriter(); 118 PrintWriter stackWriter = new PrintWriter(stringWriter); 119 ((Throwable) arg).printStackTrace(stackWriter); 120 error(stringWriter.toString()); 121 } else { 122 error(arg.toString()); 123 } 124 } 125 } 126 127 public void error(String msg) { 128 log(msg, SimpleLog.LOG_LEVEL_ERROR); 129 } 130 131}