| 1 | |
package jp.co.y_net.amm.common; |
| 2 | |
|
| 3 | |
import org.slf4j.LoggerFactory; |
| 4 | |
|
| 5 | |
|
| 6 | |
|
| 7 | |
|
| 8 | |
|
| 9 | |
|
| 10 | |
|
| 11 | |
|
| 12 | |
|
| 13 | |
|
| 14 | |
|
| 15 | |
|
| 16 | |
|
| 17 | |
|
| 18 | |
|
| 19 | |
|
| 20 | |
|
| 21 | |
|
| 22 | |
|
| 23 | |
|
| 24 | |
|
| 25 | |
|
| 26 | |
|
| 27 | 0 | public class AppLogger { |
| 28 | |
private static boolean isLocal; |
| 29 | 0 | private static org.slf4j.Logger loggerSlf = LoggerFactory.getLogger(AppLogger.class.getName()); |
| 30 | |
|
| 31 | |
static { |
| 32 | |
|
| 33 | |
|
| 34 | |
|
| 35 | |
|
| 36 | |
|
| 37 | 0 | String debugKeys = ResourceReader.getStringQuick("debug.users"); |
| 38 | |
String[] arrayDebugKey; |
| 39 | 0 | if(debugKeys == null) { |
| 40 | 0 | arrayDebugKey = new String[]{}; |
| 41 | 0 | } else { |
| 42 | 0 | arrayDebugKey = debugKeys.split(","); |
| 43 | |
} |
| 44 | 0 | for(String key: arrayDebugKey) { |
| 45 | 0 | if (key.trim().equals(System.getenv("USERNAME"))) { |
| 46 | 0 | isLocal = true; |
| 47 | 0 | break; |
| 48 | |
} |
| 49 | |
} |
| 50 | 0 | } |
| 51 | |
|
| 52 | |
|
| 53 | |
public static void main(String[] args) throws InterruptedException { |
| 54 | 0 | sample(); |
| 55 | 0 | } |
| 56 | |
|
| 57 | |
private static void sample() { |
| 58 | 0 | String str = "sundata"; |
| 59 | 0 | AppLogger.debug("変数 str の値 = " + str); |
| 60 | |
|
| 61 | |
|
| 62 | |
|
| 63 | |
|
| 64 | |
|
| 65 | |
|
| 66 | 0 | AppLogger.info("Infoレベルの出力。"); |
| 67 | 0 | AppLogger.info(new Exception("例外クラスにも対応")); |
| 68 | |
|
| 69 | |
|
| 70 | |
|
| 71 | |
|
| 72 | |
|
| 73 | 0 | AppLogger.error("Errorレベルの出力。"); |
| 74 | 0 | AppLogger.error(new Exception("例外クラスにも対応")); |
| 75 | |
try { |
| 76 | 0 | int[] i = new int[]{1, 2, 3}; |
| 77 | 0 | if (i[3] == 4) { |
| 78 | 0 | return; |
| 79 | |
} |
| 80 | 0 | } catch (Exception e) { |
| 81 | 0 | AppLogger.error("配列の操作に失敗", e); |
| 82 | |
} |
| 83 | 0 | } |
| 84 | |
public static boolean isDebug() { |
| 85 | 0 | return isLocal; |
| 86 | |
} |
| 87 | |
|
| 88 | |
|
| 89 | |
|
| 90 | |
|
| 91 | |
|
| 92 | |
|
| 93 | |
public static void debug(String message) { |
| 94 | 0 | if (isLocal) System.out.println("[DEBUG] " + toPlace(getFromStack()) + "\n" + toString(message)); |
| 95 | 0 | } |
| 96 | |
|
| 97 | |
|
| 98 | |
|
| 99 | |
|
| 100 | |
|
| 101 | |
|
| 102 | |
|
| 103 | |
|
| 104 | |
|
| 105 | |
|
| 106 | |
|
| 107 | |
public static void info(String message) { |
| 108 | 0 | if (isLocal) System.out.println("[INFO ] " + toPlace(getFromStack()) + "\n" + toString(message)); |
| 109 | 0 | loggerSlf.info(toPlace(getFromStack()) + "\n" + toString(message)); |
| 110 | 0 | } |
| 111 | |
|
| 112 | |
|
| 113 | |
|
| 114 | |
|
| 115 | |
public static void info(Throwable exception) { |
| 116 | 0 | if (isLocal) System.out.println("[INFO ] " + toPlace(getFromStack()) + "\n" + toString(exception)); |
| 117 | 0 | loggerSlf.info(toPlace(getFromStack()) + "\n" + toString(exception)); |
| 118 | 0 | } |
| 119 | |
|
| 120 | |
|
| 121 | |
|
| 122 | |
|
| 123 | |
|
| 124 | |
public static void info(String message, Throwable exception) { |
| 125 | 0 | if (isLocal) System.out.println("[INFO ] " + toPlace(getFromStack()) + "\n" + toString(message)); |
| 126 | 0 | if (isLocal) System.out.println("[INFO ] " + toPlace(getFromStack()) + "\n" + toString(exception)); |
| 127 | 0 | loggerSlf.info(toPlace(getFromStack()) + "\n" + toString(message)); |
| 128 | 0 | loggerSlf.info(toPlace(getFromStack()) + "\n" + toString(exception)); |
| 129 | 0 | } |
| 130 | |
|
| 131 | |
|
| 132 | |
|
| 133 | |
|
| 134 | |
public static void error(String message) { |
| 135 | 0 | if (isLocal) System.out.println("[ERROR] " + toPlace(getFromStack()) + "\n" + toString(message)); |
| 136 | 0 | loggerSlf.error(toPlace(getFromStack()) + "\n" + toString(message)); |
| 137 | 0 | } |
| 138 | |
|
| 139 | |
|
| 140 | |
|
| 141 | |
|
| 142 | |
public static void error(Throwable exception) { |
| 143 | 0 | if (isLocal) System.out.println("[ERROR] " + toPlace(getFromStack()) + "\n" + toString(exception)); |
| 144 | 0 | loggerSlf.error(toPlace(getFromStack()) + "\n" + toString(exception)); |
| 145 | 0 | } |
| 146 | |
|
| 147 | |
|
| 148 | |
|
| 149 | |
|
| 150 | |
public static void error(String message, Throwable exception) { |
| 151 | 0 | if (isLocal) System.out.println("[ERROR] " + toPlace(getFromStack()) + "\n" + toString(message)); |
| 152 | 0 | if (isLocal) System.out.println("[ERROR] " + toPlace(getFromStack()) + "\n" + toString(exception)); |
| 153 | 0 | loggerSlf.error(toPlace(getFromStack()) + "\n" + toString(message)); |
| 154 | 0 | loggerSlf.error(toPlace(getFromStack()) + "\n" + toString(exception)); |
| 155 | 0 | } |
| 156 | |
|
| 157 | |
|
| 158 | |
private static StackTraceElement getFromStack() { |
| 159 | 0 | return new Throwable().getStackTrace()[2]; |
| 160 | |
} |
| 161 | |
private static String toPlace(StackTraceElement stack) { |
| 162 | 0 | return stack.getClassName() + "#" + stack.getMethodName() + "(" |
| 163 | 0 | + stack.getFileName() + ":" + stack.getLineNumber() + ")"; |
| 164 | |
} |
| 165 | |
private static String toString(Object message) { |
| 166 | |
String log; |
| 167 | 0 | if (message instanceof Throwable) { |
| 168 | 0 | log = " " + readStackTrace(new StringBuilder(), (Throwable) message, 1, "jp.co.y_net").toString(); |
| 169 | 0 | } else if (message == null) { |
| 170 | 0 | log = " " + null; |
| 171 | 0 | } else { |
| 172 | 0 | log = " " + message.toString(); |
| 173 | |
} |
| 174 | 0 | return log; |
| 175 | |
} |
| 176 | |
|
| 177 | |
|
| 178 | |
|
| 179 | |
|
| 180 | |
|
| 181 | |
|
| 182 | |
private static StringBuilder readStackTrace(StringBuilder sb, Throwable throwable, int token, String filter) { |
| 183 | 0 | if(sb == null) sb = new StringBuilder(); |
| 184 | 0 | final String LINE_SEP = System.getProperty("line.separator"); |
| 185 | |
try { |
| 186 | 0 | String spacer = ""; |
| 187 | 0 | for (int i = 0; i < token; i++) { |
| 188 | 0 | spacer += " "; |
| 189 | |
} |
| 190 | 0 | sb.append(spacer + throwable.getClass().getName() + ":" + throwable.getMessage() + LINE_SEP); |
| 191 | 0 | StackTraceElement[] se = throwable.getStackTrace(); |
| 192 | 0 | for (int i = 0; i < se.length; i++) { |
| 193 | 0 | String trace = se[i].toString(); |
| 194 | 0 | if(filter == null |
| 195 | 0 | || (filter != null && trace.startsWith(filter))) { |
| 196 | |
|
| 197 | 0 | sb.append(spacer + " " + trace + LINE_SEP); |
| 198 | |
} |
| 199 | |
} |
| 200 | 0 | if (throwable.getCause() != null) { |
| 201 | |
|
| 202 | 0 | readStackTrace(sb, throwable.getCause(), token + 1, filter); |
| 203 | |
} |
| 204 | 0 | } catch (Exception e) { |
| 205 | |
|
| 206 | 0 | e.printStackTrace(); |
| 207 | |
} |
| 208 | 0 | return sb; |
| 209 | |
} |
| 210 | |
} |