Skip to content

Feature/logging Adding logging functionality to the application #17

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 30 additions & 8 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,29 @@
<relativePath/> <!-- lookup parent from repository -->
</parent>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>11</java.version>
</properties>

<dependencies>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.30</version>
<scope>provided</scope>
</dependency>


<!--log4j2 dependencies -->
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-api</artifactId>
<version>${log4j-api.version}</version>
</dependency>


<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>${slf4j.version}</version>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
Expand Down Expand Up @@ -53,6 +69,14 @@
<scope>test</scope>
</dependency>
</dependencies>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>17</java.version>
<slf4j.version>1.7.36</slf4j.version>
<log4j-api.version>2.17.1</log4j-api.version>
<disruptor.version>3.3.6</disruptor.version>
</properties>

<build>
<plugins>
Expand All @@ -62,6 +86,4 @@
</plugin>
</plugins>
</build>


</project>
Original file line number Diff line number Diff line change
@@ -1,16 +1,25 @@
package com.example.easynotes.controller;


import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/")
@Slf4j
public class IndexController {

@GetMapping
public String sayHello() {
return "Hello and Welcome to the EasyNotes application. You can create a new Note by making a POST request to /api/notes endpoint.";
try{
log.info("/ API hit");
return "Hello and Welcome to the EasyNotes application. You can create a new Note by making a POST request to /api/notes endpoint.";
} catch (Exception e ){
log.error("Error:{}",e);
return "Contact Administrator";
}

}
}
62 changes: 62 additions & 0 deletions src/main/java/com/example/easynotes/controller/NoteController.java
Original file line number Diff line number Diff line change
@@ -1,25 +1,42 @@
package com.example.easynotes.controller;

import com.example.easynotes.exception.Messages;
import com.example.easynotes.exception.ResourceNotFoundException;
import com.example.easynotes.model.Note;
import com.example.easynotes.repository.NoteRepository;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.validation.Valid;
import java.io.*;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.List;

/**
* Created by rajeevkumarsingh on 27/06/17.
*/
@RestController
@RequestMapping("/api")
@Slf4j
public class NoteController {

@Autowired
NoteRepository noteRepository;


@Autowired
Messages messages;



private static final long MAX_FILE_SIZE = 2L * 1024 * 1024 * 1024; // 10GB


@GetMapping("/notes")
public List<Note> getAllNotes() {
return noteRepository.findAll();
Expand Down Expand Up @@ -59,4 +76,49 @@ public ResponseEntity<?> deleteNote(@PathVariable(value = "id") Long noteId) {

return ResponseEntity.ok().build();
}

@PostMapping("/upload/file")
public ResponseEntity<String> uploadFile(@RequestParam("file") MultipartFile file) throws IOException {
log.info(file.getOriginalFilename());

// #TODO : put this value in properties file
final String uploadLocation="C:\\Users\\harsh\\PROJECTS\\src\\main\\resources\\notes";

// 1. Validate file size (between 2000 and 2000 MB)
if (file.isEmpty() || file.getSize() > MAX_FILE_SIZE) {
log.info("File Size is :{}",file.getSize());
return ResponseEntity.badRequest().body(messages.FileLimit);
}

// 2. Extract filename directly from MultipartFile (assuming no specific format)
String fileName = file.getOriginalFilename();
log.info("File Name uploaded is {}",fileName);

// 3. Sanitize filename
if (fileName == null || fileName.isEmpty() || fileName.contains("..")) {
return new ResponseEntity<>(messages.InvalidFileName, HttpStatus.BAD_REQUEST);
}

// Perform additional processing with the sanitized filename and uploaded file content (not shown for brevity)
Path filePath=Paths.get(uploadLocation).resolve(file.getOriginalFilename());


// Create input stream from uploaded file
try (InputStream inputStream = file.getInputStream();
// Create output stream to write file directly to disk
OutputStream outputStream = new FileOutputStream(filePath.toFile())) {
byte[] buffer = new byte[1024];
int bytesRead;
while ((bytesRead = inputStream.read(buffer)) != -1) {
outputStream.write(buffer, 0, bytesRead);
}

return new ResponseEntity<>(messages.SuccessFullyUploaded,HttpStatus.OK);
} catch(Exception e ){
log.error(messages.ErrorSavingFile);
return new ResponseEntity<>(messages.ErrorSavingFile,HttpStatus.INTERNAL_SERVER_ERROR);
}

}

}
12 changes: 12 additions & 0 deletions src/main/java/com/example/easynotes/exception/Messages.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package com.example.easynotes.exception;

import org.springframework.stereotype.Component;

@Component
public class Messages {
public final String FileNotFound="File Not Found";
public final String InvalidFileName="Invalid FileName";
public final String SuccessFullyUploaded="File uploaded successfully";
public final String ErrorSavingFile ="Error saving the file";
public final String FileLimit=" File must be not empty and less than 2Gb";
}
20 changes: 20 additions & 0 deletions src/main/java/config/AppConfig.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package config;

import com.example.easynotes.exception.Messages;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;

@ComponentScan
@Configuration
public class AppConfig {
private String messages;
private HttpStatus status;
@Bean
public Messages messages(){
return new Messages();
}

}
14 changes: 13 additions & 1 deletion src/main/resources/application.properties
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,16 @@ spring.datasource.password = callicoder
spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.MySQL5InnoDBDialect

# Hibernate ddl auto (create, create-drop, validate, update)
spring.jpa.hibernate.ddl-auto = update
spring.jpa.hibernate.ddl-auto = update

# Loggers and Levels
logging.level.root=INFO
logging.level.org.springframework.web=DEBUG
logging.level.org.hibernate=WARN
logging.level.org.hibernate.SQL=DEBUG
logging.level.org.hibernate.exception=ERROR

# File Logging Configuration
logging.file.path=logs/
logging.file.name=application.log
logging.file.max-size=10MB
Binary file added src/main/resources/notes/2023-01-05 (1).png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added src/main/resources/notes/SortServer2008Compat.nls
Binary file not shown.
Binary file added src/main/resources/notes/SortWindows62.nls
Binary file not shown.
1 change: 1 addition & 0 deletions src/main/resources/notes/thoughts.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
you need to experience the bad to enjoy the good and the good to identify the bad