| 123456789101112131415161718192021222324252627282930313233343536373839 |
- package com.utic.ptis.server.utils;
- import java.nio.charset.Charset;
- import java.text.DecimalFormat;
- public class LogUtils {
- private LogUtils() {}
- public static String repeatString(String str, int count) {
- StringBuilder sb = new StringBuilder();
- for (int i = 0; i < count; i++) {
- sb.append(str);
- }
- return sb.toString();
- }
- public static String adjustLogMsg(String logMsg, int targetBytes) {
- byte[] bytes = logMsg.getBytes(Charset.forName("EUC-KR"));
- if (bytes.length > targetBytes) {
- // 초과하는 경우 한글이 깨지지 않도록 문자열을 자름
- while (logMsg.getBytes(Charset.forName("EUC-KR")).length > targetBytes) {
- logMsg = logMsg.substring(0, logMsg.length() - 1);
- }
- } else {
- // 부족한 경우 앞쪽에 공백 추가
- int paddingSize = targetBytes - bytes.length;
- logMsg = repeatString(" ", paddingSize) + logMsg; // 공백을 반복하여 추가
- }
- return logMsg;
- }
- public static String elapsedLog(String logMsg, int count, long elapsedTime) {
- String adjustedMsg = adjustLogMsg(logMsg, 30);
- DecimalFormat df = new DecimalFormat("#,###");
- return String.format("%s: [%7s EA, %8s ms]", adjustedMsg, df.format(count), df.format(elapsedTime));
- }
- }
|