Change log package to logger

pull/146/head
johnniang 2019-04-22 22:22:40 +08:00
parent e92328911c
commit d0576bf653
6 changed files with 75 additions and 7 deletions

View File

@ -0,0 +1,38 @@
package run.halo.app.event.logger;
import org.springframework.context.ApplicationEvent;
import run.halo.app.model.enums.LogType;
import run.halo.app.model.params.LogParam;
import run.halo.app.utils.ValidationUtils;
/**
* @author johnniang
* @date 19-4-20
*/
public class LogEvent extends ApplicationEvent {
private final LogParam logParam;
/**
* Create a new ApplicationEvent.
*
* @param source the object on which the event initially occurred (never {@code null})
* @param logParam login param
*/
public LogEvent(Object source, LogParam logParam) {
super(source);
// Validate the log param
ValidationUtils.validate(logParam);
this.logParam = logParam;
}
public LogEvent(Object source, String logKey, LogType logType, String content) {
this(source, new LogParam(logKey, logType, content));
}
public LogParam getLogParam() {
return logParam;
}
}

View File

@ -0,0 +1,32 @@
package run.halo.app.event.logger;
import org.springframework.context.event.EventListener;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;
import run.halo.app.model.entity.Log;
import run.halo.app.service.LogService;
/**
* Log event listener.
*
* @author johnniang
* @date 19-4-21
*/
@Component
public class LogEventListener {
private final LogService logService;
public LogEventListener(LogService logService) {
this.logService = logService;
}
@EventListener
@Async
public void onApplicationEvent(LogEvent event) {
// Convert to log
Log logToCreate = event.getLogParam().convertTo();
// Create log
logService.create(logToCreate);
}
}

View File

@ -11,15 +11,13 @@ import org.springframework.data.jpa.domain.Specification;
import org.springframework.lang.NonNull;
import org.springframework.lang.Nullable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;
import org.springframework.util.CollectionUtils;
import run.halo.app.event.log.LogEvent;
import run.halo.app.event.logger.LogEvent;
import run.halo.app.event.post.VisitEvent;
import run.halo.app.exception.AlreadyExistsException;
import run.halo.app.exception.BadRequestException;
import run.halo.app.exception.NotFoundException;
import run.halo.app.exception.ServiceException;
import run.halo.app.model.dto.CategoryOutputDTO;
import run.halo.app.model.dto.TagOutputDTO;
import run.halo.app.model.dto.post.PostMinimalOutputDTO;

View File

@ -8,7 +8,7 @@ import org.springframework.stereotype.Service;
import org.springframework.util.Assert;
import org.springframework.util.CollectionUtils;
import run.halo.app.cache.StringCacheStore;
import run.halo.app.event.log.LogEvent;
import run.halo.app.event.logger.LogEvent;
import run.halo.app.exception.BadRequestException;
import run.halo.app.exception.NotFoundException;
import run.halo.app.model.entity.User;

View File

@ -10,7 +10,7 @@ import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import run.halo.app.event.log.LogEvent;
import run.halo.app.event.logger.LogEvent;
import run.halo.app.exception.BadRequestException;
import run.halo.app.model.entity.*;
import run.halo.app.model.enums.AttachmentType;

View File

@ -3,8 +3,8 @@ package run.halo.app.event;
import lombok.extern.slf4j.Slf4j;
import org.junit.Test;
import org.springframework.context.ApplicationListener;
import run.halo.app.event.log.LogEvent;
import run.halo.app.event.log.LogEventListener;
import run.halo.app.event.logger.LogEvent;
import run.halo.app.event.logger.LogEventListener;
import run.halo.app.utils.ReflectionUtils;
import java.lang.reflect.ParameterizedType;