LogUtils.java 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839
  1. package com.utic.ptis.server.utils;
  2. import java.nio.charset.Charset;
  3. import java.text.DecimalFormat;
  4. public class LogUtils {
  5. private LogUtils() {}
  6. public static String repeatString(String str, int count) {
  7. StringBuilder sb = new StringBuilder();
  8. for (int i = 0; i < count; i++) {
  9. sb.append(str);
  10. }
  11. return sb.toString();
  12. }
  13. public static String adjustLogMsg(String logMsg, int targetBytes) {
  14. byte[] bytes = logMsg.getBytes(Charset.forName("EUC-KR"));
  15. if (bytes.length > targetBytes) {
  16. // 초과하는 경우 한글이 깨지지 않도록 문자열을 자름
  17. while (logMsg.getBytes(Charset.forName("EUC-KR")).length > targetBytes) {
  18. logMsg = logMsg.substring(0, logMsg.length() - 1);
  19. }
  20. } else {
  21. // 부족한 경우 앞쪽에 공백 추가
  22. int paddingSize = targetBytes - bytes.length;
  23. logMsg = repeatString(" ", paddingSize) + logMsg; // 공백을 반복하여 추가
  24. }
  25. return logMsg;
  26. }
  27. public static String elapsedLog(String logMsg, int count, long elapsedTime) {
  28. String adjustedMsg = adjustLogMsg(logMsg, 30);
  29. DecimalFormat df = new DecimalFormat("#,###");
  30. return String.format("%s: [%7s EA, %8s ms]", adjustedMsg, df.format(count), df.format(elapsedTime));
  31. }
  32. }