/lofka-persistence/src/main/java/com/github/tsingjyujing/lofka/persistence/util/DocumentUtil.java

https://github.com/TsingJyujing/lofka · Java · 94 lines · 70 code · 7 blank · 17 comment · 9 complexity · 433e9d27cb74404b7b7b292609b792ec MD5 · raw file

  1. package com.github.tsingjyujing.lofka.persistence.util;
  2. import com.github.tsingjyujing.lofka.basic.LoggerJson;
  3. import com.google.gson.Gson;
  4. import org.bson.Document;
  5. import java.util.Date;
  6. import java.util.Map;
  7. import java.util.Properties;
  8. public class DocumentUtil {
  9. private static final Gson GSON = new Gson();
  10. private static final Document EXPIRED_SETTING = new Document(
  11. "TRACE", 1000L * 5 * 60
  12. ).append(
  13. "DEBUG", 1000L * 10 * 60
  14. ).append(
  15. "INFO", 1000L * 24 * 60 * 60
  16. ).append(
  17. "WARN", 1000L * 31 * 24 * 60 * 60
  18. ).append(
  19. "ERROR", 1000L * 366 * 24 * 60 * 60
  20. ).append(
  21. "FATAL", 1000L * 732 * 24 * 60 * 60
  22. ).append(
  23. "DEFAULT", 1000L * 31 * 24 * 60 * 60
  24. ).append(
  25. "NGINX", 1000L * 190 * 24 * 60 * 60
  26. );
  27. /**
  28. * MongoDB 日志处理工具
  29. * <p>
  30. * 1. 时间戳转换为Date类型
  31. * 2. 写入时间追加
  32. * 3. 根据等级和Type确定超时时间
  33. * 4. 以`[` 和 `{` 开头的消息体尝试转换为JSON
  34. *
  35. * @param rawData
  36. * @return
  37. */
  38. public static Document mongoLogProcessor(Document rawData, Document expiredSetting) {
  39. Document doc = new Document(rawData);
  40. // 时间戳转换为Date类型
  41. Double logTime = doc.getDouble(LoggerJson.TAG_TIMESTAMP);
  42. if (doc.containsKey(LoggerJson.TAG_TIMESTAMP)) {
  43. doc.append(LoggerJson.TAG_TIMESTAMP, new Date(logTime.longValue()));
  44. }
  45. doc.append("write_timestamp", new Date(System.currentTimeMillis()));
  46. String level = null;
  47. if (doc.containsKey(LoggerJson.TAG_LEVEL)) {
  48. level = doc.getString(LoggerJson.TAG_LEVEL).toUpperCase();
  49. doc.append(LoggerJson.TAG_LEVEL, level);
  50. }
  51. String logType = null;
  52. if (doc.containsKey("type")) {
  53. logType = doc.getString("type").toUpperCase();
  54. doc.append("type", logType);
  55. } else {
  56. logType = "NO_TYPE";
  57. doc.append("type", "NO_TYPE");
  58. }
  59. double expiredMills = expiredSetting.getDouble("DEFAULT");
  60. if (expiredSetting.containsKey(level)) {
  61. expiredMills = expiredSetting.getDouble(level);
  62. } else if (expiredSetting.containsKey(logType)) {
  63. expiredMills = expiredSetting.getDouble(logType);
  64. }
  65. doc.append("expired_time", new Date((long) (logTime + expiredMills)));
  66. return doc;
  67. }
  68. /**
  69. * @param properties
  70. * @param prefix
  71. * @return
  72. */
  73. public static Document getExpiredSetting(Properties properties, String prefix) {
  74. final Document doc = new Document();
  75. final String prefixWithDot = prefix + ".";
  76. for (Map.Entry<Object, Object> entry : properties.entrySet()) {
  77. final String key = entry.getKey().toString();
  78. if (key.startsWith(prefixWithDot)) {
  79. doc.append(
  80. key.substring(prefixWithDot.length()),
  81. Double.valueOf(entry.getValue().toString())
  82. );
  83. }
  84. }
  85. return doc;
  86. }
  87. }