Add logging configuration.

This commit is contained in:
2019-02-18 22:38:35 +01:00
parent 8044b24144
commit 0d27ccced8
4 changed files with 86 additions and 6 deletions

View File

@@ -6,6 +6,8 @@ import java.nio.file.Path;
import java.nio.file.Paths; import java.nio.file.Paths;
import org.apache.commons.lang.RandomStringUtils; import org.apache.commons.lang.RandomStringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value; import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.io.Resource; import org.springframework.core.io.Resource;
import org.springframework.core.io.UrlResource; import org.springframework.core.io.UrlResource;
@@ -14,14 +16,16 @@ import org.springframework.web.multipart.MultipartFile;
@Service @Service
public class FileUploadService { public class FileUploadService {
/** Logger. */
private static final Logger LOG = LoggerFactory.getLogger(FileUploadService.class);
/** Length of uploaded file names. */ /** Length of uploaded file names. */
private static final int DESTINATION_IMAGE_NAME_LENGTH = 30; private static final int DESTINATION_IMAGE_NAME_LENGTH = 30;
@Value("codiki.files.upload") @Value("${codiki.files.upload}")
private String folderUpload; private String folderUpload;
@Value("codiki.files.profile-images") @Value("${codiki.files.profile-images}")
private String folderProfileImages; private String folderProfileImages;
@Value("codiki.files.images") @Value("${codiki.files.images}")
private String folderImages; private String folderImages;
/** /**
@@ -46,9 +50,12 @@ public class FileUploadService {
String result; String result;
try { try {
result = buildDestinationFileName(); result = buildDestinationFileName();
Files.copy(pFile.getInputStream(), Paths.get(pPath).resolve(result)); final Path destinationFile = Paths.get(pPath).resolve(result);
LOG.debug("Upload file saved as {}", destinationFile.toString());
Files.copy(pFile.getInputStream(), destinationFile);
return result; return result;
} catch (final Exception pEx) { } catch (final Exception pEx) {
LOG.error("Error during file upload.", pEx);
// TODO : Refactor exception // TODO : Refactor exception
throw new RuntimeException(); throw new RuntimeException();
} }

View File

@@ -9,6 +9,8 @@ import javax.servlet.http.HttpServletResponse;
import org.codiki.core.entities.dto.ImageDTO; import org.codiki.core.entities.dto.ImageDTO;
import org.codiki.core.utils.StringUtils; import org.codiki.core.utils.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.Resource; import org.springframework.core.io.Resource;
import org.springframework.http.HttpHeaders; import org.springframework.http.HttpHeaders;
@@ -25,6 +27,7 @@ import org.springframework.web.multipart.MultipartFile;
@RestController @RestController
@RequestMapping("/api/images") @RequestMapping("/api/images")
public class ImageController { public class ImageController {
private static final Logger LOG = LoggerFactory.getLogger(ImageController.class);
@Autowired @Autowired
private ImageService imageService; private ImageService imageService;
@@ -32,11 +35,13 @@ public class ImageController {
@PostMapping("/uploadAvatar") @PostMapping("/uploadAvatar")
public ResponseEntity<String> uploadAvatar(@RequestParam("file") MultipartFile pFile, public ResponseEntity<String> uploadAvatar(@RequestParam("file") MultipartFile pFile,
final HttpServletRequest pRequest, final HttpServletResponse pResponse, final Principal pPrincipal) { final HttpServletRequest pRequest, final HttpServletResponse pResponse, final Principal pPrincipal) {
LOG.debug("Upload avatar.");
ResponseEntity<String> result; ResponseEntity<String> result;
try { try {
result = ResponseEntity.status(HttpStatus.OK) result = ResponseEntity.status(HttpStatus.OK)
.body(imageService.uploadAvatar(pFile, pRequest, pResponse, pPrincipal)); .body(imageService.uploadAvatar(pFile, pRequest, pResponse, pPrincipal));
} catch(final Exception pEx) { } catch(final Exception pEx) {
LOG.error("Error during avatar upload.", pEx);
result = ResponseEntity.status(HttpStatus.EXPECTATION_FAILED) result = ResponseEntity.status(HttpStatus.EXPECTATION_FAILED)
.body(StringUtils.concat("Fail to upload ", pFile.getOriginalFilename() + ".")); .body(StringUtils.concat("Fail to upload ", pFile.getOriginalFilename() + "."));
} }

View File

@@ -10,12 +10,14 @@ spring.jpa.properties.hibernate.temp.use_jdbc_metadata_defaults = false
# Because detection is disabled you have to set correct dialect by hand. # Because detection is disabled you have to set correct dialect by hand.
spring.jpa.database-platform=org.hibernate.dialect.PostgreSQL9Dialect spring.jpa.database-platform=org.hibernate.dialect.PostgreSQL9Dialect
logging.level.org.hibernate=DEBUG #logging.level.org.hibernate=DEBUG
spring.servlet.multipart.max-file-size=104857600 spring.servlet.multipart.max-file-size=104857600
codiki.files.upload=/opt/codiki/pictures/tmp codiki.files.upload=/opt/codiki/pictures/tmp
codiki.files.profile-images=/opt/codiki/pictures/profiles codiki.files.profile-images=/opt/codiki/pictures/profiles
codiki.files.images=/opt/codiki/pictures/posts codiki.files.images=/opt/codiki/pictures/posts
logging.level.org.codiki=DEBUG
logging.path=/opt/codiki/logs
logging.file=codiki
cors.enabled=false cors.enabled=false

View File

@@ -0,0 +1,66 @@
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<property resource="application.properties" />
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<target>System.out</target>
<filter class="ch.qos.logback.classic.filter.LevelFilter">
<level>DEBUG</level>
<onMatch>ACCEPT</onMatch>
</filter>
<filter class="ch.qos.logback.classic.filter.LevelFilter">
<level>INFO</level>
<onMatch>ACCEPT</onMatch>
</filter>
<filter class="ch.qos.logback.classic.filter.LevelFilter">
<level>TRACE</level>
<onMatch>ACCEPT</onMatch>
</filter>
<filter class="ch.qos.logback.classic.filter.LevelFilter">
<level>WARN</level>
<onMatch>DENY</onMatch>
</filter>
<filter class="ch.qos.logback.classic.filter.LevelFilter">
<level>ERROR</level>
<onMatch>DENY</onMatch>
</filter>
<encoder>
<pattern>%date %-5level [%-10.10thread] %-40.40logger{40} %msg%n</pattern>
</encoder>
</appender>
<appender name="STDERR" class="ch.qos.logback.core.ConsoleAppender">
<target>System.err</target>
<filter class="ch.qos.logback.classic.filter.ThresholdFilter">
<level>WARN</level>
</filter>
<encoder>
<pattern>%date %-5level [%-10.10thread] %-40.40logger{40} %msg%n</pattern>
</encoder>
</appender>
<appender name="FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>${logging.path}/${logging.file}.log</file>
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<fileNamePattern>${logging.path}/${logging.file}.%d{yyyy-MM-dd}.%i.log</fileNamePattern>
<timeBasedFileNamingAndTriggeringPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
<maxFileSize>5MB</maxFileSize>
</timeBasedFileNamingAndTriggeringPolicy>
<maxHistory>30</maxHistory>
</rollingPolicy>
<encoder>
<pattern>%date %-5level [%-10.10thread] %-40.40logger{40} %msg%n</pattern>
</encoder>
</appender>
<logger name="org.codiki" level="INFO"/>
<root level="WARN">
<appender-ref ref="STDOUT"/>
<appender-ref ref="STDERR"/>
<appender-ref ref="FILE"/>
<!-- <springProfile name="production">
<appender-ref ref="FILE"/>
</springProfile> -->
</root>
</configuration>