Skip to content

Commit

Permalink
Merge pull request #11 from ALEEF02/feature/logging
Browse files Browse the repository at this point in the history
Feature/logging
  • Loading branch information
JonMike8 authored Nov 25, 2024
2 parents c10f401 + d98c0ae commit 4c72ba4
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 10 deletions.
Empty file added .gitignore
Empty file.
4 changes: 3 additions & 1 deletion MavenBack/.gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
/target/
*.cfg
*.cfg
.idea
/logs
14 changes: 13 additions & 1 deletion MavenBack/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,19 @@
<version>1.10.0</version>
</dependency>
-->

<!-- SLF4J API -->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>2.0.9</version> <!-- Use the latest version -->
</dependency>

<!-- Logback Classic (SLF4J binding) -->
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<version>1.4.11</version> <!-- Use the latest version -->
</dependency>
</dependencies>

<build>
Expand Down
20 changes: 12 additions & 8 deletions MavenBack/src/main/java/ppp/ServerMain.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@

import com.kaaz.configuration.ConfigurationBuilder;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import ppp.service.ServiceHandlerThread;
//import ppp.ServerConfig;
import ppp.db.WebDb;
Expand All @@ -23,12 +25,14 @@

public class ServerMain {

public static void main(String[] args) throws Exception {
private static final Logger logger = LoggerFactory.getLogger(ServerMain.class);

public static void main(String[] args) throws Exception {
// Create a server that listens on port 8080.
Server server = new Server(8080);
WebAppContext webAppContext = new WebAppContext();
server.setHandler(webAppContext);
logger.info("Web server created.");

// Load static content from the resources directory.
URL webAppDir =
Expand All @@ -49,27 +53,27 @@ public static void main(String[] args) throws Exception {
new ConfigurationBuilder(ServerConfig.class, new File("application.cfg")).build(true);

// Connect to the DB
System.out.print("DB connecting...\n\t");
logger.info("DB connecting...");
WebDb.init();
System.out.println("DB connected.");
logger.info("DB connected.");

// Initialize out DB caches
System.out.print("Initializing caches... ");
logger.info("Initializing caches...");
CUser.init();
CGames.init();
CGlicko.init();
GlickoTwo.init();
System.out.println("done.");
logger.info("Caches initialized.");

// Initialize the services
System.out.print("Initializing services... ");
logger.info("Initializing services...");
Thread serviceHandler = new ServiceHandlerThread();
serviceHandler.start();
System.out.println("done.");
logger.info("Services initialized.");

// Start the server! 🚀
server.start();
System.out.println("Server started!");
logger.info("Server started!");

// Keep the main thread alive while the server is running.
server.join();
Expand Down
36 changes: 36 additions & 0 deletions MavenBack/src/main/resources/logback.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<configuration>
<!-- Console Appender -->
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<pattern>%d{yyyy-MM-dd HH:mm:ss} %-5level %logger{36} - %msg%n</pattern>
</encoder>
</appender>

<!-- General Log File Appender -->
<appender name="FILE" class="ch.qos.logback.core.FileAppender">
<file>logs/application.log</file>
<append>true</append> <!-- Append to file instead of overwriting -->
<encoder>
<pattern>%d{yyyy-MM-dd HH:mm:ss} %-5level %logger{36} - %msg%n</pattern>
</encoder>
</appender>

<!-- Error Log File Appender -->
<appender name="ERROR_FILE" class="ch.qos.logback.core.FileAppender">
<file>logs/error.log</file>
<append>true</append>
<encoder>
<pattern>%d{yyyy-MM-dd HH:mm:ss} %-5level %logger{36} - %msg%n</pattern>
</encoder>
<filter class="ch.qos.logback.classic.filter.ThresholdFilter">
<level>ERROR</level> <!-- Only logs at ERROR level will be written -->
</filter>
</appender>

<!-- Root Logger Configuration -->
<root level="info">
<appender-ref ref="STDOUT" /> <!-- Logs to console -->
<appender-ref ref="FILE" /> <!-- Logs to general log file -->
<appender-ref ref="ERROR_FILE" /> <!-- Logs to error-specific file -->
</root>
</configuration>

0 comments on commit 4c72ba4

Please sign in to comment.