Skip to content

Commit

Permalink
Feat/#81 로그 레벨 세분화 (#85)
Browse files Browse the repository at this point in the history
* feature: 로그 레벨 세분화

- enum 값 추가

* feature: 로그 저장방식 변경

- enum의 ordinal 값을 사용해서 저장하도록 변경

* feature: log 생성자 추가

- 정수형 level을 통해서 생성할 수 있도록 추가
  • Loading branch information
LuizyHub committed Aug 19, 2024
1 parent 019faa7 commit d9c3e1e
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 5 deletions.
13 changes: 12 additions & 1 deletion logbat/src/main/java/info/logbat/domain/log/domain/Log.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public Log(String appKey, String level, String data, LocalDateTime timestamp) {
this(null, appKey, level, data, timestamp);
}

public Log(Long logId, String appKey, String level, String data,
public Log(Long logId, String appKey, Integer level, String data,
LocalDateTime timestamp) {
this.logId = logId;
validateAppKey(appKey);
Expand All @@ -33,6 +33,17 @@ public Log(Long logId, String appKey, String level, String data,
this.timestamp = timestamp;
}

public Log(Long logId, String appKey, String level, String data,
LocalDateTime timestamp) {
this.logId = logId;
validateAppKey(appKey);
this.appKey = appKey;
this.level = Level.from(level);
this.data = LogData.from(data);
validateTimestamp(timestamp);
this.timestamp = timestamp;
}

private void validateAppKey(String appKey) {
if (appKey == null || appKey.isBlank()) {
throw new IllegalArgumentException("appKey는 null이거나 빈 문자열이 될 수 없습니다.");
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
package info.logbat.domain.log.domain.enums;

public enum Level {
ERROR,
INFO;
TRACE, // 0
DEBUG, // 1
INFO, // 2
WARN, // 3
ERROR; // 4


public static Level from(String level) {
if (level == null || level.isBlank()) {
Expand All @@ -19,4 +23,18 @@ public static Level from(String level) {

throw new IllegalArgumentException("level이 올바르지 않습니다.");
}

public static Level from(Integer level) {
if (level == null) {
throw new IllegalArgumentException("level은 null이 될 수 없습니다.");
}

for (Level logLevel : Level.values()) {
if (logLevel.ordinal() == level) {
return logLevel;
}
}

throw new IllegalArgumentException("level이 올바르지 않습니다.");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public long save(Log log) {
jdbcTemplate.update(connection -> {
PreparedStatement ps = connection.prepareStatement(sql, Statement.RETURN_GENERATED_KEYS);
ps.setBytes(1, UUIDUtil.uuidStringToBytes(log.getAppKey()));
ps.setString(2, log.getLevel().name());
ps.setInt(2, log.getLevel().ordinal());
ps.setString(3, log.getData().getValue());
ps.setTimestamp(4, Timestamp.valueOf(log.getTimestamp()));
return ps;
Expand Down Expand Up @@ -57,7 +57,7 @@ public Optional<Log> findById(Long logId) {
private static final RowMapper<Log> LOG_ROW_MAPPER = (rs, rowNum) -> new Log(
rs.getLong("log_id"),
UUIDUtil.bytesToUuidString(rs.getBytes("app_key")),
rs.getString("level"),
rs.getInt("level"),
rs.getString("data"),
rs.getTimestamp("timestamp").toLocalDateTime()
);
Expand Down

0 comments on commit d9c3e1e

Please sign in to comment.